fix(schema): harden migrations, FKs, seed, and truncation (PMS-196) #210
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/pms-196-harden-migrations-rls-integrity"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Most RLS findings were already closed by 038/041 (fail-closed policy, tenant-less table coverage). This change handles the remaining schema-integrity and code-path findings, using new forward migrations rather than editing shipped migrations (sqlx validates the checksum of every applied migration on run, and that path is now fatal).
New migration 042_pms196_fks_and_triggers.sql: re-runs the updated_at trigger sweep idempotently (DROP TRIGGER IF EXISTS + CREATE), which both makes trigger attachment re-runnable and attaches the trigger to post-024 tables that never got one (e.g. kb_article_votes); adds appointment_reminders FKs on tenant_id/appointment_id with ON DELETE CASCADE; adds the four bare companies pointer FKs with ON DELETE SET NULL.
New auth migration 20260516000001_org_invitations_citext.sql: changes org_invitations.email to CITEXT to match admin_invites (so case no longer bypasses the one-open-invite-per-(tenant,email) guard) and adds the octet_length(token_hash)=32 CHECK that admin_invites already carries.
src/main.rs: migration failure now fails fast (.expect) instead of being swallowed as a warning, so the server never starts on a stale schema.
src/modules/tenants/service.rs: copy_default_config no longer parses a hardcoded magic UUID with unwrap(); the seed-source tenant is read from MOKOSH_SEED_TENANT_ID (fallible parse, Configuration error on malformed value) and falls back to Uuid::from_u128(1).
src/utils/validation.rs: truncate() snaps the cut point back to the nearest char boundary via char_indices so multi-byte UTF-8 input no longer panics; adds a regression test.
#PMS-196
Co-Authored-By: Claude Opus 4.8 (1M context) noreply@anthropic.com