30 lines
1.3 KiB
SQL
30 lines
1.3 KiB
SQL
-- Access tokens table: stores hashed API tokens for Users and PlatformApps
|
|
-- Reference: Chatwoot access_tokens model (replaces direct api_key on platform_apps)
|
|
|
|
CREATE TABLE IF NOT EXISTS access_tokens (
|
|
id SERIAL PRIMARY KEY,
|
|
owner_type VARCHAR(100) NOT NULL,
|
|
owner_id INTEGER NOT NULL,
|
|
token VARCHAR(255) NOT NULL UNIQUE,
|
|
token_prefix VARCHAR(20) NOT NULL,
|
|
name VARCHAR(255),
|
|
expires_at TIMESTAMP WITH TIME ZONE,
|
|
last_used_at TIMESTAMP WITH TIME ZONE,
|
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
|
deleted_at TIMESTAMP WITH TIME ZONE
|
|
);
|
|
|
|
-- Unique constraint: one active token per (owner_type, owner_id) pair (excluding soft-deleted)
|
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_access_tokens_owner_unique
|
|
ON access_tokens(owner_type, owner_id) WHERE deleted_at IS NULL;
|
|
|
|
-- Indexes for lookup performance
|
|
CREATE INDEX IF NOT EXISTS idx_access_tokens_owner_type
|
|
ON access_tokens(owner_type);
|
|
CREATE INDEX IF NOT EXISTS idx_access_tokens_owner_id
|
|
ON access_tokens(owner_id);
|
|
CREATE INDEX IF NOT EXISTS idx_access_tokens_token_prefix
|
|
ON access_tokens(token_prefix) WHERE deleted_at IS NULL;
|
|
CREATE INDEX IF NOT EXISTS idx_access_tokens_deleted_at
|
|
ON access_tokens(deleted_at); |