diff --git a/migrations/sqlite/000000_init.up.sql b/migrations/sqlite/000000_init.up.sql index 26b2f6e5..f02d1001 100644 --- a/migrations/sqlite/000000_init.up.sql +++ b/migrations/sqlite/000000_init.up.sql @@ -444,6 +444,11 @@ CREATE TABLE IF NOT EXISTS organization_join_requests ( CREATE INDEX IF NOT EXISTS idx_org_join_requests_org_id ON organization_join_requests(organization_id); CREATE INDEX IF NOT EXISTS idx_org_join_requests_user_id ON organization_join_requests(user_id); CREATE INDEX IF NOT EXISTS idx_org_join_requests_status ON organization_join_requests(status); +-- Plan 3 (#1303): at most one pending request per (org, tenant, type). +-- Approved/rejected rows are not constrained so the audit trail stays. +CREATE UNIQUE INDEX IF NOT EXISTS uq_org_join_requests_pending_per_tenant + ON organization_join_requests(organization_id, tenant_id, request_type) + WHERE status = 'pending'; CREATE TABLE IF NOT EXISTS agent_shares ( id VARCHAR(36) PRIMARY KEY, diff --git a/migrations/versioned/000045_org_tenant_members.down.sql b/migrations/versioned/000045_org_tenant_members.down.sql index 817ffbca..d9de5069 100644 --- a/migrations/versioned/000045_org_tenant_members.down.sql +++ b/migrations/versioned/000045_org_tenant_members.down.sql @@ -24,4 +24,12 @@ ALTER INDEX IF EXISTS idx_org_members_role_pre_plan3 RENAME TO idx_org_ -- Drop the new table. The unique/secondary indexes go with it. DROP TABLE IF EXISTS organization_tenant_members; +-- Drop the partial unique index added in step 5 of up.sql. The data +-- mutation (pending → rejected) is intentionally NOT reverted: those +-- duplicates were already wrong under Plan 3 semantics, and we don't +-- have enough information to safely re-promote which one should win. +-- The `[Plan 3] superseded` review_message tag makes the affected +-- rows easy to audit if a manual fix is later needed. +DROP INDEX IF EXISTS uq_org_join_requests_pending_per_tenant; + DO $$ BEGIN RAISE NOTICE '[Migration 000045] Plan 3 reverted'; END $$; diff --git a/migrations/versioned/000045_org_tenant_members.up.sql b/migrations/versioned/000045_org_tenant_members.up.sql index 4e142e7e..e23efa1a 100644 --- a/migrations/versioned/000045_org_tenant_members.up.sql +++ b/migrations/versioned/000045_org_tenant_members.up.sql @@ -123,4 +123,41 @@ ALTER INDEX IF EXISTS idx_org_members_user_id RENAME TO idx_org_members_us ALTER INDEX IF EXISTS idx_org_members_tenant_id RENAME TO idx_org_members_tenant_id_pre_plan3; ALTER INDEX IF EXISTS idx_org_members_role RENAME TO idx_org_members_role_pre_plan3; +-- 5. Dedup pending join/upgrade requests at the (org, tenant, type) +-- level. Plan 3 lifts the dedup key from per-user to per-tenant +-- (see GetPendingRequestByTenantAndType), so any leftover duplicate +-- pending rows from the old per-user model are now ambiguous: one +-- row would silently shadow the others and admins would see a +-- "ghost" entry remaining in the list after they approved one. +-- +-- Strategy: keep the earliest pending row per (org, tenant, type), +-- mark the rest as 'rejected' with a system review_message so the +-- audit trail makes it clear this was a Plan 3 cleanup, not a +-- human reject. Then add a partial unique index to keep the +-- invariant going forward. +DO $$ BEGIN RAISE NOTICE '[Migration 000045] Deduping pending join/upgrade requests at (org, tenant, type) level'; END $$; +WITH ranked AS ( + SELECT id, + ROW_NUMBER() OVER ( + PARTITION BY organization_id, tenant_id, request_type + ORDER BY created_at ASC, id ASC + ) AS rn + FROM organization_join_requests + WHERE status = 'pending' +) +UPDATE organization_join_requests r + SET status = 'rejected', + review_message = '[Plan 3] superseded: another pending request from the same tenant was kept', + updated_at = CURRENT_TIMESTAMP + FROM ranked + WHERE r.id = ranked.id + AND ranked.rn > 1; + +-- Partial unique index: only one pending row per (org, tenant, type). +-- Postgres lets approved/rejected rows coexist freely so the historical +-- audit trail is preserved. +CREATE UNIQUE INDEX IF NOT EXISTS uq_org_join_requests_pending_per_tenant + ON organization_join_requests (organization_id, tenant_id, request_type) + WHERE status = 'pending'; + DO $$ BEGIN RAISE NOTICE '[Migration 000045] Plan 3 setup ready'; END $$;