-
-
Notifications
You must be signed in to change notification settings - Fork 129
fix(account): enforce unique GitHub links #2829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
WcaleNieWolny
wants to merge
8
commits into
main
Choose a base branch
from
wolny/unique-github-account-link
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+305
−1
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e351158
test(account): cover unique GitHub links
WcaleNieWolny 4de93b1
fix(account): enforce unique GitHub links
WcaleNieWolny 746849e
fix(db): prebuild GitHub uniqueness index
WcaleNieWolny 1c177c8
fix(db): validate GitHub uniqueness index
WcaleNieWolny a6f5799
fix(db): recover invalid GitHub index builds
WcaleNieWolny 4c7d69b
fix(db): serialize GitHub index prebuild
WcaleNieWolny b82581e
fix(account): hide GitHub link ownership conflicts
WcaleNieWolny 9efa001
fix(db): sequence GitHub index migration
WcaleNieWolny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| -- Required pre-deploy step: build the GitHub identity index without blocking | ||
| -- writes. Run this file with psql, which executes each statement in autocommit | ||
| -- mode and supports the conditional recovery command below. In SQL Editor, | ||
| -- check for and drop an invalid same-named index first, then run the CREATE | ||
| -- INDEX statement and validation block separately. CREATE INDEX CONCURRENTLY | ||
| -- cannot run inside a transaction. | ||
| -- | ||
| -- Example (psql): | ||
| -- psql "$DATABASE_URL" -v ON_ERROR_STOP=1 \ | ||
| -- -f scripts/ops/users_github_id_unique_index.sql | ||
|
|
||
| -- Keep recovery, creation, and validation in one serialized session. An | ||
| -- invalid index can also be a concurrent build that another deploy is still | ||
| -- running, so a second invocation must wait instead of dropping it. | ||
| SELECT pg_catalog.pg_advisory_lock( | ||
| pg_catalog.hashtextextended('public.users_github_id_key', 0) | ||
| ); | ||
|
|
||
| -- A failed concurrent build leaves an invalid same-named index. Generate a | ||
| -- concurrent drop only for that recoverable state; valid indexes remain intact. | ||
| SELECT format( | ||
| 'DROP INDEX CONCURRENTLY %I.%I', | ||
| index_namespace.nspname, | ||
| idx.relname | ||
| ) | ||
| FROM pg_catalog.pg_class AS idx | ||
| JOIN pg_catalog.pg_namespace AS index_namespace | ||
| ON index_namespace.oid = idx.relnamespace | ||
| JOIN pg_catalog.pg_index AS index_meta | ||
| ON index_meta.indexrelid = idx.oid | ||
| WHERE index_namespace.nspname = 'public' | ||
| AND idx.relname = 'users_github_id_key' | ||
| AND NOT index_meta.indisvalid | ||
| \gexec | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS users_github_id_key | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| ON public.users (github_id); | ||
|
WcaleNieWolny marked this conversation as resolved.
|
||
|
|
||
| DO $$ | ||
| BEGIN | ||
| IF NOT EXISTS ( | ||
| SELECT 1 | ||
| FROM pg_catalog.pg_class AS idx | ||
| JOIN pg_catalog.pg_namespace AS idx_ns | ||
| ON idx_ns.oid = idx.relnamespace | ||
| JOIN pg_catalog.pg_index AS index_meta | ||
| ON index_meta.indexrelid = idx.oid | ||
| JOIN pg_catalog.pg_class AS indexed_table | ||
| ON indexed_table.oid = index_meta.indrelid | ||
| JOIN pg_catalog.pg_namespace AS table_ns | ||
| ON table_ns.oid = indexed_table.relnamespace | ||
| JOIN pg_catalog.pg_attribute AS indexed_column | ||
| ON indexed_column.attrelid = indexed_table.oid | ||
| AND indexed_column.attname = 'github_id' | ||
| AND NOT indexed_column.attisdropped | ||
| JOIN pg_catalog.pg_am AS access_method | ||
| ON access_method.oid = idx.relam | ||
| WHERE idx_ns.nspname = 'public' | ||
| AND idx.relname = 'users_github_id_key' | ||
| AND table_ns.nspname = 'public' | ||
| AND indexed_table.relname = 'users' | ||
| AND index_meta.indisvalid | ||
| AND index_meta.indisready | ||
| AND index_meta.indisunique | ||
| AND index_meta.indislive | ||
| AND NOT index_meta.indnullsnotdistinct | ||
| AND index_meta.indpred IS NULL | ||
| AND index_meta.indexprs IS NULL | ||
| AND index_meta.indnkeyatts = 1 | ||
| AND index_meta.indnatts = 1 | ||
| AND index_meta.indkey[0] = indexed_column.attnum | ||
| AND access_method.amname = 'btree' | ||
| ) THEN | ||
| RAISE EXCEPTION '%', | ||
| 'Index public.users_github_id_key is not a valid, ready, unique ' | ||
| || 'btree index on public.users(github_id) with NULLS DISTINCT. ' | ||
| || 'Drop it and rerun this script.'; | ||
| END IF; | ||
| END | ||
| $$; | ||
|
|
||
| SELECT pg_catalog.pg_advisory_unlock( | ||
| pg_catalog.hashtextextended('public.users_github_id_key', 0) | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
supabase/migrations/20260802171300_enforce_unique_github_id.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| DO $$ | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| DECLARE | ||
| duplicate_github_id bigint; | ||
| duplicate_count bigint; | ||
| BEGIN | ||
| SELECT github_id, COUNT(*) | ||
| INTO duplicate_github_id, duplicate_count | ||
| FROM public.users | ||
| WHERE github_id IS NOT NULL | ||
| GROUP BY github_id | ||
| HAVING COUNT(*) > 1 | ||
| ORDER BY github_id | ||
| LIMIT 1; | ||
|
|
||
| IF duplicate_github_id IS NOT NULL THEN | ||
| RAISE EXCEPTION | ||
| 'Cannot enforce unique GitHub account links: ' | ||
| 'github_id % is linked to % users', | ||
| duplicate_github_id, | ||
| duplicate_count; | ||
| END IF; | ||
| END | ||
| $$; | ||
|
|
||
| -- Production operators must prebuild this index concurrently with | ||
| -- scripts/ops/users_github_id_unique_index.sql before applying the migration. | ||
| -- Refuse the blocking fallback on every populated database. Fresh local/test | ||
| -- databases are empty here, so they remain self-contained without allowing a | ||
| -- missed production pre-deploy step to silently lock writes. | ||
| DO $$ | ||
| BEGIN | ||
| IF pg_catalog.to_regclass('public.users_github_id_key') IS NULL | ||
| AND EXISTS (SELECT 1 FROM public.users LIMIT 1) | ||
| THEN | ||
| RAISE EXCEPTION '%', | ||
| 'Prebuild public.users_github_id_key concurrently with ' | ||
| || 'scripts/ops/users_github_id_unique_index.sql before applying ' | ||
| || 'this migration to a populated database.'; | ||
| END IF; | ||
| END | ||
| $$; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| CREATE UNIQUE INDEX IF NOT EXISTS users_github_id_key | ||
| ON public.users (github_id); | ||
|
|
||
| DO $$ | ||
| BEGIN | ||
| IF NOT EXISTS ( | ||
| SELECT 1 | ||
| FROM pg_catalog.pg_class AS idx | ||
| JOIN pg_catalog.pg_namespace AS idx_ns | ||
| ON idx_ns.oid = idx.relnamespace | ||
| JOIN pg_catalog.pg_index AS index_meta | ||
| ON index_meta.indexrelid = idx.oid | ||
| JOIN pg_catalog.pg_class AS indexed_table | ||
| ON indexed_table.oid = index_meta.indrelid | ||
| JOIN pg_catalog.pg_namespace AS table_ns | ||
| ON table_ns.oid = indexed_table.relnamespace | ||
| JOIN pg_catalog.pg_attribute AS indexed_column | ||
| ON indexed_column.attrelid = indexed_table.oid | ||
| AND indexed_column.attname = 'github_id' | ||
| AND NOT indexed_column.attisdropped | ||
| JOIN pg_catalog.pg_am AS access_method | ||
| ON access_method.oid = idx.relam | ||
| WHERE idx_ns.nspname = 'public' | ||
| AND idx.relname = 'users_github_id_key' | ||
| AND table_ns.nspname = 'public' | ||
| AND indexed_table.relname = 'users' | ||
| AND index_meta.indisvalid | ||
| AND index_meta.indisready | ||
| AND index_meta.indisunique | ||
| AND index_meta.indislive | ||
| AND NOT index_meta.indnullsnotdistinct | ||
| AND index_meta.indpred IS NULL | ||
| AND index_meta.indexprs IS NULL | ||
| AND index_meta.indnkeyatts = 1 | ||
| AND index_meta.indnatts = 1 | ||
| AND index_meta.indkey[0] = indexed_column.attnum | ||
| AND access_method.amname = 'btree' | ||
| ) THEN | ||
| RAISE EXCEPTION '%', | ||
| 'Index public.users_github_id_key is not a valid, ready, unique ' | ||
| || 'btree index on public.users(github_id) with NULLS DISTINCT. ' | ||
| || 'Drop it and rerun scripts/ops/users_github_id_unique_index.sql ' | ||
| || 'before applying this migration.'; | ||
| END IF; | ||
| END | ||
| $$; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| ALTER TABLE public.users | ||
| ADD CONSTRAINT users_github_id_key | ||
| UNIQUE USING INDEX users_github_id_key; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.