Files
gochat/backend/migrations/000010_add_access_tokens_table.up.sql
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

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);