-- Ensure warehouse names and generated references stay unique in PostgreSQL.
CREATE UNIQUE INDEX IF NOT EXISTS ux_warehouses_name_ci
ON warehouses (LOWER(name));

CREATE UNIQUE INDEX IF NOT EXISTS ux_warehouses_number
ON warehouses ((NULLIF(TRIM(raw->>'warehouseNumber'), '')))
WHERE NULLIF(TRIM(raw->>'warehouseNumber'), '') IS NOT NULL;
