@rudderhq/db 0.4.6-canary.1 → 0.4.6-canary.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,89 @@
1
+ CREATE TABLE "organization_issue_prefix_aliases" (
2
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
3
+ "org_id" uuid NOT NULL,
4
+ "prefix" text NOT NULL,
5
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
6
+ );
7
+ --> statement-breakpoint
8
+ ALTER TABLE "organization_issue_prefix_aliases" ADD CONSTRAINT "organization_issue_prefix_aliases_org_id_organizations_id_fk" FOREIGN KEY ("org_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
9
+ CREATE UNIQUE INDEX "organization_issue_prefix_aliases_prefix_idx" ON "organization_issue_prefix_aliases" USING btree ("prefix");
10
+ --> statement-breakpoint
11
+ SELECT pg_advisory_xact_lock(hashtext('rudder:organization-issue-prefix'));
12
+ --> statement-breakpoint
13
+ LOCK TABLE "organizations" IN SHARE ROW EXCLUSIVE MODE;
14
+ --> statement-breakpoint
15
+ DO $$
16
+ BEGIN
17
+ IF EXISTS (
18
+ SELECT lower(route_key)
19
+ FROM (
20
+ SELECT "id" AS org_id, "url_key" AS route_key FROM "organizations"
21
+ UNION ALL
22
+ SELECT "id" AS org_id, "issue_prefix" AS route_key FROM "organizations"
23
+ ) existing_route_keys
24
+ GROUP BY lower(route_key)
25
+ HAVING count(DISTINCT org_id) > 1
26
+ ) THEN
27
+ RAISE EXCEPTION 'Existing organization route identities conflict case-insensitively. Resolve the conflicting URL and Issue Keys before applying migration 0100.';
28
+ END IF;
29
+ END
30
+ $$;
31
+ --> statement-breakpoint
32
+ CREATE FUNCTION "enforce_organization_route_key_namespace"() RETURNS trigger
33
+ LANGUAGE plpgsql
34
+ AS $$
35
+ BEGIN
36
+ PERFORM pg_advisory_xact_lock(hashtext('rudder:organization-issue-prefix'));
37
+ IF EXISTS (
38
+ SELECT 1
39
+ FROM "organizations" other
40
+ WHERE other."id" <> NEW."id"
41
+ AND (
42
+ lower(other."url_key") IN (lower(NEW."url_key"), lower(NEW."issue_prefix"))
43
+ OR lower(other."issue_prefix") IN (lower(NEW."url_key"), lower(NEW."issue_prefix"))
44
+ )
45
+ ) OR EXISTS (
46
+ SELECT 1
47
+ FROM "organization_issue_prefix_aliases" alias
48
+ WHERE alias."org_id" <> NEW."id"
49
+ AND lower(alias."prefix") IN (lower(NEW."url_key"), lower(NEW."issue_prefix"))
50
+ ) THEN
51
+ RAISE EXCEPTION 'Organization URL key and Issue Key routes must be unique across organizations.';
52
+ END IF;
53
+ RETURN NEW;
54
+ END
55
+ $$;
56
+ --> statement-breakpoint
57
+ CREATE FUNCTION "enforce_organization_alias_route_key_namespace"() RETURNS trigger
58
+ LANGUAGE plpgsql
59
+ AS $$
60
+ BEGIN
61
+ PERFORM pg_advisory_xact_lock(hashtext('rudder:organization-issue-prefix'));
62
+ IF EXISTS (
63
+ SELECT 1
64
+ FROM "organizations" organization
65
+ WHERE organization."id" <> NEW."org_id"
66
+ AND (
67
+ lower(organization."url_key") = lower(NEW."prefix")
68
+ OR lower(organization."issue_prefix") = lower(NEW."prefix")
69
+ )
70
+ ) OR EXISTS (
71
+ SELECT 1
72
+ FROM "organization_issue_prefix_aliases" alias
73
+ WHERE alias."org_id" <> NEW."org_id"
74
+ AND alias."id" <> NEW."id"
75
+ AND lower(alias."prefix") = lower(NEW."prefix")
76
+ ) THEN
77
+ RAISE EXCEPTION 'Historical Issue Key routes must be unique across organizations.';
78
+ END IF;
79
+ RETURN NEW;
80
+ END
81
+ $$;
82
+ --> statement-breakpoint
83
+ CREATE TRIGGER "organizations_route_key_namespace_trigger"
84
+ BEFORE INSERT OR UPDATE OF "url_key", "issue_prefix" ON "organizations"
85
+ FOR EACH ROW EXECUTE FUNCTION "enforce_organization_route_key_namespace"();
86
+ --> statement-breakpoint
87
+ CREATE TRIGGER "organization_alias_route_key_namespace_trigger"
88
+ BEFORE INSERT OR UPDATE OF "org_id", "prefix" ON "organization_issue_prefix_aliases"
89
+ FOR EACH ROW EXECUTE FUNCTION "enforce_organization_alias_route_key_namespace"();