Files
gochat/backend/migrations/000040_add_sla_tables.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

76 lines
3.4 KiB
SQL

CREATE TABLE IF NOT EXISTS sla_policies (
id SERIAL PRIMARY KEY,
account_id INTEGER NOT NULL,
name VARCHAR(255) NOT NULL,
description TEXT,
first_response_time_threshold INTEGER DEFAULT 0,
next_response_time_threshold INTEGER DEFAULT 0,
resolution_time_threshold INTEGER DEFAULT 0,
only_during_business_hours BOOLEAN DEFAULT FALSE,
paused_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
CREATE INDEX IF NOT EXISTS idx_sla_policies_account_id ON sla_policies(account_id);
CREATE INDEX IF NOT EXISTS idx_sla_policies_paused_at ON sla_policies(paused_at);
CREATE INDEX IF NOT EXISTS idx_sla_policies_deleted_at ON sla_policies(deleted_at);
CREATE TABLE IF NOT EXISTS sla_policy_inboxes (
id SERIAL PRIMARY KEY,
sla_policy_id INTEGER NOT NULL,
inbox_id INTEGER NOT NULL,
account_id INTEGER NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_sla_inbox_unique ON sla_policy_inboxes(inbox_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_sla_policy_inboxes_sla_policy_id ON sla_policy_inboxes(sla_policy_id);
CREATE INDEX IF NOT EXISTS idx_sla_policy_inboxes_account_id ON sla_policy_inboxes(account_id);
CREATE INDEX IF NOT EXISTS idx_sla_policy_inboxes_deleted_at ON sla_policy_inboxes(deleted_at);
CREATE TABLE IF NOT EXISTS applied_slas (
id SERIAL PRIMARY KEY,
sla_policy_id INTEGER NOT NULL,
conversation_id INTEGER NOT NULL,
account_id INTEGER NOT NULL,
sla_status VARCHAR(50) NOT NULL DEFAULT 'active',
frt_target_at TIMESTAMPTZ,
nrt_target_at TIMESTAMPTZ,
rt_target_at TIMESTAMPTZ,
frt_actual_at TIMESTAMPTZ,
nrt_actual_at TIMESTAMPTZ,
rt_actual_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
CREATE INDEX IF NOT EXISTS idx_applied_slas_sla_policy_id ON applied_slas(sla_policy_id);
CREATE INDEX IF NOT EXISTS idx_applied_slas_conversation_id ON applied_slas(conversation_id);
CREATE INDEX IF NOT EXISTS idx_applied_slas_account_id ON applied_slas(account_id);
CREATE INDEX IF NOT EXISTS idx_applied_slas_frt_target_at ON applied_slas(frt_target_at);
CREATE INDEX IF NOT EXISTS idx_applied_slas_nrt_target_at ON applied_slas(nrt_target_at);
CREATE INDEX IF NOT EXISTS idx_applied_slas_rt_target_at ON applied_slas(rt_target_at);
CREATE INDEX IF NOT EXISTS idx_applied_slas_deleted_at ON applied_slas(deleted_at);
CREATE TABLE IF NOT EXISTS sla_events (
id SERIAL PRIMARY KEY,
applied_sla_id INTEGER NOT NULL,
account_id INTEGER NOT NULL,
conversation_id INTEGER NOT NULL,
inbox_id INTEGER NOT NULL,
sla_policy_id INTEGER NOT NULL,
event_type VARCHAR(50) NOT NULL,
meta JSONB DEFAULT '{}',
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
CREATE INDEX IF NOT EXISTS idx_sla_events_applied_sla_id ON sla_events(applied_sla_id);
CREATE INDEX IF NOT EXISTS idx_sla_events_account_id ON sla_events(account_id);
CREATE INDEX IF NOT EXISTS idx_sla_events_conversation_id ON sla_events(conversation_id);
CREATE INDEX IF NOT EXISTS idx_sla_events_inbox_id ON sla_events(inbox_id);
CREATE INDEX IF NOT EXISTS idx_sla_events_sla_policy_id ON sla_events(sla_policy_id);
CREATE INDEX IF NOT EXISTS idx_sla_events_deleted_at ON sla_events(deleted_at);