Files
probo/pkg/coredata/migrations/20251028T152339Z.sql
Sacha Al Himdani 75f3f92f04 Add rank to reference
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
2025-10-29 12:37:25 +01:00

20 lines
585 B
SQL

ALTER TABLE trust_center_references ADD COLUMN rank INTEGER;
WITH ranked_references AS (
SELECT
id,
ROW_NUMBER() OVER (PARTITION BY trust_center_id ORDER BY created_at DESC, id DESC) AS rn
FROM trust_center_references
)
UPDATE trust_center_references tcr
SET rank = rr.rn
FROM ranked_references rr
WHERE tcr.id = rr.id;
ALTER TABLE trust_center_references ALTER COLUMN rank SET NOT NULL;
ALTER TABLE trust_center_references
ADD CONSTRAINT trust_center_references_trust_center_id_rank_key
UNIQUE (trust_center_id, rank)
DEFERRABLE INITIALLY DEFERRED;