-- Adds User.discord_id for Discord account linking (backend/discord_link.py).
--
-- This project has no Alembic: SQLAlchemy create_all() only creates *missing
-- tables*, so it will NOT add this column to an existing accounts DB. deploy.sh
-- and deploy-dev.sh run deploy/migrate-db.py, which applies the pending files
-- here on every deploy (tracked in a schema_migrations table, backing the DB up
-- first). A fresh DB gets the column + uniqueness from the model and is baselined
-- rather than ALTERed. To apply by hand instead (back the DB up first):
--
--   sqlite3 /opt/thermograph/data/accounts.sqlite < deploy/migrations/001-user-discord-id.sql

ALTER TABLE user ADD COLUMN discord_id VARCHAR(32);

-- SQLite can't add a UNIQUE column via ALTER, so enforce it with a partial unique
-- index (NULLs are allowed to repeat; a real id links to at most one account).
CREATE UNIQUE INDEX IF NOT EXISTS ix_user_discord_id
    ON user (discord_id) WHERE discord_id IS NOT NULL;
