ALTER TABLE "form_templates" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
ALTER TABLE "view_tag_subfields" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
DROP TABLE "form_templates" CASCADE;--> statement-breakpoint
DROP TABLE "view_tag_subfields" CASCADE;--> statement-breakpoint
ALTER TABLE "tag_subfields" DROP CONSTRAINT "uniq_tag_type_attribute";--> statement-breakpoint
ALTER TABLE "tag_classes" DROP CONSTRAINT "tag_classes_tag_type_code_tag_types_code_fk";
--> statement-breakpoint
ALTER TABLE "tag_subfield_options" DROP CONSTRAINT "tag_subfield_options_attributes_id_tag_subfields_attributes_id_fk";
--> statement-breakpoint
ALTER TABLE "tag_subfields" DROP CONSTRAINT "tag_subfields_tag_type_code_tag_types_code_fk";
--> statement-breakpoint
/* 
    Unfortunately in current drizzle-kit version we can't automatically get name for primary key.
    We are working on making it available!

    Meanwhile you can:
        1. Check pk name in your database, by running
            SELECT constraint_name FROM information_schema.table_constraints
            WHERE table_schema = 'public'
                AND table_name = 'tag_types'
                AND constraint_type = 'PRIMARY KEY';
        2. Uncomment code below and paste pk name manually
        
    Hope to release this update as soon as possible
*/

-- ALTER TABLE "tag_types" DROP CONSTRAINT "<constraint_name>";--> statement-breakpoint
ALTER TABLE "tag_types" ADD CONSTRAINT "tag_types_code_project_id_pk" PRIMARY KEY("code","project_id");--> statement-breakpoint
ALTER TABLE "form_metas" ADD COLUMN "project_id" integer NOT NULL;--> statement-breakpoint
ALTER TABLE "tag_classes" ADD COLUMN "project_id" integer NOT NULL;--> statement-breakpoint
ALTER TABLE "tag_subfield_options" ADD COLUMN "project_id" integer NOT NULL;--> statement-breakpoint
ALTER TABLE "tag_subfields" ADD COLUMN "project_id" integer NOT NULL;--> statement-breakpoint
ALTER TABLE "tag_type_class_form_mappings" ADD COLUMN "project_id" integer NOT NULL;--> statement-breakpoint
ALTER TABLE "tag_types" ADD COLUMN "project_id" integer NOT NULL;--> statement-breakpoint
ALTER TABLE "form_metas" ADD CONSTRAINT "form_metas_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tag_classes" ADD CONSTRAINT "tag_classes_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tag_classes" ADD CONSTRAINT "tag_classes_tag_type_code_project_id_tag_types_code_project_id_fk" FOREIGN KEY ("tag_type_code","project_id") REFERENCES "public"."tag_types"("code","project_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tag_subfield_options" ADD CONSTRAINT "tag_subfield_options_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tag_subfield_options" ADD CONSTRAINT "tag_subfield_options_attributes_id_project_id_tag_subfields_attributes_id_project_id_fk" FOREIGN KEY ("attributes_id","project_id") REFERENCES "public"."tag_subfields"("attributes_id","project_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tag_subfields" ADD CONSTRAINT "tag_subfields_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tag_subfields" ADD CONSTRAINT "tag_subfields_tag_type_code_project_id_tag_types_code_project_id_fk" FOREIGN KEY ("tag_type_code","project_id") REFERENCES "public"."tag_types"("code","project_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tag_type_class_form_mappings" ADD CONSTRAINT "tag_type_class_form_mappings_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tag_types" ADD CONSTRAINT "tag_types_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "form_metas" ADD CONSTRAINT "form_code_project_unique" UNIQUE("project_id","form_code");--> statement-breakpoint
ALTER TABLE "tag_classes" ADD CONSTRAINT "uniq_code_in_project" UNIQUE("project_id","code");--> statement-breakpoint
ALTER TABLE "tag_subfield_options" ADD CONSTRAINT "uniq_attribute_project_code" UNIQUE("project_id","attributes_id","code");--> statement-breakpoint
ALTER TABLE "tag_subfields" ADD CONSTRAINT "uniq_tag_type_attribute" UNIQUE("project_id","tag_type_code","attributes_id");--> statement-breakpoint
ALTER TABLE "tag_type_class_form_mappings" ADD CONSTRAINT "uniq_mapping_in_project" UNIQUE("project_id","tag_type_label","class_label","form_code");--> statement-breakpoint
CREATE VIEW "public"."view_tag_subfields" AS (select "tag_subfields"."id", "tag_subfields"."project_id", "tag_subfields"."tag_type_code", "tag_types"."description", "tag_subfields"."attributes_id", "tag_subfields"."attributes_description", "tag_subfields"."expression", "tag_subfields"."delimiter", "tag_subfields"."sort_order", "tag_subfields"."created_at", "tag_subfields"."updated_at", "projects"."code", "projects"."name" from "tag_subfields" inner join "tag_types" on ("tag_subfields"."tag_type_code" = "tag_types"."code" and "tag_subfields"."project_id" = "tag_types"."project_id") inner join "projects" on "tag_subfields"."project_id" = "projects"."id");