diff --git a/packages/db/src/migrations/0294_equal_echo.sql b/packages/db/src/migrations/0294_equal_echo.sql new file mode 100644 index 00000000000..20f0e5018a1 --- /dev/null +++ b/packages/db/src/migrations/0294_equal_echo.sql @@ -0,0 +1,37 @@ +CREATE TABLE IF NOT EXISTS "issue_human_work_grants" ( + "issue_id" uuid PRIMARY KEY NOT NULL, + "company_id" uuid NOT NULL, + "agent_id" uuid NOT NULL, + "user_id" text NOT NULL, + "created_at" timestamp with time zone DEFAULT now() NOT NULL +); + +--> statement-breakpoint +DO $$ BEGIN +ALTER TABLE "issue_human_work_grants" ADD CONSTRAINT "issue_human_work_grants_company_id_issue_id_issues_company_id_id_fk" FOREIGN KEY ("company_id","issue_id") REFERENCES "public"."issues"("company_id","id") ON DELETE cascade ON UPDATE no action; +EXCEPTION WHEN duplicate_object THEN NULL; +END $$; + +--> statement-breakpoint +DO $$ BEGIN +ALTER TABLE "issue_human_work_grants" ADD CONSTRAINT "issue_human_work_grants_company_id_agent_id_agents_company_id_id_fk" FOREIGN KEY ("company_id","agent_id") REFERENCES "public"."agents"("company_id","id") ON DELETE cascade ON UPDATE no action; +EXCEPTION WHEN duplicate_object THEN NULL; +END $$; + +--> statement-breakpoint +CREATE OR REPLACE FUNCTION revoke_issue_human_work_grant() RETURNS trigger AS $$ +BEGIN + DELETE FROM issue_human_work_grants WHERE issue_id = OLD.id AND company_id = OLD.company_id; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +--> statement-breakpoint +DROP TRIGGER IF EXISTS issue_human_work_grant_reassignment ON issues; + +--> statement-breakpoint +CREATE TRIGGER issue_human_work_grant_reassignment +AFTER UPDATE OF assignee_agent_id, assignee_user_id ON issues +FOR EACH ROW WHEN (OLD.assignee_agent_id IS DISTINCT FROM NEW.assignee_agent_id + OR OLD.assignee_user_id IS DISTINCT FROM NEW.assignee_user_id) +EXECUTE FUNCTION revoke_issue_human_work_grant(); diff --git a/packages/db/src/migrations/meta/_journal.json b/packages/db/src/migrations/meta/_journal.json index 71acfdde0c6..6877700bb5b 100644 --- a/packages/db/src/migrations/meta/_journal.json +++ b/packages/db/src/migrations/meta/_journal.json @@ -2045,6 +2045,13 @@ "when": 1790796608171, "tag": "0293_broad_rattler", "breakpoints": true + }, + { + "idx": 294, + "version": "7", + "when": 1790806258326, + "tag": "0294_equal_echo", + "breakpoints": true } ] } \ No newline at end of file