94 lines
3.4 KiB
TypeScript
94 lines
3.4 KiB
TypeScript
import type { MigrateDownArgs, MigrateUpArgs } from '@payloadcms/db-postgres'
|
|
|
|
import { sql } from '@payloadcms/db-postgres'
|
|
|
|
export async function up({ payload }: MigrateUpArgs): Promise<void> {
|
|
await payload.db.drizzle.execute(sql`
|
|
|
|
CREATE TABLE IF NOT EXISTS "users" (
|
|
"id" serial PRIMARY KEY NOT NULL,
|
|
"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
|
|
"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
|
|
"email" varchar NOT NULL,
|
|
"reset_password_token" varchar,
|
|
"reset_password_expiration" timestamp(3) with time zone,
|
|
"salt" varchar,
|
|
"hash" varchar,
|
|
"login_attempts" numeric,
|
|
"lock_until" timestamp(3) with time zone
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS "media" (
|
|
"id" serial PRIMARY KEY NOT NULL,
|
|
"alt" varchar NOT NULL,
|
|
"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
|
|
"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
|
|
"url" varchar,
|
|
"thumbnail_u_r_l" varchar,
|
|
"filename" varchar,
|
|
"mime_type" varchar,
|
|
"filesize" numeric,
|
|
"width" numeric,
|
|
"height" numeric,
|
|
"focal_x" numeric,
|
|
"focal_y" numeric
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS "payload_preferences" (
|
|
"id" serial PRIMARY KEY NOT NULL,
|
|
"key" varchar,
|
|
"value" jsonb,
|
|
"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
|
|
"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS "payload_preferences_rels" (
|
|
"id" serial PRIMARY KEY NOT NULL,
|
|
"order" integer,
|
|
"parent_id" integer NOT NULL,
|
|
"path" varchar NOT NULL,
|
|
"users_id" integer
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS "payload_migrations" (
|
|
"id" serial PRIMARY KEY NOT NULL,
|
|
"name" varchar,
|
|
"batch" numeric,
|
|
"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
|
|
"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS "users_created_at_idx" ON "users" ("created_at");
|
|
CREATE UNIQUE INDEX IF NOT EXISTS "users_email_idx" ON "users" ("email");
|
|
CREATE INDEX IF NOT EXISTS "media_created_at_idx" ON "media" ("created_at");
|
|
CREATE UNIQUE INDEX IF NOT EXISTS "media_filename_idx" ON "media" ("filename");
|
|
CREATE INDEX IF NOT EXISTS "payload_preferences_key_idx" ON "payload_preferences" ("key");
|
|
CREATE INDEX IF NOT EXISTS "payload_preferences_created_at_idx" ON "payload_preferences" ("created_at");
|
|
CREATE INDEX IF NOT EXISTS "payload_preferences_rels_order_idx" ON "payload_preferences_rels" ("order");
|
|
CREATE INDEX IF NOT EXISTS "payload_preferences_rels_parent_idx" ON "payload_preferences_rels" ("parent_id");
|
|
CREATE INDEX IF NOT EXISTS "payload_preferences_rels_path_idx" ON "payload_preferences_rels" ("path");
|
|
CREATE INDEX IF NOT EXISTS "payload_migrations_created_at_idx" ON "payload_migrations" ("created_at");
|
|
DO $$ BEGIN
|
|
ALTER TABLE "payload_preferences_rels" ADD CONSTRAINT "payload_preferences_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "payload_preferences"("id") ON DELETE cascade ON UPDATE no action;
|
|
EXCEPTION
|
|
WHEN duplicate_object THEN null;
|
|
END $$;
|
|
|
|
DO $$ BEGIN
|
|
ALTER TABLE "payload_preferences_rels" ADD CONSTRAINT "payload_preferences_rels_users_fk" FOREIGN KEY ("users_id") REFERENCES "users"("id") ON DELETE cascade ON UPDATE no action;
|
|
EXCEPTION
|
|
WHEN duplicate_object THEN null;
|
|
END $$;
|
|
`)
|
|
}
|
|
|
|
export async function down({ payload }: MigrateDownArgs): Promise<void> {
|
|
await payload.db.drizzle.execute(sql`
|
|
|
|
DROP TABLE "users";
|
|
DROP TABLE "media";
|
|
DROP TABLE "payload_preferences";
|
|
DROP TABLE "payload_preferences_rels";
|
|
DROP TABLE "payload_migrations";`)
|
|
}
|