nxtgauge-backend-rust/crates/db/migrations/20260421235959_role_config_table_renames_early.up.sql
Ashwin Kumar Sivakumar cc2dab112b
All checks were successful
build-and-release / build (cron) (push) Successful in 58s
build-and-release / build (companies) (push) Successful in 1m59s
build-and-release / build (customers) (push) Successful in 2m9s
build-and-release / build (employees) (push) Successful in 2m17s
build-and-release / build (developers) (push) Successful in 2m29s
build-and-release / build (catering-services) (push) Successful in 2m45s
build-and-release / build (gateway) (push) Successful in 49s
build-and-release / build (jobs) (push) Successful in 42s
build-and-release / build (fitness-trainers) (push) Successful in 2m38s
build-and-release / build (graphic-designers) (push) Successful in 1m57s
build-and-release / build (payments) (push) Successful in 1m54s
build-and-release / build (job-seekers) (push) Successful in 2m57s
build-and-release / build (makeup-artists) (push) Successful in 2m47s
build-and-release / build (photographers) (push) Successful in 2m48s
build-and-release / build (social-media-managers) (push) Successful in 2m59s
backend-integration-tests / ai-credits (push) Successful in 50s
build-and-release / build (tutors) (push) Successful in 2m45s
build-and-release / build (ugc-content-creators) (push) Successful in 2m50s
build-and-release / build (video-editors) (push) Successful in 2m40s
build-and-release / build (users) (push) Successful in 4m47s
fix(db): 4 fresh-bootstrap migration chain bugs found + verified today
Reconciling the local dev DB (63 migrations behind) surfaced 4 genuine
bugs in the migration chain itself -- not just this DB's legacy
scripts/init-db.sql seeding -- confirmed by replaying the full chain
against a brand new, completely empty database from scratch:

- 20260318235959: customer_profiles.status is UPDATEd by
  20260319090000's backfill 4 months before it's ever ADDed
  (20260721020000). Adds it early (IF NOT EXISTS).
- 20260401235959: 20260317190000 creates the old users-linked
  `employees` shape; 20260402030000's CREATE TABLE IF NOT EXISTS no-ops
  against it and fails creating an index on a column that was never
  added. Conditionally drops the old shape first (only if it's still
  pre-transition -- gated on the missing `email` column), restoring
  20260402030000's own documented original intent.
- 20260419235959: the schema `20260420000003_external_role_modules`
  needs (persona_types, modules, role_module_access, ...) was only ever
  defined in a disabled .up.sql.skip (its version slot was taken by
  ...seed.sql); the seed data that depends on it was never skipped.
  Creates that schema early -- verbatim content, purely additive.
- 20260421235959: same disabled-migration pattern for the
  role_permissions -> role_admin_permissions / dashboard_configs ->
  role_sidebar_configs / runtime_configs -> role_runtime_configs /
  user_roles -> user_role_assignments renames that 20260422000000's
  widget seed needs. Conditionally renames each pair (only if old name
  exists and new name doesn't), verified against live Rust code that
  already queries the new names exclusively.

payments/invoices were NOT chain bugs -- confirmed clean on the fresh
bootstrap test -- so no fix needed for those; they only conflicted on
this one DB because of its scripts/init-db.sql legacy seed, which is
already a documented, known gap.

Verified twice: applied cleanly as no-ops against the now-fully-migrated
local dev DB, and applied successfully end-to-end (0 manual steps)
against a brand new empty database created from scratch.

Runbook updated with the full pending-migration list and today's
findings.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-14 18:30:42 +05:30

37 lines
2.1 KiB
SQL

-- 20260420000002_cleanup_role_tables.up.sql (the rename half relevant here)
-- was disabled as .up.sql.skip, so role_permissions/dashboard_configs/
-- runtime_configs/user_roles never get renamed to the names the live app
-- code (config.rs, external_roles.rs, user_roles.rs, ...) actually queries:
-- role_admin_permissions/role_sidebar_configs/role_runtime_configs/
-- user_role_assignments. 20260422000000_seed_widgets needs
-- role_sidebar_configs to exist, so any fresh bootstrap fails there.
--
-- Unlike the external-role-module tables migration, this can't just be
-- "IF NOT EXISTS" -- a RENAME's source table stops existing once it's
-- done, so re-running it errors on an environment where it already
-- happened (including this repo's own production, per the skip file's own
-- history). Each rename below only fires if the old name still exists and
-- the new name doesn't yet -- safe to run on any environment regardless of
-- which (if any) of these renames it already has.
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'role_permissions')
AND NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'role_admin_permissions') THEN
ALTER TABLE role_permissions RENAME TO role_admin_permissions;
END IF;
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'dashboard_configs')
AND NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'role_sidebar_configs') THEN
ALTER TABLE dashboard_configs RENAME TO role_sidebar_configs;
END IF;
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'runtime_configs')
AND NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'role_runtime_configs') THEN
ALTER TABLE runtime_configs RENAME TO role_runtime_configs;
END IF;
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'user_roles')
AND NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'user_role_assignments') THEN
ALTER TABLE user_roles RENAME TO user_role_assignments;
END IF;
END $$;