Skip to content
174 changes: 166 additions & 8 deletions app/controlplane/pkg/data/ent/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/controlplane/pkg/data/ent/ent.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions app/controlplane/pkg/data/ent/hook/hook.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Modify "referrer_references" table
-- The foreign keys get the names of the new edge schema. A rename keeps them valid without
-- checking every row again.
ALTER TABLE "referrer_references" RENAME CONSTRAINT "referrer_references_referred_by_id" TO "referrer_references_referrers_referred_by";
ALTER TABLE "referrer_references" RENAME CONSTRAINT "referrer_references_referrer_id" TO "referrer_references_referrers_referrer";
-- A non-volatile default does not rewrite the table, so the lock is held only briefly.
ALTER TABLE "referrer_references" ADD COLUMN "created_at" timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP;
-- A nullable column with no default only changes the catalog.
ALTER TABLE "referrer_references" ADD COLUMN "organization_id" uuid NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- atlas:txmode none

-- Fill in the new columns of the existing references, in one pass over the table. Outside a
-- transaction with the schema change above, the update holds row locks only, until it ends. Reads
-- and new references are not blocked. Storing an existing reference again waits for the update.
--
-- created_at is the creation time of the referrer the reference points to.
--
-- organization_id is the organization of the attestation that stored the reference. Storing an
-- attestation links every referrer it touches, both ends of each reference included, to the
-- attestation's workflow, so that organization is one that both ends belong to. When the ends
-- share exactly one organization, it is that one. When they share more, the reference stays NULL,
-- which means that it is read in every organization.
WITH "referrer_orgs" AS (
SELECT DISTINCT "rw"."referrer_id", "w"."organization_id"
FROM "referrer_workflows" AS "rw"
JOIN "workflows" AS "w" ON "w"."id" = "rw"."workflow_id"
), "backfill" AS (
SELECT "rr"."referrer_id", "rr"."referred_by_id", "r"."created_at",
CASE WHEN count("b"."organization_id") = 1 THEN (array_agg("b"."organization_id") FILTER (WHERE "b"."organization_id" IS NOT NULL))[1] END AS "organization_id"
FROM "referrer_references" AS "rr"
JOIN "referrers" AS "r" ON "r"."id" = "rr"."referred_by_id"
LEFT JOIN "referrer_orgs" AS "a" ON "a"."referrer_id" = "rr"."referrer_id"
LEFT JOIN "referrer_orgs" AS "b" ON "b"."referrer_id" = "rr"."referred_by_id" AND "b"."organization_id" = "a"."organization_id"
GROUP BY "rr"."referrer_id", "rr"."referred_by_id", "r"."created_at"
)
UPDATE "referrer_references" AS "rr"
SET "created_at" = "b"."created_at", "organization_id" = "b"."organization_id"
FROM "backfill" AS "b"
WHERE "b"."referrer_id" = "rr"."referrer_id" AND "b"."referred_by_id" = "rr"."referred_by_id"
AND ("rr"."created_at" <> "b"."created_at" OR "rr"."organization_id" IS DISTINCT FROM "b"."organization_id");
-- The update leaves a dead version of each row. Reclaim them before the index build reads the table.
VACUUM (ANALYZE) "referrer_references";

-- Create index "referrerreference_referrer_id_organization_id_created_at" to table: "referrer_references"
CREATE INDEX CONCURRENTLY "referrerreference_referrer_id_organization_id_created_at" ON "referrer_references" ("referrer_id", "organization_id", "created_at" DESC, "referred_by_id" DESC);
4 changes: 3 additions & 1 deletion app/controlplane/pkg/data/ent/migrate/migrations/atlas.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
h1:sz67a3W+tAr+VPRPPJ48+ancduf0og/7OrQnz+ECdyk=
h1:hWv3uPoEHIMT147AEfHNRFZ7uMnYVlKAVQACoFjPAoE=
20230706165452_init-schema.sql h1:VvqbNFEQnCvUVyj2iDYVQQxDM0+sSXqocpt/5H64k8M=
20230710111950-cas-backend.sql h1:A8iBuSzZIEbdsv9ipBtscZQuaBp3V5/VMw7eZH6GX+g=
20230712094107-cas-backends-workflow-runs.sql h1:a5rzxpVGyd56nLRSsKrmCFc9sebg65RWzLghKHh5xvI=
Expand Down Expand Up @@ -144,3 +144,5 @@ h1:sz67a3W+tAr+VPRPPJ48+ancduf0og/7OrQnz+ECdyk=
20260929114227.sql h1:oulkhKH6cKhFva/5mYx6aSaFarM6YgeuTh0Plw8X38E=
20260930160057.sql h1:rKEj/76c/TiBjl/loP4Bld1BPMPTBlCWhaWvN/pJaRY=
20261015173225.sql h1:s5wTFJqcXx+cGUPpzyY93wP/IuizLaQ/9rP/VqtamRE=
20261116124542.sql h1:PNFvP+qcO1URsP9QsKGpAwaAL9m7yP7bzi/8SHk6yNs=
20261116124543.sql h1:r+8IOuLFQ3iMrQjYN8hPGxItVJu/bidc0/yeEUe6RFQ=
Loading
Loading