feat(mam-api): add groups and API tokens schema patch
This commit is contained in:
parent
3154cce37c
commit
2d8c44c529
1 changed files with 36 additions and 0 deletions
36
services/mam-api/src/db/schema_patch_groups_tokens.sql
Normal file
36
services/mam-api/src/db/schema_patch_groups_tokens.sql
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
-- Wild Dragon MAM – Groups & API Tokens schema patch
|
||||
-- Run with: psql $DATABASE_URL -f schema_patch_groups_tokens.sql
|
||||
|
||||
-- User groups
|
||||
CREATE TABLE IF NOT EXISTS groups (
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
name TEXT UNIQUE NOT NULL,
|
||||
description TEXT,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- User ↔ group memberships
|
||||
CREATE TABLE IF NOT EXISTS user_groups (
|
||||
user_id UUID NOT NULL REFERENCES users ON DELETE CASCADE,
|
||||
group_id UUID NOT NULL REFERENCES groups ON DELETE CASCADE,
|
||||
PRIMARY KEY (user_id, group_id)
|
||||
);
|
||||
|
||||
-- Personal API tokens (Bearer auth alternative to session cookies)
|
||||
-- token_hash : SHA-256(raw_token) stored as hex
|
||||
-- token_prefix: first 8 chars of raw token for display only
|
||||
CREATE TABLE IF NOT EXISTS api_tokens (
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||
user_id UUID NOT NULL REFERENCES users ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
token_hash TEXT NOT NULL UNIQUE,
|
||||
token_prefix TEXT NOT NULL,
|
||||
last_used_at TIMESTAMPTZ,
|
||||
expires_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_api_tokens_user_id ON api_tokens(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_api_tokens_hash ON api_tokens(token_hash);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_groups_user ON user_groups(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_groups_group ON user_groups(group_id);
|
||||
Loading…
Reference in a new issue