mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-17 03:10:38 +09:00
51 lines
3.1 KiB
MySQL
51 lines
3.1 KiB
MySQL
|
|
CREATE TABLE IF NOT EXISTS "issue_reference_mentions" (
|
||
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||
|
|
"company_id" uuid NOT NULL,
|
||
|
|
"source_issue_id" uuid NOT NULL,
|
||
|
|
"target_issue_id" uuid NOT NULL,
|
||
|
|
"source_kind" text NOT NULL,
|
||
|
|
"source_record_id" uuid,
|
||
|
|
"document_key" text,
|
||
|
|
"matched_text" text,
|
||
|
|
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||
|
|
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||
|
|
);
|
||
|
|
--> statement-breakpoint
|
||
|
|
DO $$ BEGIN
|
||
|
|
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'issue_reference_mentions_company_id_companies_id_fk') THEN
|
||
|
|
ALTER TABLE "issue_reference_mentions" ADD CONSTRAINT "issue_reference_mentions_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;
|
||
|
|
END IF;
|
||
|
|
END $$;
|
||
|
|
--> statement-breakpoint
|
||
|
|
DO $$ BEGIN
|
||
|
|
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'issue_reference_mentions_source_issue_id_issues_id_fk') THEN
|
||
|
|
ALTER TABLE "issue_reference_mentions" ADD CONSTRAINT "issue_reference_mentions_source_issue_id_issues_id_fk" FOREIGN KEY ("source_issue_id") REFERENCES "public"."issues"("id") ON DELETE cascade ON UPDATE no action;
|
||
|
|
END IF;
|
||
|
|
END $$;
|
||
|
|
--> statement-breakpoint
|
||
|
|
DO $$ BEGIN
|
||
|
|
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'issue_reference_mentions_target_issue_id_issues_id_fk') THEN
|
||
|
|
ALTER TABLE "issue_reference_mentions" ADD CONSTRAINT "issue_reference_mentions_target_issue_id_issues_id_fk" FOREIGN KEY ("target_issue_id") REFERENCES "public"."issues"("id") ON DELETE cascade ON UPDATE no action;
|
||
|
|
END IF;
|
||
|
|
END $$;--> statement-breakpoint
|
||
|
|
CREATE INDEX IF NOT EXISTS "issue_reference_mentions_company_source_issue_idx" ON "issue_reference_mentions" USING btree ("company_id","source_issue_id");--> statement-breakpoint
|
||
|
|
CREATE INDEX IF NOT EXISTS "issue_reference_mentions_company_target_issue_idx" ON "issue_reference_mentions" USING btree ("company_id","target_issue_id");--> statement-breakpoint
|
||
|
|
CREATE INDEX IF NOT EXISTS "issue_reference_mentions_company_issue_pair_idx" ON "issue_reference_mentions" USING btree ("company_id","source_issue_id","target_issue_id");--> statement-breakpoint
|
||
|
|
DELETE FROM "issue_reference_mentions"
|
||
|
|
WHERE "id" IN (
|
||
|
|
SELECT "id"
|
||
|
|
FROM (
|
||
|
|
SELECT
|
||
|
|
"id",
|
||
|
|
row_number() OVER (
|
||
|
|
PARTITION BY "company_id", "source_issue_id", "target_issue_id", "source_kind", "source_record_id"
|
||
|
|
ORDER BY "created_at", "id"
|
||
|
|
) AS "row_number"
|
||
|
|
FROM "issue_reference_mentions"
|
||
|
|
) AS "duplicates"
|
||
|
|
WHERE "duplicates"."row_number" > 1
|
||
|
|
);--> statement-breakpoint
|
||
|
|
DROP INDEX IF EXISTS "issue_reference_mentions_company_source_mention_uq";--> statement-breakpoint
|
||
|
|
CREATE UNIQUE INDEX IF NOT EXISTS "issue_reference_mentions_company_source_mention_record_uq" ON "issue_reference_mentions" USING btree ("company_id","source_issue_id","target_issue_id","source_kind","source_record_id") WHERE "source_record_id" IS NOT NULL;--> statement-breakpoint
|
||
|
|
CREATE UNIQUE INDEX IF NOT EXISTS "issue_reference_mentions_company_source_mention_null_record_uq" ON "issue_reference_mentions" USING btree ("company_id","source_issue_id","target_issue_id","source_kind") WHERE "source_record_id" IS NULL;
|