import { MigrateUpArgs, MigrateDownArgs, sql } from '@payloadcms/db-postgres'

export async function up({ payload, req }: MigrateUpArgs): Promise<void> {
  await payload.db.drizzle.execute(sql`
   CREATE TYPE "public"."enum_worship_type" AS ENUM('MASS', 'FAMILY', 'WORD');
  CREATE TYPE "public"."enum_blog_blocks_text_width" AS ENUM('1/2', '3/4');
  CREATE TYPE "public"."enum_group_blocks_text_width" AS ENUM('1/2', '3/4');
  CREATE TYPE "public"."enum_testimony_category" AS ENUM('EUCHARIST');
  CREATE TYPE "public"."enum_users_roles" AS ENUM('user', 'employee', 'admin');
  CREATE TABLE IF NOT EXISTS "parish_contact_persons" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar NOT NULL,
  	"description" varchar NOT NULL
  );
  
  CREATE TABLE IF NOT EXISTS "parish_gallery" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"photo_id" uuid NOT NULL
  );
  
  CREATE TABLE IF NOT EXISTS "parish" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"name" varchar NOT NULL,
  	"slug" varchar NOT NULL,
  	"description" varchar NOT NULL,
  	"history" varchar NOT NULL,
  	"contact" varchar NOT NULL,
  	"photo_id" uuid NOT NULL,
  	"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 "parish_rels" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"order" integer,
  	"parent_id" uuid NOT NULL,
  	"path" varchar NOT NULL,
  	"church_id" uuid,
  	"employees_id" uuid
  );
  
  CREATE TABLE IF NOT EXISTS "church" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"name" varchar NOT NULL,
  	"address" 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
  );
  
  CREATE TABLE IF NOT EXISTS "worship" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"date" timestamp(3) with time zone NOT NULL,
  	"location_id" uuid NOT NULL,
  	"type" "enum_worship_type" NOT NULL,
  	"title" varchar,
  	"cancelled" boolean DEFAULT false NOT NULL,
  	"liturgical_day" varchar,
  	"celebrant" varchar,
  	"description" varchar,
  	"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 "announcement" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"date" timestamp(3) with time zone NOT NULL,
  	"document_id" uuid NOT NULL,
  	"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 "announcement_rels" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"order" integer,
  	"parent_id" uuid NOT NULL,
  	"path" varchar NOT NULL,
  	"parish_id" uuid
  );
  
  CREATE TABLE IF NOT EXISTS "blog_blocks_text" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"content" jsonb NOT NULL,
  	"content_html" varchar,
  	"width" "enum_blog_blocks_text_width" DEFAULT '1/2' NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "blog_blocks_document" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"file_id" uuid NOT NULL,
  	"button" varchar DEFAULT 'Download Flyer' NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "blog_blocks_contactform" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar DEFAULT 'Ich bin dabei!' NOT NULL,
  	"description" varchar DEFAULT 'Um dich anzumelden oder uns zu unterstützen, fülle bitte das Kontaktformular aus. Wir freuen uns sehr, dass du Teil unserer Gemeinschaft bist und mit deinem Engagement dazu beiträgst, unsere Ziele zu erreichen. Solltest du Fragen haben oder weitere Informationen benötigen, zögere nicht, uns zu kontaktieren – wir sind gerne für dich da!' NOT NULL,
  	"email" varchar DEFAULT 'kontakt@dreikoenige.berlin' NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "blog_blocks_gallery_items" (
  	"_order" integer NOT NULL,
  	"_parent_id" varchar NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"photo_id" uuid NOT NULL
  );
  
  CREATE TABLE IF NOT EXISTS "blog_blocks_gallery" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "blog" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"photo_id" uuid,
  	"title" varchar NOT NULL,
  	"excerpt" 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
  );
  
  CREATE TABLE IF NOT EXISTS "blog_rels" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"order" integer,
  	"parent_id" uuid NOT NULL,
  	"path" varchar NOT NULL,
  	"parish_id" uuid
  );
  
  CREATE TABLE IF NOT EXISTS "highlight" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"from" timestamp(3) with time zone NOT NULL,
  	"until" timestamp(3) with time zone NOT NULL,
  	"date" timestamp(3) with time zone NOT NULL,
  	"text" 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
  );
  
  CREATE TABLE IF NOT EXISTS "highlight_rels" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"order" integer,
  	"parent_id" uuid NOT NULL,
  	"path" varchar NOT NULL,
  	"event_id" uuid,
  	"blog_id" uuid,
  	"worship_id" uuid
  );
  
  CREATE TABLE IF NOT EXISTS "event" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"photo_id" uuid,
  	"title" varchar NOT NULL,
  	"date" timestamp(3) with time zone NOT NULL,
  	"location_id" uuid NOT NULL,
  	"contact_id" uuid,
  	"short_description" varchar NOT NULL,
  	"description" varchar NOT NULL,
  	"flyer_id" uuid,
  	"cancelled" boolean DEFAULT false NOT NULL,
  	"is_recurring" boolean DEFAULT false NOT NULL,
  	"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 "event_rels" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"order" integer,
  	"parent_id" uuid NOT NULL,
  	"path" varchar NOT NULL,
  	"parish_id" uuid,
  	"group_id" uuid
  );
  
  CREATE TABLE IF NOT EXISTS "contact_person" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"name" varchar NOT NULL,
  	"email" varchar,
  	"telephone" varchar,
  	"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 "locations" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"name" varchar NOT NULL,
  	"address" varchar,
  	"coordinates" geometry(Point),
  	"notes" varchar,
  	"barrier_free" boolean DEFAULT false,
  	"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 "group_blocks_text" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"content" jsonb NOT NULL,
  	"content_html" varchar,
  	"width" "enum_group_blocks_text_width" DEFAULT '1/2' NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "group_blocks_gallery_items" (
  	"_order" integer NOT NULL,
  	"_parent_id" varchar NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"photo_id" uuid NOT NULL
  );
  
  CREATE TABLE IF NOT EXISTS "group_blocks_gallery" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "group_blocks_document" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"file_id" uuid NOT NULL,
  	"button" varchar DEFAULT 'Download Flyer' NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "group_blocks_contactform" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar DEFAULT 'Ich bin dabei!' NOT NULL,
  	"description" varchar DEFAULT 'Um dich anzumelden oder uns zu unterstützen, fülle bitte das Kontaktformular aus. Wir freuen uns sehr, dass du Teil unserer Gemeinschaft bist und mit deinem Engagement dazu beiträgst, unsere Ziele zu erreichen. Solltest du Fragen haben oder weitere Informationen benötigen, zögere nicht, uns zu kontaktieren – wir sind gerne für dich da!' NOT NULL,
  	"email" varchar DEFAULT 'kontakt@dreikoenige.berlin' NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "group" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"photo_id" uuid,
  	"name" varchar NOT NULL,
  	"slug" varchar NOT NULL,
  	"short_description" varchar NOT NULL,
  	"text" jsonb,
  	"text_html" varchar,
  	"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 "employees" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"photo_id" uuid,
  	"name" varchar NOT NULL,
  	"occupation" varchar NOT NULL,
  	"email" varchar,
  	"telephone" varchar,
  	"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 "testimony" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"testimony" varchar NOT NULL,
  	"name" varchar NOT NULL,
  	"occupation" varchar,
  	"category" "enum_testimony_category" NOT NULL,
  	"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 "page_blocks_content" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"content" jsonb NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "page_blocks_title" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"title" varchar NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "page_blocks_testimony" (
  	"_order" integer NOT NULL,
  	"_parent_id" uuid NOT NULL,
  	"_path" text NOT NULL,
  	"id" varchar PRIMARY KEY NOT NULL,
  	"testimony_id" uuid NOT NULL,
  	"block_name" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "page" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"title" varchar NOT NULL,
  	"slug" 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
  );
  
  CREATE TABLE IF NOT EXISTS "users" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"name" varchar DEFAULT '' NOT NULL,
  	"roles" "enum_users_roles" DEFAULT 'user' 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 DEFAULT 0,
  	"lock_until" timestamp(3) with time zone
  );
  
  CREATE TABLE IF NOT EXISTS "users_rels" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"order" integer,
  	"parent_id" uuid NOT NULL,
  	"path" varchar NOT NULL,
  	"group_id" uuid
  );
  
  CREATE TABLE IF NOT EXISTS "documents" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"name" varchar NOT NULL,
  	"prefix" varchar DEFAULT 'documents/',
  	"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 "media" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"alt" varchar NOT NULL,
  	"search" varchar,
  	"prefix" varchar DEFAULT 'media/',
  	"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,
  	"sizes_thumbnail_url" varchar,
  	"sizes_thumbnail_width" numeric,
  	"sizes_thumbnail_height" numeric,
  	"sizes_thumbnail_mime_type" varchar,
  	"sizes_thumbnail_filesize" numeric,
  	"sizes_thumbnail_filename" varchar,
  	"sizes_banner_url" varchar,
  	"sizes_banner_width" numeric,
  	"sizes_banner_height" numeric,
  	"sizes_banner_mime_type" varchar,
  	"sizes_banner_filesize" numeric,
  	"sizes_banner_filename" varchar,
  	"sizes_gallery_url" varchar,
  	"sizes_gallery_width" numeric,
  	"sizes_gallery_height" numeric,
  	"sizes_gallery_mime_type" varchar,
  	"sizes_gallery_filesize" numeric,
  	"sizes_gallery_filename" varchar,
  	"sizes_tablet_url" varchar,
  	"sizes_tablet_width" numeric,
  	"sizes_tablet_height" numeric,
  	"sizes_tablet_mime_type" varchar,
  	"sizes_tablet_filesize" numeric,
  	"sizes_tablet_filename" varchar
  );
  
  CREATE TABLE IF NOT EXISTS "payload_locked_documents" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  	"global_slug" varchar,
  	"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_locked_documents_rels" (
  	"id" serial PRIMARY KEY NOT NULL,
  	"order" integer,
  	"parent_id" uuid NOT NULL,
  	"path" varchar NOT NULL,
  	"parish_id" uuid,
  	"church_id" uuid,
  	"worship_id" uuid,
  	"announcement_id" uuid,
  	"blog_id" uuid,
  	"highlight_id" uuid,
  	"event_id" uuid,
  	"contact_person_id" uuid,
  	"locations_id" uuid,
  	"group_id" uuid,
  	"employees_id" uuid,
  	"testimony_id" uuid,
  	"page_id" uuid,
  	"users_id" uuid,
  	"documents_id" uuid,
  	"media_id" uuid
  );
  
  CREATE TABLE IF NOT EXISTS "payload_preferences" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() 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" uuid NOT NULL,
  	"path" varchar NOT NULL,
  	"users_id" uuid
  );
  
  CREATE TABLE IF NOT EXISTS "payload_migrations" (
  	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() 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
  );
  
  DO $$ BEGIN
   ALTER TABLE "parish_contact_persons" ADD CONSTRAINT "parish_contact_persons_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "parish_gallery" ADD CONSTRAINT "parish_gallery_photo_id_media_id_fk" FOREIGN KEY ("photo_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "parish_gallery" ADD CONSTRAINT "parish_gallery_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "parish" ADD CONSTRAINT "parish_photo_id_media_id_fk" FOREIGN KEY ("photo_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "parish_rels" ADD CONSTRAINT "parish_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "parish_rels" ADD CONSTRAINT "parish_rels_church_fk" FOREIGN KEY ("church_id") REFERENCES "public"."church"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "parish_rels" ADD CONSTRAINT "parish_rels_employees_fk" FOREIGN KEY ("employees_id") REFERENCES "public"."employees"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "worship" ADD CONSTRAINT "worship_location_id_church_id_fk" FOREIGN KEY ("location_id") REFERENCES "public"."church"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "announcement" ADD CONSTRAINT "announcement_document_id_documents_id_fk" FOREIGN KEY ("document_id") REFERENCES "public"."documents"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "announcement_rels" ADD CONSTRAINT "announcement_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."announcement"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "announcement_rels" ADD CONSTRAINT "announcement_rels_parish_fk" FOREIGN KEY ("parish_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "blog_blocks_text" ADD CONSTRAINT "blog_blocks_text_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."blog"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "blog_blocks_document" ADD CONSTRAINT "blog_blocks_document_file_id_documents_id_fk" FOREIGN KEY ("file_id") REFERENCES "public"."documents"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "blog_blocks_document" ADD CONSTRAINT "blog_blocks_document_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."blog"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "blog_blocks_contactform" ADD CONSTRAINT "blog_blocks_contactform_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."blog"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "blog_blocks_gallery_items" ADD CONSTRAINT "blog_blocks_gallery_items_photo_id_media_id_fk" FOREIGN KEY ("photo_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "blog_blocks_gallery_items" ADD CONSTRAINT "blog_blocks_gallery_items_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."blog_blocks_gallery"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "blog_blocks_gallery" ADD CONSTRAINT "blog_blocks_gallery_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."blog"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "blog" ADD CONSTRAINT "blog_photo_id_media_id_fk" FOREIGN KEY ("photo_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "blog_rels" ADD CONSTRAINT "blog_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."blog"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "blog_rels" ADD CONSTRAINT "blog_rels_parish_fk" FOREIGN KEY ("parish_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "highlight_rels" ADD CONSTRAINT "highlight_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."highlight"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "highlight_rels" ADD CONSTRAINT "highlight_rels_event_fk" FOREIGN KEY ("event_id") REFERENCES "public"."event"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "highlight_rels" ADD CONSTRAINT "highlight_rels_blog_fk" FOREIGN KEY ("blog_id") REFERENCES "public"."blog"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "highlight_rels" ADD CONSTRAINT "highlight_rels_worship_fk" FOREIGN KEY ("worship_id") REFERENCES "public"."worship"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "event" ADD CONSTRAINT "event_photo_id_media_id_fk" FOREIGN KEY ("photo_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "event" ADD CONSTRAINT "event_location_id_locations_id_fk" FOREIGN KEY ("location_id") REFERENCES "public"."locations"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "event" ADD CONSTRAINT "event_contact_id_contact_person_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contact_person"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "event" ADD CONSTRAINT "event_flyer_id_documents_id_fk" FOREIGN KEY ("flyer_id") REFERENCES "public"."documents"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "event_rels" ADD CONSTRAINT "event_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."event"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "event_rels" ADD CONSTRAINT "event_rels_parish_fk" FOREIGN KEY ("parish_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "event_rels" ADD CONSTRAINT "event_rels_group_fk" FOREIGN KEY ("group_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "group_blocks_text" ADD CONSTRAINT "group_blocks_text_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "group_blocks_gallery_items" ADD CONSTRAINT "group_blocks_gallery_items_photo_id_media_id_fk" FOREIGN KEY ("photo_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "group_blocks_gallery_items" ADD CONSTRAINT "group_blocks_gallery_items_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."group_blocks_gallery"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "group_blocks_gallery" ADD CONSTRAINT "group_blocks_gallery_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "group_blocks_document" ADD CONSTRAINT "group_blocks_document_file_id_documents_id_fk" FOREIGN KEY ("file_id") REFERENCES "public"."documents"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "group_blocks_document" ADD CONSTRAINT "group_blocks_document_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "group_blocks_contactform" ADD CONSTRAINT "group_blocks_contactform_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "group" ADD CONSTRAINT "group_photo_id_media_id_fk" FOREIGN KEY ("photo_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "employees" ADD CONSTRAINT "employees_photo_id_media_id_fk" FOREIGN KEY ("photo_id") REFERENCES "public"."media"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "page_blocks_content" ADD CONSTRAINT "page_blocks_content_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."page"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "page_blocks_title" ADD CONSTRAINT "page_blocks_title_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."page"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "page_blocks_testimony" ADD CONSTRAINT "page_blocks_testimony_testimony_id_testimony_id_fk" FOREIGN KEY ("testimony_id") REFERENCES "public"."testimony"("id") ON DELETE set null ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "page_blocks_testimony" ADD CONSTRAINT "page_blocks_testimony_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."page"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "users_rels" ADD CONSTRAINT "users_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "users_rels" ADD CONSTRAINT "users_rels_group_fk" FOREIGN KEY ("group_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."payload_locked_documents"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_parish_fk" FOREIGN KEY ("parish_id") REFERENCES "public"."parish"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_church_fk" FOREIGN KEY ("church_id") REFERENCES "public"."church"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_worship_fk" FOREIGN KEY ("worship_id") REFERENCES "public"."worship"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_announcement_fk" FOREIGN KEY ("announcement_id") REFERENCES "public"."announcement"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_blog_fk" FOREIGN KEY ("blog_id") REFERENCES "public"."blog"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_highlight_fk" FOREIGN KEY ("highlight_id") REFERENCES "public"."highlight"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_event_fk" FOREIGN KEY ("event_id") REFERENCES "public"."event"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_contact_person_fk" FOREIGN KEY ("contact_person_id") REFERENCES "public"."contact_person"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_locations_fk" FOREIGN KEY ("locations_id") REFERENCES "public"."locations"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_group_fk" FOREIGN KEY ("group_id") REFERENCES "public"."group"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_employees_fk" FOREIGN KEY ("employees_id") REFERENCES "public"."employees"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_testimony_fk" FOREIGN KEY ("testimony_id") REFERENCES "public"."testimony"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_page_fk" FOREIGN KEY ("page_id") REFERENCES "public"."page"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_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 $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_documents_fk" FOREIGN KEY ("documents_id") REFERENCES "public"."documents"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  DO $$ BEGIN
   ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_media_fk" FOREIGN KEY ("media_id") REFERENCES "public"."media"("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_parent_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."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 "public"."users"("id") ON DELETE cascade ON UPDATE no action;
  EXCEPTION
   WHEN duplicate_object THEN null;
  END $$;
  
  CREATE INDEX IF NOT EXISTS "parish_contact_persons_order_idx" ON "parish_contact_persons" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "parish_contact_persons_parent_id_idx" ON "parish_contact_persons" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "parish_gallery_order_idx" ON "parish_gallery" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "parish_gallery_parent_id_idx" ON "parish_gallery" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "parish_gallery_photo_idx" ON "parish_gallery" USING btree ("photo_id");
  CREATE INDEX IF NOT EXISTS "parish_photo_idx" ON "parish" USING btree ("photo_id");
  CREATE INDEX IF NOT EXISTS "parish_updated_at_idx" ON "parish" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "parish_created_at_idx" ON "parish" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "parish_rels_order_idx" ON "parish_rels" USING btree ("order");
  CREATE INDEX IF NOT EXISTS "parish_rels_parent_idx" ON "parish_rels" USING btree ("parent_id");
  CREATE INDEX IF NOT EXISTS "parish_rels_path_idx" ON "parish_rels" USING btree ("path");
  CREATE INDEX IF NOT EXISTS "parish_rels_church_id_idx" ON "parish_rels" USING btree ("church_id");
  CREATE INDEX IF NOT EXISTS "parish_rels_employees_id_idx" ON "parish_rels" USING btree ("employees_id");
  CREATE INDEX IF NOT EXISTS "church_updated_at_idx" ON "church" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "church_created_at_idx" ON "church" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "worship_location_idx" ON "worship" USING btree ("location_id");
  CREATE INDEX IF NOT EXISTS "worship_updated_at_idx" ON "worship" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "worship_created_at_idx" ON "worship" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "announcement_document_idx" ON "announcement" USING btree ("document_id");
  CREATE INDEX IF NOT EXISTS "announcement_updated_at_idx" ON "announcement" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "announcement_created_at_idx" ON "announcement" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "announcement_rels_order_idx" ON "announcement_rels" USING btree ("order");
  CREATE INDEX IF NOT EXISTS "announcement_rels_parent_idx" ON "announcement_rels" USING btree ("parent_id");
  CREATE INDEX IF NOT EXISTS "announcement_rels_path_idx" ON "announcement_rels" USING btree ("path");
  CREATE INDEX IF NOT EXISTS "announcement_rels_parish_id_idx" ON "announcement_rels" USING btree ("parish_id");
  CREATE INDEX IF NOT EXISTS "blog_blocks_text_order_idx" ON "blog_blocks_text" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "blog_blocks_text_parent_id_idx" ON "blog_blocks_text" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "blog_blocks_text_path_idx" ON "blog_blocks_text" USING btree ("_path");
  CREATE INDEX IF NOT EXISTS "blog_blocks_document_order_idx" ON "blog_blocks_document" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "blog_blocks_document_parent_id_idx" ON "blog_blocks_document" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "blog_blocks_document_path_idx" ON "blog_blocks_document" USING btree ("_path");
  CREATE INDEX IF NOT EXISTS "blog_blocks_document_file_idx" ON "blog_blocks_document" USING btree ("file_id");
  CREATE INDEX IF NOT EXISTS "blog_blocks_contactform_order_idx" ON "blog_blocks_contactform" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "blog_blocks_contactform_parent_id_idx" ON "blog_blocks_contactform" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "blog_blocks_contactform_path_idx" ON "blog_blocks_contactform" USING btree ("_path");
  CREATE INDEX IF NOT EXISTS "blog_blocks_gallery_items_order_idx" ON "blog_blocks_gallery_items" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "blog_blocks_gallery_items_parent_id_idx" ON "blog_blocks_gallery_items" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "blog_blocks_gallery_items_photo_idx" ON "blog_blocks_gallery_items" USING btree ("photo_id");
  CREATE INDEX IF NOT EXISTS "blog_blocks_gallery_order_idx" ON "blog_blocks_gallery" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "blog_blocks_gallery_parent_id_idx" ON "blog_blocks_gallery" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "blog_blocks_gallery_path_idx" ON "blog_blocks_gallery" USING btree ("_path");
  CREATE INDEX IF NOT EXISTS "blog_photo_idx" ON "blog" USING btree ("photo_id");
  CREATE INDEX IF NOT EXISTS "blog_updated_at_idx" ON "blog" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "blog_created_at_idx" ON "blog" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "blog_rels_order_idx" ON "blog_rels" USING btree ("order");
  CREATE INDEX IF NOT EXISTS "blog_rels_parent_idx" ON "blog_rels" USING btree ("parent_id");
  CREATE INDEX IF NOT EXISTS "blog_rels_path_idx" ON "blog_rels" USING btree ("path");
  CREATE INDEX IF NOT EXISTS "blog_rels_parish_id_idx" ON "blog_rels" USING btree ("parish_id");
  CREATE INDEX IF NOT EXISTS "highlight_updated_at_idx" ON "highlight" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "highlight_created_at_idx" ON "highlight" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "highlight_rels_order_idx" ON "highlight_rels" USING btree ("order");
  CREATE INDEX IF NOT EXISTS "highlight_rels_parent_idx" ON "highlight_rels" USING btree ("parent_id");
  CREATE INDEX IF NOT EXISTS "highlight_rels_path_idx" ON "highlight_rels" USING btree ("path");
  CREATE INDEX IF NOT EXISTS "highlight_rels_event_id_idx" ON "highlight_rels" USING btree ("event_id");
  CREATE INDEX IF NOT EXISTS "highlight_rels_blog_id_idx" ON "highlight_rels" USING btree ("blog_id");
  CREATE INDEX IF NOT EXISTS "highlight_rels_worship_id_idx" ON "highlight_rels" USING btree ("worship_id");
  CREATE INDEX IF NOT EXISTS "event_photo_idx" ON "event" USING btree ("photo_id");
  CREATE INDEX IF NOT EXISTS "event_location_idx" ON "event" USING btree ("location_id");
  CREATE INDEX IF NOT EXISTS "event_contact_idx" ON "event" USING btree ("contact_id");
  CREATE INDEX IF NOT EXISTS "event_flyer_idx" ON "event" USING btree ("flyer_id");
  CREATE INDEX IF NOT EXISTS "event_updated_at_idx" ON "event" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "event_created_at_idx" ON "event" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "event_rels_order_idx" ON "event_rels" USING btree ("order");
  CREATE INDEX IF NOT EXISTS "event_rels_parent_idx" ON "event_rels" USING btree ("parent_id");
  CREATE INDEX IF NOT EXISTS "event_rels_path_idx" ON "event_rels" USING btree ("path");
  CREATE INDEX IF NOT EXISTS "event_rels_parish_id_idx" ON "event_rels" USING btree ("parish_id");
  CREATE INDEX IF NOT EXISTS "event_rels_group_id_idx" ON "event_rels" USING btree ("group_id");
  CREATE INDEX IF NOT EXISTS "contact_person_updated_at_idx" ON "contact_person" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "contact_person_created_at_idx" ON "contact_person" USING btree ("created_at");
  CREATE UNIQUE INDEX IF NOT EXISTS "locations_name_idx" ON "locations" USING btree ("name");
  CREATE INDEX IF NOT EXISTS "locations_updated_at_idx" ON "locations" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "locations_created_at_idx" ON "locations" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "group_blocks_text_order_idx" ON "group_blocks_text" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "group_blocks_text_parent_id_idx" ON "group_blocks_text" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "group_blocks_text_path_idx" ON "group_blocks_text" USING btree ("_path");
  CREATE INDEX IF NOT EXISTS "group_blocks_gallery_items_order_idx" ON "group_blocks_gallery_items" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "group_blocks_gallery_items_parent_id_idx" ON "group_blocks_gallery_items" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "group_blocks_gallery_items_photo_idx" ON "group_blocks_gallery_items" USING btree ("photo_id");
  CREATE INDEX IF NOT EXISTS "group_blocks_gallery_order_idx" ON "group_blocks_gallery" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "group_blocks_gallery_parent_id_idx" ON "group_blocks_gallery" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "group_blocks_gallery_path_idx" ON "group_blocks_gallery" USING btree ("_path");
  CREATE INDEX IF NOT EXISTS "group_blocks_document_order_idx" ON "group_blocks_document" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "group_blocks_document_parent_id_idx" ON "group_blocks_document" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "group_blocks_document_path_idx" ON "group_blocks_document" USING btree ("_path");
  CREATE INDEX IF NOT EXISTS "group_blocks_document_file_idx" ON "group_blocks_document" USING btree ("file_id");
  CREATE INDEX IF NOT EXISTS "group_blocks_contactform_order_idx" ON "group_blocks_contactform" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "group_blocks_contactform_parent_id_idx" ON "group_blocks_contactform" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "group_blocks_contactform_path_idx" ON "group_blocks_contactform" USING btree ("_path");
  CREATE INDEX IF NOT EXISTS "group_photo_idx" ON "group" USING btree ("photo_id");
  CREATE UNIQUE INDEX IF NOT EXISTS "group_slug_idx" ON "group" USING btree ("slug");
  CREATE INDEX IF NOT EXISTS "group_updated_at_idx" ON "group" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "group_created_at_idx" ON "group" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "employees_photo_idx" ON "employees" USING btree ("photo_id");
  CREATE INDEX IF NOT EXISTS "employees_updated_at_idx" ON "employees" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "employees_created_at_idx" ON "employees" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "testimony_updated_at_idx" ON "testimony" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "testimony_created_at_idx" ON "testimony" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "page_blocks_content_order_idx" ON "page_blocks_content" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "page_blocks_content_parent_id_idx" ON "page_blocks_content" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "page_blocks_content_path_idx" ON "page_blocks_content" USING btree ("_path");
  CREATE INDEX IF NOT EXISTS "page_blocks_title_order_idx" ON "page_blocks_title" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "page_blocks_title_parent_id_idx" ON "page_blocks_title" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "page_blocks_title_path_idx" ON "page_blocks_title" USING btree ("_path");
  CREATE INDEX IF NOT EXISTS "page_blocks_testimony_order_idx" ON "page_blocks_testimony" USING btree ("_order");
  CREATE INDEX IF NOT EXISTS "page_blocks_testimony_parent_id_idx" ON "page_blocks_testimony" USING btree ("_parent_id");
  CREATE INDEX IF NOT EXISTS "page_blocks_testimony_path_idx" ON "page_blocks_testimony" USING btree ("_path");
  CREATE INDEX IF NOT EXISTS "page_blocks_testimony_testimony_idx" ON "page_blocks_testimony" USING btree ("testimony_id");
  CREATE INDEX IF NOT EXISTS "page_updated_at_idx" ON "page" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "page_created_at_idx" ON "page" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "users_updated_at_idx" ON "users" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "users_created_at_idx" ON "users" USING btree ("created_at");
  CREATE UNIQUE INDEX IF NOT EXISTS "users_email_idx" ON "users" USING btree ("email");
  CREATE INDEX IF NOT EXISTS "users_rels_order_idx" ON "users_rels" USING btree ("order");
  CREATE INDEX IF NOT EXISTS "users_rels_parent_idx" ON "users_rels" USING btree ("parent_id");
  CREATE INDEX IF NOT EXISTS "users_rels_path_idx" ON "users_rels" USING btree ("path");
  CREATE INDEX IF NOT EXISTS "users_rels_group_id_idx" ON "users_rels" USING btree ("group_id");
  CREATE INDEX IF NOT EXISTS "documents_updated_at_idx" ON "documents" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "documents_created_at_idx" ON "documents" USING btree ("created_at");
  CREATE UNIQUE INDEX IF NOT EXISTS "documents_filename_idx" ON "documents" USING btree ("filename");
  CREATE INDEX IF NOT EXISTS "media_updated_at_idx" ON "media" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "media_created_at_idx" ON "media" USING btree ("created_at");
  CREATE UNIQUE INDEX IF NOT EXISTS "media_filename_idx" ON "media" USING btree ("filename");
  CREATE INDEX IF NOT EXISTS "media_sizes_thumbnail_sizes_thumbnail_filename_idx" ON "media" USING btree ("sizes_thumbnail_filename");
  CREATE INDEX IF NOT EXISTS "media_sizes_banner_sizes_banner_filename_idx" ON "media" USING btree ("sizes_banner_filename");
  CREATE INDEX IF NOT EXISTS "media_sizes_gallery_sizes_gallery_filename_idx" ON "media" USING btree ("sizes_gallery_filename");
  CREATE INDEX IF NOT EXISTS "media_sizes_tablet_sizes_tablet_filename_idx" ON "media" USING btree ("sizes_tablet_filename");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_global_slug_idx" ON "payload_locked_documents" USING btree ("global_slug");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_updated_at_idx" ON "payload_locked_documents" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_created_at_idx" ON "payload_locked_documents" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_order_idx" ON "payload_locked_documents_rels" USING btree ("order");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_parent_idx" ON "payload_locked_documents_rels" USING btree ("parent_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_path_idx" ON "payload_locked_documents_rels" USING btree ("path");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_parish_id_idx" ON "payload_locked_documents_rels" USING btree ("parish_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_church_id_idx" ON "payload_locked_documents_rels" USING btree ("church_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_worship_id_idx" ON "payload_locked_documents_rels" USING btree ("worship_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_announcement_id_idx" ON "payload_locked_documents_rels" USING btree ("announcement_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_blog_id_idx" ON "payload_locked_documents_rels" USING btree ("blog_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_highlight_id_idx" ON "payload_locked_documents_rels" USING btree ("highlight_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_event_id_idx" ON "payload_locked_documents_rels" USING btree ("event_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_contact_person_id_idx" ON "payload_locked_documents_rels" USING btree ("contact_person_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_locations_id_idx" ON "payload_locked_documents_rels" USING btree ("locations_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_group_id_idx" ON "payload_locked_documents_rels" USING btree ("group_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_employees_id_idx" ON "payload_locked_documents_rels" USING btree ("employees_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_testimony_id_idx" ON "payload_locked_documents_rels" USING btree ("testimony_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_page_id_idx" ON "payload_locked_documents_rels" USING btree ("page_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_users_id_idx" ON "payload_locked_documents_rels" USING btree ("users_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_documents_id_idx" ON "payload_locked_documents_rels" USING btree ("documents_id");
  CREATE INDEX IF NOT EXISTS "payload_locked_documents_rels_media_id_idx" ON "payload_locked_documents_rels" USING btree ("media_id");
  CREATE INDEX IF NOT EXISTS "payload_preferences_key_idx" ON "payload_preferences" USING btree ("key");
  CREATE INDEX IF NOT EXISTS "payload_preferences_updated_at_idx" ON "payload_preferences" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "payload_preferences_created_at_idx" ON "payload_preferences" USING btree ("created_at");
  CREATE INDEX IF NOT EXISTS "payload_preferences_rels_order_idx" ON "payload_preferences_rels" USING btree ("order");
  CREATE INDEX IF NOT EXISTS "payload_preferences_rels_parent_idx" ON "payload_preferences_rels" USING btree ("parent_id");
  CREATE INDEX IF NOT EXISTS "payload_preferences_rels_path_idx" ON "payload_preferences_rels" USING btree ("path");
  CREATE INDEX IF NOT EXISTS "payload_preferences_rels_users_id_idx" ON "payload_preferences_rels" USING btree ("users_id");
  CREATE INDEX IF NOT EXISTS "payload_migrations_updated_at_idx" ON "payload_migrations" USING btree ("updated_at");
  CREATE INDEX IF NOT EXISTS "payload_migrations_created_at_idx" ON "payload_migrations" USING btree ("created_at");`)
}

export async function down({ payload, req }: MigrateDownArgs): Promise<void> {
  await payload.db.drizzle.execute(sql`
   DROP TABLE "parish_contact_persons" CASCADE;
  DROP TABLE "parish_gallery" CASCADE;
  DROP TABLE "parish" CASCADE;
  DROP TABLE "parish_rels" CASCADE;
  DROP TABLE "church" CASCADE;
  DROP TABLE "worship" CASCADE;
  DROP TABLE "announcement" CASCADE;
  DROP TABLE "announcement_rels" CASCADE;
  DROP TABLE "blog_blocks_text" CASCADE;
  DROP TABLE "blog_blocks_document" CASCADE;
  DROP TABLE "blog_blocks_contactform" CASCADE;
  DROP TABLE "blog_blocks_gallery_items" CASCADE;
  DROP TABLE "blog_blocks_gallery" CASCADE;
  DROP TABLE "blog" CASCADE;
  DROP TABLE "blog_rels" CASCADE;
  DROP TABLE "highlight" CASCADE;
  DROP TABLE "highlight_rels" CASCADE;
  DROP TABLE "event" CASCADE;
  DROP TABLE "event_rels" CASCADE;
  DROP TABLE "contact_person" CASCADE;
  DROP TABLE "locations" CASCADE;
  DROP TABLE "group_blocks_text" CASCADE;
  DROP TABLE "group_blocks_gallery_items" CASCADE;
  DROP TABLE "group_blocks_gallery" CASCADE;
  DROP TABLE "group_blocks_document" CASCADE;
  DROP TABLE "group_blocks_contactform" CASCADE;
  DROP TABLE "group" CASCADE;
  DROP TABLE "employees" CASCADE;
  DROP TABLE "testimony" CASCADE;
  DROP TABLE "page_blocks_content" CASCADE;
  DROP TABLE "page_blocks_title" CASCADE;
  DROP TABLE "page_blocks_testimony" CASCADE;
  DROP TABLE "page" CASCADE;
  DROP TABLE "users" CASCADE;
  DROP TABLE "users_rels" CASCADE;
  DROP TABLE "documents" CASCADE;
  DROP TABLE "media" CASCADE;
  DROP TABLE "payload_locked_documents" CASCADE;
  DROP TABLE "payload_locked_documents_rels" CASCADE;
  DROP TABLE "payload_preferences" CASCADE;
  DROP TABLE "payload_preferences_rels" CASCADE;
  DROP TABLE "payload_migrations" CASCADE;
  DROP TYPE "public"."enum_worship_type";
  DROP TYPE "public"."enum_blog_blocks_text_width";
  DROP TYPE "public"."enum_group_blocks_text_width";
  DROP TYPE "public"."enum_testimony_category";
  DROP TYPE "public"."enum_users_roles";`)
}
