Files
gochat/backend/migrations/000008_add_search_indexes.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

37 lines
2.1 KiB
SQL

-- M14: Global Search + Advanced Filter — performance indexes
-- Adds indexes to support ILIKE text search and advanced filter queries
-- across conversations, messages, and contacts.
-- Conversations: text search on status, labels, and account filtering
CREATE INDEX IF NOT EXISTS idx_conversations_account_status ON conversations (account_id, status);
CREATE INDEX IF NOT EXISTS idx_conversations_account_assignee ON conversations (account_id, assignee_id);
CREATE INDEX IF NOT EXISTS idx_conversations_account_priority ON conversations (account_id, priority);
CREATE INDEX IF NOT EXISTS idx_conversations_account_inbox ON conversations (account_id, inbox_id);
CREATE INDEX IF NOT EXISTS idx_conversations_labels ON conversations (account_id, labels);
-- Messages: text search on content, filter by type/sender/private
CREATE INDEX IF NOT EXISTS idx_messages_account_type ON messages (account_id, message_type);
CREATE INDEX IF NOT EXISTS idx_messages_account_sender ON messages (account_id, sender_type);
CREATE INDEX IF NOT EXISTS idx_messages_account_content_type ON messages (account_id, content_type);
CREATE INDEX IF NOT EXISTS idx_messages_account_private ON messages (account_id, private);
CREATE INDEX IF NOT EXISTS idx_messages_conversation_id ON messages (conversation_id);
-- Contacts: text search on name, email, phone; filter by source/source_id.
DO $$
BEGIN
IF EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'contacts' AND column_name = 'source'
) THEN
CREATE INDEX IF NOT EXISTS idx_contacts_account_source ON contacts (account_id, source);
ELSIF EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'contacts' AND column_name = 'source_id'
) THEN
CREATE INDEX IF NOT EXISTS idx_contacts_account_source_id ON contacts (account_id, source_id);
END IF;
END $$;
CREATE INDEX IF NOT EXISTS idx_contacts_name ON contacts (account_id, name);
CREATE INDEX IF NOT EXISTS idx_contacts_email ON contacts (account_id, email);
CREATE INDEX IF NOT EXISTS idx_contacts_phone ON contacts (account_id, phone_number);