175 lines
6 KiB
TypeScript
175 lines
6 KiB
TypeScript
import { MigrateUpArgs, MigrateDownArgs, sql } from "@payloadcms/db-postgres";
|
|
|
|
/**
|
|
* Adds targeting, path matching, and enabled fields to the shims global.
|
|
* This is a diff migration — production already has the base shims_shims table
|
|
* with (name, summary, content, type). This adds the new columns, enum types,
|
|
* and junction tables without recreating anything that already exists.
|
|
*/
|
|
export async function up({ db }: MigrateUpArgs): Promise<void> {
|
|
// --- New enum types ---
|
|
|
|
await db.execute(sql`
|
|
DO $$ BEGIN
|
|
CREATE TYPE "public"."enum_shims_shims_targeting_audience"
|
|
AS ENUM('all', 'percentage', 'users', 'roles');
|
|
EXCEPTION WHEN duplicate_object THEN null;
|
|
END $$;
|
|
`);
|
|
|
|
await db.execute(sql`
|
|
DO $$ BEGIN
|
|
CREATE TYPE "public"."enum_shims_shims_targeting_roles"
|
|
AS ENUM('guest', 'user', 'trusted', 'admin', 'developer');
|
|
EXCEPTION WHEN duplicate_object THEN null;
|
|
END $$;
|
|
`);
|
|
|
|
await db.execute(sql`
|
|
DO $$ BEGIN
|
|
CREATE TYPE "public"."enum_shims_shims_paths_match_type"
|
|
AS ENUM('all', 'include', 'exclude');
|
|
EXCEPTION WHEN duplicate_object THEN null;
|
|
END $$;
|
|
`);
|
|
|
|
// --- New columns on shims_shims ---
|
|
|
|
await db.execute(sql`
|
|
ALTER TABLE "shims_shims"
|
|
ADD COLUMN IF NOT EXISTS "enabled" boolean DEFAULT false NOT NULL;
|
|
`);
|
|
|
|
await db.execute(sql`
|
|
ALTER TABLE "shims_shims"
|
|
ADD COLUMN IF NOT EXISTS "targeting_audience"
|
|
"enum_shims_shims_targeting_audience" DEFAULT 'all' NOT NULL;
|
|
`);
|
|
|
|
await db.execute(sql`
|
|
ALTER TABLE "shims_shims"
|
|
ADD COLUMN IF NOT EXISTS "targeting_percentage" numeric;
|
|
`);
|
|
|
|
await db.execute(sql`
|
|
ALTER TABLE "shims_shims"
|
|
ADD COLUMN IF NOT EXISTS "paths_match_type"
|
|
"enum_shims_shims_paths_match_type" DEFAULT 'all' NOT NULL;
|
|
`);
|
|
|
|
// --- Junction table: shims_shims_targeting_roles (hasMany select) ---
|
|
|
|
await db.execute(sql`
|
|
CREATE TABLE IF NOT EXISTS "shims_shims_targeting_roles" (
|
|
"order" integer NOT NULL,
|
|
"parent_id" varchar NOT NULL,
|
|
"value" "enum_shims_shims_targeting_roles",
|
|
"id" serial PRIMARY KEY NOT NULL
|
|
);
|
|
`);
|
|
|
|
await db.execute(sql`
|
|
DO $$ BEGIN
|
|
ALTER TABLE "shims_shims_targeting_roles"
|
|
ADD CONSTRAINT "shims_shims_targeting_roles_parent_id_fk"
|
|
FOREIGN KEY ("parent_id") REFERENCES "public"."shims_shims"("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_shims_targeting_roles_order_idx"
|
|
ON "shims_shims_targeting_roles" USING btree ("order");
|
|
CREATE INDEX IF NOT EXISTS "shims_shims_targeting_roles_parent_id_idx"
|
|
ON "shims_shims_targeting_roles" USING btree ("parent_id");
|
|
`);
|
|
|
|
// --- Junction table: shims_shims_targeting_users (hasMany relationship) ---
|
|
|
|
await db.execute(sql`
|
|
CREATE TABLE IF NOT EXISTS "shims_shims_targeting_users" (
|
|
"_order" integer NOT NULL,
|
|
"_parent_id" varchar NOT NULL,
|
|
"id" varchar PRIMARY KEY NOT NULL,
|
|
"value" integer NOT NULL
|
|
);
|
|
`);
|
|
|
|
await db.execute(sql`
|
|
DO $$ BEGIN
|
|
ALTER TABLE "shims_shims_targeting_users"
|
|
ADD CONSTRAINT "shims_shims_targeting_users_parent_id_fk"
|
|
FOREIGN KEY ("_parent_id") REFERENCES "public"."shims_shims"("id")
|
|
ON DELETE cascade ON UPDATE no action;
|
|
EXCEPTION WHEN duplicate_object THEN null;
|
|
END $$;
|
|
`);
|
|
|
|
await db.execute(sql`
|
|
DO $$ BEGIN
|
|
ALTER TABLE "shims_shims_targeting_users"
|
|
ADD CONSTRAINT "shims_shims_targeting_users_value_fk"
|
|
FOREIGN KEY ("value") REFERENCES "public"."users"("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 "shims_shims_targeting_users_order_idx"
|
|
ON "shims_shims_targeting_users" USING btree ("_order");
|
|
CREATE INDEX IF NOT EXISTS "shims_shims_targeting_users_parent_id_idx"
|
|
ON "shims_shims_targeting_users" USING btree ("_parent_id");
|
|
CREATE INDEX IF NOT EXISTS "shims_shims_targeting_users_value_idx"
|
|
ON "shims_shims_targeting_users" USING btree ("value");
|
|
`);
|
|
|
|
// --- Junction table: shims_shims_paths_paths (hasMany text) ---
|
|
|
|
await db.execute(sql`
|
|
CREATE TABLE IF NOT EXISTS "shims_shims_paths_paths" (
|
|
"_order" integer NOT NULL,
|
|
"_parent_id" varchar NOT NULL,
|
|
"id" varchar PRIMARY KEY NOT NULL,
|
|
"value" varchar
|
|
);
|
|
`);
|
|
|
|
await db.execute(sql`
|
|
DO $$ BEGIN
|
|
ALTER TABLE "shims_shims_paths_paths"
|
|
ADD CONSTRAINT "shims_shims_paths_paths_parent_id_fk"
|
|
FOREIGN KEY ("_parent_id") REFERENCES "public"."shims_shims"("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_shims_paths_paths_order_idx"
|
|
ON "shims_shims_paths_paths" USING btree ("_order");
|
|
CREATE INDEX IF NOT EXISTS "shims_shims_paths_paths_parent_id_idx"
|
|
ON "shims_shims_paths_paths" USING btree ("_parent_id");
|
|
`);
|
|
}
|
|
|
|
export async function down({ db }: MigrateDownArgs): Promise<void> {
|
|
// Drop junction tables (cascade removes FK rows)
|
|
await db.execute(sql`DROP TABLE IF EXISTS "shims_shims_paths_paths";`);
|
|
await db.execute(sql`DROP TABLE IF EXISTS "shims_shims_targeting_users";`);
|
|
await db.execute(sql`DROP TABLE IF EXISTS "shims_shims_targeting_roles";`);
|
|
|
|
// Drop columns
|
|
await db.execute(sql`
|
|
ALTER TABLE "shims_shims" DROP COLUMN IF EXISTS "enabled";
|
|
ALTER TABLE "shims_shims" DROP COLUMN IF EXISTS "targeting_audience";
|
|
ALTER TABLE "shims_shims" DROP COLUMN IF EXISTS "targeting_percentage";
|
|
ALTER TABLE "shims_shims" DROP COLUMN IF EXISTS "paths_match_type";
|
|
`);
|
|
|
|
// Drop enum types
|
|
await db.execute(sql`DROP TYPE IF EXISTS "public"."enum_shims_shims_paths_match_type";`);
|
|
await db.execute(sql`DROP TYPE IF EXISTS "public"."enum_shims_shims_targeting_roles";`);
|
|
await db.execute(sql`DROP TYPE IF EXISTS "public"."enum_shims_shims_targeting_audience";`);
|
|
}
|