fix(migrations): migrate profile awards to array storage
This commit is contained in:
parent
e1014207f4
commit
1dfbb58dae
2 changed files with 128 additions and 0 deletions
122
src/migrations/20260821_120000.ts
Normal file
122
src/migrations/20260821_120000.ts
Normal file
|
|
@ -0,0 +1,122 @@
|
||||||
|
import { MigrateDownArgs, MigrateUpArgs, sql } from "@payloadcms/db-postgres";
|
||||||
|
|
||||||
|
const awardsTable = "profiles_progression_awards";
|
||||||
|
|
||||||
|
export async function up({ db }: MigrateUpArgs): Promise<void> {
|
||||||
|
await db.execute(sql`
|
||||||
|
CREATE TABLE IF NOT EXISTS "${sql.raw(awardsTable)}" (
|
||||||
|
"_order" integer NOT NULL,
|
||||||
|
"_parent_id" integer NOT NULL,
|
||||||
|
"id" varchar PRIMARY KEY NOT NULL,
|
||||||
|
"award_id" integer NOT NULL,
|
||||||
|
"awarded_by_id" integer,
|
||||||
|
"awarded_at" timestamp(3) with time zone NOT NULL,
|
||||||
|
"award_reason" varchar
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
|
||||||
|
await db.execute(sql`
|
||||||
|
DO $$ BEGIN
|
||||||
|
ALTER TABLE "${sql.raw(awardsTable)}"
|
||||||
|
ADD CONSTRAINT "profiles_progression_awards_parent_id_fk"
|
||||||
|
FOREIGN KEY ("_parent_id") REFERENCES "public"."profiles"("id")
|
||||||
|
ON DELETE cascade ON UPDATE no action;
|
||||||
|
EXCEPTION WHEN duplicate_object THEN null;
|
||||||
|
END $$;
|
||||||
|
`);
|
||||||
|
|
||||||
|
await db.execute(sql`
|
||||||
|
DO $$ BEGIN
|
||||||
|
ALTER TABLE "${sql.raw(awardsTable)}"
|
||||||
|
ADD CONSTRAINT "profiles_progression_awards_award_id_fk"
|
||||||
|
FOREIGN KEY ("award_id") REFERENCES "public"."awards"("id")
|
||||||
|
ON DELETE set null ON UPDATE no action;
|
||||||
|
EXCEPTION WHEN duplicate_object THEN null;
|
||||||
|
END $$;
|
||||||
|
`);
|
||||||
|
|
||||||
|
await db.execute(sql`
|
||||||
|
DO $$ BEGIN
|
||||||
|
ALTER TABLE "${sql.raw(awardsTable)}"
|
||||||
|
ADD CONSTRAINT "profiles_progression_awards_awarded_by_id_fk"
|
||||||
|
FOREIGN KEY ("awarded_by_id") REFERENCES "public"."profiles"("id")
|
||||||
|
ON DELETE set null ON UPDATE no action;
|
||||||
|
EXCEPTION WHEN duplicate_object THEN null;
|
||||||
|
END $$;
|
||||||
|
`);
|
||||||
|
|
||||||
|
await db.execute(sql`
|
||||||
|
CREATE INDEX IF NOT EXISTS "profiles_progression_awards_order_idx"
|
||||||
|
ON "${sql.raw(awardsTable)}" USING btree ("_order");
|
||||||
|
CREATE INDEX IF NOT EXISTS "profiles_progression_awards_parent_id_idx"
|
||||||
|
ON "${sql.raw(awardsTable)}" USING btree ("_parent_id");
|
||||||
|
CREATE INDEX IF NOT EXISTS "profiles_progression_awards_award_id_idx"
|
||||||
|
ON "${sql.raw(awardsTable)}" USING btree ("award_id");
|
||||||
|
CREATE INDEX IF NOT EXISTS "profiles_progression_awards_awarded_by_id_idx"
|
||||||
|
ON "${sql.raw(awardsTable)}" USING btree ("awarded_by_id");
|
||||||
|
`);
|
||||||
|
|
||||||
|
// The old relationship did not record award metadata. Preserve its award,
|
||||||
|
// profile, and ordering; use the profile creation date as the best available
|
||||||
|
// timestamp for these historical entries.
|
||||||
|
await db.execute(sql`
|
||||||
|
INSERT INTO "${sql.raw(awardsTable)}"
|
||||||
|
("_order", "_parent_id", "id", "award_id", "awarded_at")
|
||||||
|
SELECT
|
||||||
|
ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY rels."parent_id"
|
||||||
|
ORDER BY rels."order" NULLS LAST, rels."id"
|
||||||
|
) - 1,
|
||||||
|
rels."parent_id",
|
||||||
|
md5('profile-award:' || rels."id"),
|
||||||
|
rels."awards_id",
|
||||||
|
profiles."created_at"
|
||||||
|
FROM "profiles_rels" rels
|
||||||
|
INNER JOIN "profiles" profiles ON profiles."id" = rels."parent_id"
|
||||||
|
WHERE rels."awards_id" IS NOT NULL
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM "${sql.raw(awardsTable)}" existing
|
||||||
|
WHERE existing."id" = md5('profile-award:' || rels."id")
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
|
||||||
|
await db.execute(sql`
|
||||||
|
ALTER TABLE "profiles_rels"
|
||||||
|
DROP CONSTRAINT IF EXISTS "profiles_rels_awards_fk";
|
||||||
|
DROP INDEX IF EXISTS "profiles_rels_awards_id_idx";
|
||||||
|
ALTER TABLE "profiles_rels" DROP COLUMN IF EXISTS "awards_id";
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down({ db }: MigrateDownArgs): Promise<void> {
|
||||||
|
await db.execute(sql`
|
||||||
|
ALTER TABLE "profiles_rels" ADD COLUMN IF NOT EXISTS "awards_id" integer;
|
||||||
|
`);
|
||||||
|
|
||||||
|
await db.execute(sql`
|
||||||
|
INSERT INTO "profiles_rels" ("order", "parent_id", "path", "awards_id")
|
||||||
|
SELECT awards."_order", awards."_parent_id", 'progression.awards', awards."award_id"
|
||||||
|
FROM "${sql.raw(awardsTable)}" awards
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM "profiles_rels" rels
|
||||||
|
WHERE rels."parent_id" = awards."_parent_id"
|
||||||
|
AND rels."awards_id" = awards."award_id"
|
||||||
|
AND rels."path" = 'progression.awards'
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
|
||||||
|
await db.execute(sql`
|
||||||
|
DO $$ BEGIN
|
||||||
|
ALTER TABLE "profiles_rels"
|
||||||
|
ADD CONSTRAINT "profiles_rels_awards_fk"
|
||||||
|
FOREIGN KEY ("awards_id") REFERENCES "public"."awards"("id")
|
||||||
|
ON DELETE cascade ON UPDATE no action;
|
||||||
|
EXCEPTION WHEN duplicate_object THEN null;
|
||||||
|
END $$;
|
||||||
|
CREATE INDEX IF NOT EXISTS "profiles_rels_awards_id_idx"
|
||||||
|
ON "profiles_rels" USING btree ("awards_id");
|
||||||
|
DROP TABLE IF EXISTS "${sql.raw(awardsTable)}";
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,7 @@ import * as migration_20260818_024510 from './_archived/20260818_024510';
|
||||||
import * as migration_20260820_004157 from './20260820_004157';
|
import * as migration_20260820_004157 from './20260820_004157';
|
||||||
import * as migration_20260821_043007 from './20260821_043007';
|
import * as migration_20260821_043007 from './20260821_043007';
|
||||||
import * as migration_20260821_060000 from './20260821_060000';
|
import * as migration_20260821_060000 from './20260821_060000';
|
||||||
|
import * as migration_20260821_120000 from './20260821_120000';
|
||||||
|
|
||||||
export const migrations = [
|
export const migrations = [
|
||||||
{
|
{
|
||||||
|
|
@ -19,4 +20,9 @@ export const migrations = [
|
||||||
down: migration_20260821_060000.down,
|
down: migration_20260821_060000.down,
|
||||||
name: '20260821_060000'
|
name: '20260821_060000'
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
up: migration_20260821_120000.up,
|
||||||
|
down: migration_20260821_120000.down,
|
||||||
|
name: '20260821_120000'
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue