Files
gochat/backend/migrations/000015_add_integration_hook_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

49 lines
3.4 KiB
SQL

-- G16: Third-party integration tables (IntegrationHook + IntegrationApp)
-- Reference: Chatwoot Integrations::Hook + Integrations::App
-- IntegrationHook: per-account/per-inbox integration binding
-- Stores provider-specific settings in JSONB, keyed by hook_type (slack/shopify/linear/notion/webhook)
CREATE TABLE IF NOT EXISTS integration_hooks (
id SERIAL PRIMARY KEY,
account_id INTEGER NOT NULL,
inbox_id INTEGER, -- NULL = account-level hook
hook_type VARCHAR(50) NOT NULL, -- webhook/slack/shopify/linear/notion
status VARCHAR(20) DEFAULT 'active', -- active/inactive
url VARCHAR(1024) DEFAULT '', -- webhook callback URL
access_token VARCHAR(256), -- API token for the hook (unique)
settings JSONB DEFAULT '{}', -- provider-specific config (Slack channel_id, Shopify shop_domain, etc.)
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
deleted_at TIMESTAMP WITH TIME ZONE
);
CREATE INDEX idx_integration_hooks_account_id ON integration_hooks(account_id) WHERE deleted_at IS NULL;
CREATE INDEX idx_integration_hooks_inbox_id ON integration_hooks(inbox_id) WHERE deleted_at IS NULL;
CREATE INDEX idx_integration_hooks_hook_type ON integration_hooks(hook_type) WHERE deleted_at IS NULL;
CREATE UNIQUE INDEX idx_integration_hooks_access_token ON integration_hooks(access_token) WHERE deleted_at IS NULL AND access_token IS NOT NULL;
CREATE INDEX idx_integration_hooks_deleted_at ON integration_hooks(deleted_at);
-- IntegrationApp: catalog of supported integrations
-- Each app is keyed by hook_type (one app per provider type)
CREATE TABLE IF NOT EXISTS integration_apps (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description VARCHAR(512) DEFAULT '',
hook_type VARCHAR(50) NOT NULL, -- webhook/slack/shopify/linear/notion
icon VARCHAR(512) DEFAULT '', -- icon URL or CSS class
action_url VARCHAR(1024) DEFAULT '', -- frontend action URL for setup page
enabled BOOLEAN DEFAULT TRUE, -- whether the integration is available for installation
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
CREATE UNIQUE INDEX idx_integration_apps_hook_type ON integration_apps(hook_type);
-- Seed default integration apps
INSERT INTO integration_apps (name, description, hook_type, icon, action_url, enabled) VALUES
('Slack', 'Connect Slack channels for real-time notifications', 'slack', '/assets/integrations/slack.svg', '/app/accounts/{account_id}/integrations/slack', TRUE),
('Shopify', 'Sync Shopify orders and customer data', 'shopify', '/assets/integrations/shopify.svg', '/app/accounts/{account_id}/integrations/shopify', TRUE),
('Linear', 'Create Linear issues from conversations', 'linear', '/assets/integrations/linear.svg', '/app/accounts/{account_id}/integrations/linear', TRUE),
('Notion', 'Export conversation summaries to Notion pages', 'notion', '/assets/integrations/notion.svg', '/app/accounts/{account_id}/integrations/notion', TRUE),
('Webhook', 'Custom webhook for event-driven integrations', 'webhook', '/assets/integrations/webhook.svg', '/app/accounts/{account_id}/integrations/webhook', TRUE)
ON CONFLICT (hook_type) DO NOTHING;