1
0
Fork 0
polaris-task-force/src/migrations/20260826_050000_repair_shims_auxiliary_tables.ts

74 lines
2.4 KiB
TypeScript

import { MigrateDownArgs, MigrateUpArgs, sql } from "@payloadcms/db-postgres";
/**
* Repairs auxiliary tables required by Payload's current Shims global schema.
*
* The original Shims migrations created the global and its array/targeting
* tables, but the later relationship/text fields also require these two
* Payload-managed tables. Existing databases need this additive repair.
*/
export async function up({ db }: MigrateUpArgs): Promise<void> {
await db.execute(sql`
CREATE TABLE IF NOT EXISTS "shims_texts" (
"id" serial PRIMARY KEY NOT NULL,
"order" integer NOT NULL,
"parent_id" integer NOT NULL,
"path" varchar NOT NULL,
"text" varchar
);
CREATE TABLE IF NOT EXISTS "shims_rels" (
"id" serial PRIMARY KEY NOT NULL,
"order" integer,
"parent_id" integer NOT NULL,
"path" varchar NOT NULL,
"users_id" integer
);
`);
await db.execute(sql`
DO $$ BEGIN
ALTER TABLE "shims_texts"
ADD CONSTRAINT "shims_texts_parent_fk"
FOREIGN KEY ("parent_id") REFERENCES "public"."shims"("id")
ON DELETE cascade ON UPDATE no action;
EXCEPTION WHEN duplicate_object THEN null;
END $$;
DO $$ BEGIN
ALTER TABLE "shims_rels"
ADD CONSTRAINT "shims_rels_parent_fk"
FOREIGN KEY ("parent_id") REFERENCES "public"."shims"("id")
ON DELETE cascade ON UPDATE no action;
EXCEPTION WHEN duplicate_object THEN null;
END $$;
DO $$ BEGIN
ALTER TABLE "shims_rels"
ADD CONSTRAINT "shims_rels_users_fk"
FOREIGN KEY ("users_id") REFERENCES "public"."users"("id")
ON DELETE cascade ON UPDATE no action;
EXCEPTION WHEN duplicate_object THEN null;
END $$;
`);
await db.execute(sql`
CREATE INDEX IF NOT EXISTS "shims_texts_order_parent"
ON "shims_texts" USING btree ("order", "parent_id");
CREATE INDEX IF NOT EXISTS "shims_rels_order_idx"
ON "shims_rels" USING btree ("order");
CREATE INDEX IF NOT EXISTS "shims_rels_parent_idx"
ON "shims_rels" USING btree ("parent_id");
CREATE INDEX IF NOT EXISTS "shims_rels_path_idx"
ON "shims_rels" USING btree ("path");
CREATE INDEX IF NOT EXISTS "shims_rels_users_id_idx"
ON "shims_rels" USING btree ("users_id");
`);
}
export async function down({ db }: MigrateDownArgs): Promise<void> {
await db.execute(sql`
DROP TABLE IF EXISTS "shims_rels";
DROP TABLE IF EXISTS "shims_texts";
`);
}