Files
aura-crm/production/backend/migrations/059_cartridge_recurring_items.sql
T

36 lines
2.1 KiB
SQL

-- +goose Up
-- Identifies one physical, recurring-service cartridge across visits — the
-- thing a QR-code label gets stuck onto. Deliberately its own row rather
-- than reusing the (client_id, model, color) grouping cartridge_items'
-- Suggest() already surfaces: that grouping is explicitly a soft hint (see
-- 003_cartridges.sql's own doc comment — a client with a fleet of ten
-- identical cartridges would otherwise be indistinguishable). Printing a
-- QR onto one physical unit and scanning it back is exactly what makes the
-- identity unambiguous, so this table's row IS that identity, not a
-- lookup convenience.
CREATE TABLE cartridge_recurring_items (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
client_id UUID NOT NULL REFERENCES clients(id) ON DELETE RESTRICT,
cartridge_brand_id UUID REFERENCES cartridge_brands(id),
cartridge_model_id UUID REFERENCES cartridge_models(id),
model TEXT NOT NULL,
color TEXT NOT NULL DEFAULT 'black',
-- Plain text, not a UUID — printed as a QR and also typeable by hand as
-- a fallback if a scan fails. 8 chars from an unambiguous alphabet
-- (excludes 0/O, 1/I/L) generated in Go, see internal/cartridge/recurring.go.
code TEXT NOT NULL UNIQUE,
created_by_staff_id UUID NOT NULL,
created_by_staff_name TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX cartridge_recurring_items_client_id_idx ON cartridge_recurring_items (client_id);
-- Nullable: only cartridges someone bothered to label carry this link. A
-- repeat visit for the same physical cartridge reuses the same
-- recurring_item_id (see internal/cartridge/handler.go's Create), so this
-- column is what actually chains cartridge_items rows into one item's
-- history, not the (client_id, model, color) grouping.
ALTER TABLE cartridge_items ADD COLUMN recurring_item_id UUID REFERENCES cartridge_recurring_items(id);
CREATE INDEX cartridge_items_recurring_item_id_idx ON cartridge_items (recurring_item_id) WHERE recurring_item_id IS NOT NULL;