-- Auth cutover catch-up: copies into auth_cf what hosted Supabase Auth wrote -- after the cutover migration's copy (20261202125118_auth_cf_cutover), i.e. -- between the migration and the switch to our GoTrue. Paste the whole file -- into the Supabase SQL Editor and run it once, right after the switch. -- Running it again is harmless: it only adds rows that are still missing. -- -- Adds what hosted Auth created since the last run (AUTH_CF_CAUGHT_UP_AT in -- private.settings; the first run starts at the migration's AUTH_CF_COPIED_AT): -- - new accounts and new SSO providers, with all their rows; -- - new identities, MFA factors and email tokens of accounts that exist here -- (a factor enrolled meanwhile keeps protecting its account); -- - new sessions of accounts that exist here, their MFA claims and refresh -- tokens, so people who signed in or refreshed meanwhile stay signed in. -- Each run covers rows created up to its own start; later ones are left for -- the next run. Older rows missing here were deleted here on purpose and are -- not re-added. Passwords changed there and not here are copied. -- It also ends here what hosted Auth ended since the copy: MFA factors and -- sessions copied by the migration that no longer exist there (removals, -- sign-outs), and refresh tokens it revoked. It never re-enables or changes anything else. DO $$ DECLARE v_copied_at timestamptz; v_since timestamptz; v_started_at constant timestamptz := now(); v_new_users uuid[]; v_new_providers uuid[]; v_table text; v_filter text; v_columns text; v_values text; v_count bigint; v_note text; BEGIN IF (SELECT relkind FROM pg_catalog.pg_class WHERE oid = to_regclass('auth_cf.users')) IS DISTINCT FROM 'r' THEN RAISE EXCEPTION 'auth_cf.users is not a GoTrue table'; END IF; SELECT value::timestamptz INTO v_copied_at FROM private.settings WHERE name = 'AUTH_CF_COPIED_AT'; IF v_copied_at IS NULL THEN RAISE EXCEPTION 'no first copy recorded (AUTH_CF_COPIED_AT)'; END IF; SELECT coalesce(max(value::timestamptz), v_copied_at) INTO v_since FROM private.settings WHERE name = 'AUTH_CF_CAUGHT_UP_AT'; SELECT coalesce(array_agg(a.id), '{}') INTO v_new_users FROM auth.users AS a WHERE a.created_at > v_since AND a.created_at <= v_started_at AND NOT EXISTS (SELECT 1 FROM auth_cf.users AS c WHERE c.id = a.id); SELECT coalesce(array_agg(a.id), '{}') INTO v_new_providers FROM auth.sso_providers AS a WHERE a.created_at > v_since AND a.created_at <= v_started_at AND NOT EXISTS (SELECT 1 FROM auth_cf.sso_providers AS c WHERE c.id = a.id); -- Hosted Auth already ran Capgo's triggers on these rows when they were -- written; running them again now (e.g. the email OTP freshness check on -- MFA factors) could refuse rows that were valid then. Both are off -- for the inserts only, inside this transaction (a failure re-enables them). ALTER TABLE auth_cf.users DISABLE TRIGGER prevent_password_signup_on_sso; ALTER TABLE auth_cf.mfa_factors DISABLE TRIGGER trg_enforce_email_otp_for_mfa; -- Parents first; each filter only sees rows whose parent exists here. FOREACH v_table IN ARRAY ARRAY['sso_providers', 'sso_domains', 'saml_providers', 'users', 'identities', 'mfa_factors', 'one_time_tokens', 'sessions', 'mfa_amr_claims', 'refresh_tokens'] LOOP v_filter := CASE v_table WHEN 'users' THEN format('s.id = ANY (%L::uuid[])', v_new_users) WHEN 'sso_providers' THEN format('s.id = ANY (%L::uuid[])', v_new_providers) WHEN 'sso_domains' THEN format('s.sso_provider_id = ANY (%L::uuid[])', v_new_providers) WHEN 'saml_providers' THEN format('s.sso_provider_id = ANY (%L::uuid[])', v_new_providers) WHEN 'sessions' THEN format('s.created_at > %L AND s.created_at <= %L AND EXISTS (SELECT 1 FROM auth_cf.users AS u WHERE u.id = s.user_id)', v_since, v_started_at) WHEN 'mfa_amr_claims' THEN format('s.created_at > %L AND s.created_at <= %L AND EXISTS (SELECT 1 FROM auth_cf.sessions AS x WHERE x.id = s.session_id)', v_since, v_started_at) -- Matched by token, not id: our GoTrue numbers its own refresh tokens. WHEN 'refresh_tokens' THEN format('s.created_at > %L AND s.created_at <= %L AND EXISTS (SELECT 1 FROM auth_cf.sessions AS x WHERE x.id = s.session_id) AND NOT EXISTS (SELECT 1 FROM auth_cf.refresh_tokens AS r WHERE r.token = s.token)', v_since, v_started_at) -- identities, mfa_factors, one_time_tokens ELSE format('(s.user_id = ANY (%L::uuid[]) OR (s.created_at > %L AND s.created_at <= %L)) AND EXISTS (SELECT 1 FROM auth_cf.users AS u WHERE u.id = s.user_id)', v_new_users, v_since, v_started_at) END; -- Same column mapping as the migration; enums are cast by name. SELECT string_agg(format('%I', a.attname), ', ' ORDER BY a.attnum), string_agg(CASE WHEN t.typnamespace = 'auth_cf'::regnamespace THEN format('s.%I::text::%s', a.attname, format_type(a.atttypid, a.atttypmod)) ELSE format('s.%I', a.attname) END, ', ' ORDER BY a.attnum) INTO v_columns, v_values FROM pg_catalog.pg_attribute AS a JOIN pg_catalog.pg_type AS t ON t.oid = a.atttypid WHERE a.attrelid = format('auth_cf.%I', v_table)::regclass AND a.attnum > 0 AND NOT a.attisdropped AND a.attgenerated = '' AND NOT (v_table = 'refresh_tokens' AND a.attname = 'id') AND EXISTS ( SELECT 1 FROM pg_catalog.pg_attribute AS s WHERE s.attrelid = format('auth.%I', v_table)::regclass AND s.attname = a.attname AND s.attnum > 0 AND NOT s.attisdropped ); EXECUTE format('INSERT INTO auth_cf.%I (%s) SELECT %s FROM auth.%I AS s WHERE %s ON CONFLICT DO NOTHING', v_table, v_columns, v_values, v_table, v_filter); GET DIAGNOSTICS v_count = ROW_COUNT; RAISE NOTICE 'auth_cf.%: % added', v_table, v_count; END LOOP; -- A factor copied unverified and verified on hosted Auth since must count here -- too (only unverified -> verified; still inside the trigger-off section, since -- the email OTP check also runs on that update). UPDATE auth_cf.mfa_factors AS c SET status = 'verified', updated_at = a.updated_at FROM auth.mfa_factors AS a WHERE a.id = c.id AND a.status::text = 'verified' AND c.status::text = 'unverified'; GET DIAGNOSTICS v_count = ROW_COUNT; RAISE NOTICE 'auth_cf.mfa_factors: % verified (verified on hosted Auth)', v_count; ALTER TABLE auth_cf.users ENABLE TRIGGER prevent_password_signup_on_sso; ALTER TABLE auth_cf.mfa_factors ENABLE TRIGGER trg_enforce_email_otp_for_mfa; -- A password changed on hosted Auth after the copy replaces the copied one -- (else the old password would keep working), unless it was changed here -- too. The migration kept the copied hashes for this; the first run drops them. IF to_regclass('private.auth_cf_copied_passwords') IS NOT NULL THEN UPDATE auth_cf.users AS c SET encrypted_password = a.encrypted_password FROM auth.users AS a, private.auth_cf_copied_passwords AS p WHERE a.id = c.id AND p.id = c.id AND c.encrypted_password IS NOT DISTINCT FROM p.encrypted_password AND a.encrypted_password IS DISTINCT FROM p.encrypted_password; GET DIAGNOSTICS v_count = ROW_COUNT; RAISE NOTICE 'auth_cf.users: % passwords updated (changed on hosted Auth)', v_count; DROP TABLE private.auth_cf_copied_passwords; END IF; -- Same for MFA factors: one copied by the migration and removed on hosted -- Auth since (e.g. a lost phone) must not keep working here. Sessions that -- reached aal2 with it end too (their MFA claims would keep aal2 on refresh). WITH removed AS ( DELETE FROM auth_cf.mfa_factors AS c WHERE c.created_at <= v_copied_at AND NOT EXISTS (SELECT 1 FROM auth.mfa_factors AS a WHERE a.id = c.id) RETURNING c.id ), ended AS ( DELETE FROM auth_cf.sessions AS x USING removed WHERE x.factor_id = removed.id RETURNING x.id ) SELECT (SELECT count(1) FROM removed) || ' removed, ' || (SELECT count(1) FROM ended) || ' sessions ended' INTO v_note; RAISE NOTICE 'auth_cf.mfa_factors: % (removed on hosted Auth)', v_note; -- Sessions older than the copy only come from the migration (our GoTrue had -- none to keep): gone on hosted Auth means signed out there. Their refresh -- tokens and MFA claims go with them (ON DELETE CASCADE). DELETE FROM auth_cf.sessions AS c WHERE c.created_at <= v_copied_at AND NOT EXISTS (SELECT 1 FROM auth.sessions AS a WHERE a.id = c.id); GET DIAGNOSTICS v_count = ROW_COUNT; RAISE NOTICE 'auth_cf.sessions: % ended (signed out on hosted Auth)', v_count; -- Hosted's revocation time, not now(): GoTrue lets a revoked token refresh -- again within its reuse interval counted from updated_at. UPDATE auth_cf.refresh_tokens AS c SET revoked = true, updated_at = a.updated_at FROM auth.refresh_tokens AS a WHERE a.token = c.token AND a.revoked AND NOT c.revoked; GET DIAGNOSTICS v_count = ROW_COUNT; RAISE NOTICE 'auth_cf.refresh_tokens: % revoked (revoked on hosted Auth)', v_count; INSERT INTO private.settings (name, value) VALUES ('AUTH_CF_CAUGHT_UP_AT', v_started_at::text) ON CONFLICT (name) DO UPDATE SET value = excluded.value, updated_at = now(); END; $$;