Files
Shellcore-Accounts/scripts/sql/migrations/001SeparateCredentialsAndEmail.sql

38 lines
1.3 KiB
PL/PgSQL

-- Apply to an existing database created from the original shellcore.sql.
-- This intentionally refuses to discard any populated plaintext email values.
BEGIN;
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM accounts WHERE email IS NOT NULL) THEN
RAISE EXCEPTION 'Existing email data requires a separately designed encrypted mail-store migration';
END IF;
IF EXISTS (SELECT 1 FROM accounts WHERE username IS NULL) THEN
RAISE EXCEPTION 'Every existing account needs a username before migration';
END IF;
IF EXISTS (
SELECT 1 FROM accounts a
WHERE a.passwordHash IS NULL
AND NOT EXISTS (SELECT 1 FROM externalAccounts e WHERE e.accountId = a.id AND e.provider = 'steam')
) THEN
RAISE EXCEPTION 'An account without a local password needs a verified Steam link before migration';
END IF;
END $$;
CREATE TABLE IF NOT EXISTS localCredentials (
accountId UUID PRIMARY KEY REFERENCES accounts (id) ON DELETE CASCADE,
passwordHash TEXT NOT NULL
);
INSERT INTO localCredentials (accountId, passwordHash)
SELECT id, passwordHash FROM accounts WHERE passwordHash IS NOT NULL
ON CONFLICT (accountId) DO NOTHING;
ALTER TABLE accounts ALTER COLUMN username SET NOT NULL;
DROP INDEX IF EXISTS accountsEmailUnique;
ALTER TABLE accounts DROP COLUMN passwordHash;
ALTER TABLE accounts DROP COLUMN emailVerified;
ALTER TABLE accounts DROP COLUMN email;
COMMIT;