Files
aura-crm/production/backend/migrations/001_init.sql
T

56 lines
2.2 KiB
SQL

-- +goose Up
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
CREATE TABLE clients (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
type TEXT NOT NULL CHECK (type IN ('individual', 'company')),
name TEXT NOT NULL,
phone TEXT NOT NULL,
email TEXT,
inn TEXT,
kpp TEXT,
company_address TEXT,
created_by_staff_id UUID NOT NULL,
created_by_staff_name TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX clients_phone_idx ON clients (phone);
CREATE TABLE orders (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
client_id UUID NOT NULL REFERENCES clients(id) ON DELETE RESTRICT,
tracking_token TEXT UNIQUE NOT NULL DEFAULT encode(gen_random_bytes(16), 'hex'),
device_type TEXT NOT NULL,
device_brand TEXT,
device_model TEXT,
serial_number TEXT,
problem_description TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'new' CHECK (status IN
('new', 'diagnosing', 'in_repair', 'waiting_parts', 'ready', 'completed', 'cancelled')),
assigned_master_id UUID,
assigned_master_name TEXT,
warranty_until DATE,
original_order_id UUID REFERENCES orders(id),
price_estimate NUMERIC(10, 2),
final_price NUMERIC(10, 2),
created_by_staff_id UUID NOT NULL,
created_by_staff_name TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX orders_client_id_idx ON orders (client_id);
CREATE INDEX orders_status_idx ON orders (status);
CREATE TABLE order_events (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
order_id UUID NOT NULL REFERENCES orders(id) ON DELETE CASCADE,
type TEXT NOT NULL CHECK (type IN ('status_change', 'comment', 'photo')),
body TEXT,
file_key TEXT,
is_public BOOLEAN NOT NULL DEFAULT FALSE,
staff_id UUID NOT NULL,
staff_name TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX order_events_order_id_idx ON order_events (order_id);