Files
spsays-bff/migrations/0045_add_users_disciple_id.up.sql
T
2026-08-07 22:46:08 +05:30

20 lines
1.0 KiB
SQL

-- Direct-disciple linkage. Śrīla Prabhupāda's own initiated disciples have
-- started joining the platform; an admin links their account to the matching
-- row in the Mongo `disciples` collection so the site can mark them
-- authentically (a badge on their profile, a highlight in /disciples).
--
-- disciple_id holds the Mongo ObjectID hex (24 chars). It's a cross-store
-- reference, so no foreign key is possible — the admin endpoint validates the
-- id exists in Mongo before writing, and a dangling id degrades gracefully
-- (the name simply doesn't hydrate).
--
-- The partial UNIQUE index enforces the one-to-one rule: a given disciple can
-- be claimed by at most one account, so two users can't both present as the
-- same disciple. NULLs are unconstrained, which leaves the overwhelming
-- majority of users unaffected and keeps the index tiny.
ALTER TABLE users
ADD COLUMN IF NOT EXISTS disciple_id TEXT;
CREATE UNIQUE INDEX IF NOT EXISTS users_disciple_id_key
ON users (disciple_id) WHERE disciple_id IS NOT NULL;