feat(schema): make email field optional in accounts table

Only username is required, all other fields (email, phone, public_key, etc.) are now optional.

Changes:
- Modified 001_init_schema.up.sql to remove NOT NULL constraints
- Added partial unique index for email (only for non-NULL values)
- Created migration 006_make_email_optional for existing databases
- Set default status to 'active'

This allows automatic account creation from keygen without requiring user info.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
hailin 2025-12-06 07:16:34 -08:00
parent eb63b9341b
commit 672b6e1630
3 changed files with 33 additions and 7 deletions

View File

@ -114,16 +114,17 @@ CREATE UNIQUE INDEX idx_party_key_shares_unique ON party_key_shares(party_id, se
-- ============================================
-- Accounts table
-- Only username is required, all other fields are optional
CREATE TABLE accounts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username VARCHAR(255) UNIQUE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
email VARCHAR(255),
phone VARCHAR(50),
public_key BYTEA NOT NULL, -- MPC group public key
keygen_session_id UUID NOT NULL, -- Related Keygen session
threshold_n INTEGER NOT NULL,
threshold_t INTEGER NOT NULL,
status VARCHAR(20) NOT NULL,
public_key BYTEA, -- MPC group public key (optional, set after keygen)
keygen_session_id UUID, -- Related Keygen session (optional)
threshold_n INTEGER,
threshold_t INTEGER,
status VARCHAR(20) DEFAULT 'active',
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
last_login_at TIMESTAMP,
@ -132,7 +133,8 @@ CREATE TABLE accounts (
-- Indexes for accounts
CREATE INDEX idx_accounts_username ON accounts(username);
CREATE INDEX idx_accounts_email ON accounts(email);
CREATE INDEX idx_accounts_email ON accounts(email) WHERE email IS NOT NULL;
CREATE UNIQUE INDEX idx_accounts_email_unique ON accounts(email) WHERE email IS NOT NULL;
CREATE INDEX idx_accounts_public_key ON accounts(public_key);
CREATE INDEX idx_accounts_status ON accounts(status);

View File

@ -0,0 +1,12 @@
-- Revert email field to NOT NULL
-- Drop partial unique index
DROP INDEX IF EXISTS idx_accounts_email_unique;
-- Set email back to NOT NULL (this will fail if there are NULL emails in the table)
ALTER TABLE accounts
ALTER COLUMN email SET NOT NULL;
-- Add back UNIQUE constraint on email
ALTER TABLE accounts
ADD CONSTRAINT accounts_email_key UNIQUE (email);

View File

@ -0,0 +1,12 @@
-- Make email field optional in accounts table
-- Only username is required, other fields are optional
ALTER TABLE accounts
ALTER COLUMN email DROP NOT NULL;
-- Drop UNIQUE constraint on email since it can be NULL now
ALTER TABLE accounts
DROP CONSTRAINT IF EXISTS accounts_email_key;
-- Create partial unique index on email (only for non-NULL values)
CREATE UNIQUE INDEX idx_accounts_email_unique ON accounts(email) WHERE email IS NOT NULL;