Files
vitareba/scripts/db/accountkit-import.sql
CatoandClaude Opus 5.5 2be962e8aa Stage AccountKit accounts beside NextAuth, runtime behind AUTH_RUNTIME (off) (#134)
* Stage AccountKit tables beside NextAuth and rehearse the account import

NextAuth owns users/accounts/sessions and 31 tables reference users.id,
so AccountKit's tables are added under the auth_ prefix and the shared
user id is users.id. The import (scripts/db/accountkit-import.sql) keeps
ids, addresses, verification and password hashes byte for byte, keeps
Google links as inert rows, and refuses drift, colliding addresses and a
live shared runtime. Its CLI always rolls back. NextAuth stays
authoritative; nothing reads the new tables yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

* Serve Vita's sign-in from AccountKit behind AUTH_RUNTIME (off)

With AUTH_RUNTIME=accountkit, auth() answers today's session shape from
the shared session plus the users row on every request (role changes and
deletions take effect at once; env admins promoted as before). The pages
keep next-auth/react: /api/auth answers its session, csrf, providers,
signout and credentials calls from AccountKit and hands the rest to it.
Password sign-in keeps the per-account lockout; registration, forgot,
reset and change password go through AccountKit with the same answers
and link shape the pages expect. Unset, NextAuth answers as today.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

* Depend on AccountKit 0.13.0 for prefixed account tables

Exempt @bitbaum/accountkit from the release-age gate by name, like the
fleet's other own packages, instead of per version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

* ci: browser smoke of the patient's way in, on NextAuth and on AccountKit

The shared-package gate holds any PR that changes @bitbaum/accountkit until
browser-smoke.yml passes on its head, and Vita had none. This one builds the
production server on PostgreSQL from the committed migrations and drives
Chromium through register, dashboard, sign out, a refused wrong password,
sign in and the recovery pages, plus every auth page at 390px. It runs on
NextAuth, then, after the real import, on AccountKit, where the person
registered under NextAuth signs in with their old password.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

* ci: run the AccountKit leg only where the import is staged

The same workflow file can then ship on branches without the shared
runtime and merge cleanly beside this one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

* chore(deps): accountkit ^0.14.0, the version the canary runs

The fleet rollout gate admits a shared-package version only once the
canary (biaslens) has deployed it; biaslens is on 0.14.0, so 0.13.0 would
wait forever. 0.14.0 adds plugins and trusted origins, neither used here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-29 12:54:01 +01:00

63 lines
3.1 KiB
SQL

-- Copy NextAuth accounts into AccountKit's auth_* tables. Run only inside a
-- transaction, with NextAuth's credential writes stopped for the cutover.
-- The rehearsal CLI always rolls back; this is deliberately not a migration.
LOCK TABLE users, accounts, auth_users, auth_accounts, auth_sessions,
auth_passkeys, auth_two_factors, auth_verifications IN SHARE ROW EXCLUSIVE MODE;
CREATE TEMP TABLE vita_accountkit_users ON COMMIT DROP AS
SELECT id::text AS id, coalesce(name, '') AS name, lower(trim(email)) AS email,
email_verified IS NOT NULL AS verified, image, password
FROM users;
-- Google links keep the provider's subject. They answer only once a provider
-- with id "google" is configured; until then those people recover by email.
CREATE TEMP TABLE vita_accountkit_links ON COMMIT DROP AS
SELECT 'vita-' || provider || ':' || provider_account_id AS id,
provider_account_id AS account_id, provider AS provider_id, user_id::text AS user_id
FROM accounts WHERE type = 'oidc' OR type = 'oauth';
DO $$ BEGIN
IF EXISTS (SELECT email FROM vita_accountkit_users GROUP BY email HAVING count(*) > 1)
OR EXISTS (SELECT 1 FROM vita_accountkit_users WHERE email = '' OR email NOT LIKE '%@%')
THEN RAISE EXCEPTION 'Account import requires address reconciliation'; END IF;
-- Never a second writer beside a live shared runtime: nothing a person did
-- there (a session, a passkey, a factor, a reset) may be overwritten.
IF EXISTS (SELECT 1 FROM auth_sessions) OR EXISTS (SELECT 1 FROM auth_passkeys)
OR EXISTS (SELECT 1 FROM auth_two_factors) OR EXISTS (SELECT 1 FROM auth_verifications)
THEN RAISE EXCEPTION 'Account import refuses an active shared runtime'; END IF;
END $$;
INSERT INTO auth_users (id, name, email, email_verified, image)
SELECT id, name, email, verified, image FROM vita_accountkit_users
ON CONFLICT DO NOTHING;
INSERT INTO auth_accounts (id, account_id, provider_id, user_id, password)
SELECT 'vita-credential:' || id, id, 'credential', id, password
FROM vita_accountkit_users WHERE password IS NOT NULL
ON CONFLICT DO NOTHING;
INSERT INTO auth_accounts (id, account_id, provider_id, user_id)
SELECT id, account_id, provider_id, user_id FROM vita_accountkit_links
ON CONFLICT DO NOTHING;
DO $$ BEGIN
-- A repeat is a no-op only for the same snapshot. Anything that drifted
-- (an address, a password, a link) fails the whole transaction.
IF EXISTS (
SELECT 1 FROM vita_accountkit_users s
LEFT JOIN auth_users u ON u.id = s.id
LEFT JOIN auth_accounts c ON c.user_id = s.id AND c.provider_id = 'credential'
WHERE u.id IS NULL OR u.email IS DISTINCT FROM s.email
OR u.email_verified IS DISTINCT FROM s.verified
OR c.password IS DISTINCT FROM s.password
OR (s.password IS NULL AND c.id IS NOT NULL)
) OR EXISTS (
SELECT 1 FROM auth_users u WHERE NOT EXISTS (SELECT 1 FROM vita_accountkit_users s WHERE s.id = u.id)
) OR EXISTS (
SELECT 1 FROM vita_accountkit_links l
LEFT JOIN auth_accounts a ON a.id = l.id
WHERE a.id IS NULL OR a.user_id IS DISTINCT FROM l.user_id
) THEN RAISE EXCEPTION 'Account import snapshot differs; no changes committed'; END IF;
END $$;