Files
member-console/internal/identity/migrations/00001_init.sql
Christian Galo 1f1540d7e0 Use plain DB connection for migrations
Add ConnectPlain to open the DB without the custom search_path and
switch migration and CLI flows to run on that plain connection.
Wrap multi-statement goose migrations with StatementBegin/End to
ensure statements are executed atomically. Move Stripe price outbox
seeding into a dedicated stripe migration.
2026-04-05 18:25:05 -05:00

59 lines
2.1 KiB
SQL

-- +goose Up
-- +goose StatementBegin
-- Safety: drop any pre-existing users table (no-op on fresh installs).
DROP TABLE IF EXISTS identity.users CASCADE;
CREATE TABLE identity.users (
user_id UUID PRIMARY KEY DEFAULT uuidv7(),
oidc_subject TEXT UNIQUE NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'active',
last_login_at TIMESTAMPTZ,
last_login_ip TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE identity.persons (
person_id UUID PRIMARY KEY DEFAULT uuidv7(),
user_id UUID UNIQUE NOT NULL REFERENCES identity.users(user_id) ON DELETE CASCADE,
display_name VARCHAR(255) NOT NULL,
primary_email VARCHAR(255) NOT NULL,
primary_email_verified BOOLEAN NOT NULL DEFAULT FALSE,
status VARCHAR(20) NOT NULL DEFAULT 'active',
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_users_oidc_subject ON identity.users(oidc_subject);
CREATE INDEX idx_persons_user_id ON identity.persons(user_id);
-- Reuse the update_updated_at_column() function created by db/00001_init.sql
CREATE TRIGGER trigger_users_updated_at
BEFORE UPDATE ON identity.users
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER trigger_persons_updated_at
BEFORE UPDATE ON identity.persons
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- Per-schema role grants (Decision 115).
GRANT ALL ON ALL TABLES IN SCHEMA identity TO identity_owner;
GRANT ALL ON ALL TABLES IN SCHEMA identity TO identity_writer;
GRANT SELECT ON ALL TABLES IN SCHEMA identity TO identity_reader;
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TRIGGER IF EXISTS trigger_persons_updated_at ON identity.persons;
DROP TRIGGER IF EXISTS trigger_users_updated_at ON identity.users;
DROP INDEX IF EXISTS identity.idx_persons_user_id;
DROP INDEX IF EXISTS identity.idx_users_oidc_subject;
DROP TABLE IF EXISTS identity.persons;
DROP TABLE IF EXISTS identity.users;
-- +goose StatementEnd