Make database schema changes for Clarin CRM. Use when adding tables, columns, indexes, or modifying the PostgreSQL schema. Migrations live in database.go InitDB() function.
Migrations are SQL statements inside backend/pkg/database/database.go in the InitDB() function. They run on every backend startup, so they MUST be idempotent.
// In InitDB() at the end:
_, _ = db.Exec(ctx, `ALTER TABLE leads ADD COLUMN IF NOT EXISTS priority TEXT DEFAULT 'normal'`)
_, _ = db.Exec(ctx, `
CREATE TABLE IF NOT EXISTS campaigns (
id BIGSERIAL PRIMARY KEY,
account_id BIGINT NOT NULL REFERENCES accounts(id),
name TEXT NOT NULL,
status TEXT DEFAULT 'draft',
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
)
`)
_, _ = db.Exec(ctx, `CREATE INDEX IF NOT EXISTS idx_leads_phone ON leads(phone)`)
_, _ = db.Exec(ctx, `
CREATE TABLE IF NOT EXISTS lead_tags (
lead_id BIGINT NOT NULL REFERENCES leads(id) ON DELETE CASCADE,
tag_id BIGINT NOT NULL REFERENCES tags(id) ON DELETE CASCADE,
PRIMARY KEY (lead_id, tag_id)
)
`)
IF NOT EXISTS or ADD COLUMN IF NOT EXISTS — migrations run on every startup.domain/entities.go.repository/repository.go.docker compose build backend && docker compose up -ddocker compose logs --tail=30 backendaccounts — Multi-tenant accountsusers — Users per accountleads — CRM leads (synced from Kommo)contacts — CRM contacts (synced from Kommo)chats — WhatsApp conversationsmessages — Chat messagestags — Tags with UNIQUE(account_id, name) and kommo_idlead_tags, contact_tags, chat_tags — Junction tablesdevices — WhatsApp devicespipelines, pipeline_stages — Kanban pipelines