mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-18 22:33:05 +00:00
Drop the NOT NULL constraint on allocation_decision.rationale to align the DB schema with the Payload config (required: false since B134). Hand-written idempotent migration following the consent_text_to_jsonb precedent. Adds idempotency test in campaignMigrationReconciliation. Closes #371
45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
import { MigrateDownArgs, MigrateUpArgs, sql } from '@payloadcms/db-postgres'
|
|
|
|
/**
|
|
* Reconciles production schema drift: `allocation_decision.rationale` was created
|
|
* as `varchar NOT NULL` in the foundation migration, but the Payload config
|
|
* (`AllocationDecision.rationale.required`) was later set to `false` (B134).
|
|
* Without this reconciliation, every `pnpm migrate:create` re-emits
|
|
* `ALTER TABLE allocation_decision ALTER COLUMN rationale DROP NOT NULL`.
|
|
*
|
|
* This migration is idempotent — it only drops the constraint when the column
|
|
* is still `NOT NULL`, so it is a no-op on any database already reconciled.
|
|
*/
|
|
export async function up({ db }: MigrateUpArgs): Promise<void> {
|
|
await db.execute(sql`
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (
|
|
SELECT 1 FROM information_schema.columns
|
|
WHERE table_schema = 'public'
|
|
AND table_name = 'allocation_decision'
|
|
AND column_name = 'rationale'
|
|
AND is_nullable = 'NO'
|
|
) THEN
|
|
ALTER TABLE "allocation_decision" ALTER COLUMN "rationale" DROP NOT NULL;
|
|
END IF;
|
|
END $$;
|
|
`)
|
|
}
|
|
|
|
export async function down({ db }: MigrateDownArgs): Promise<void> {
|
|
await db.execute(sql`
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (
|
|
SELECT 1 FROM information_schema.columns
|
|
WHERE table_schema = 'public'
|
|
AND table_name = 'allocation_decision'
|
|
AND column_name = 'rationale'
|
|
AND is_nullable = 'YES'
|
|
) THEN
|
|
ALTER TABLE "allocation_decision" ALTER COLUMN "rationale" SET NOT NULL;
|
|
END IF;
|
|
END $$;
|
|
`)
|
|
}
|