Files
vmall/apps/api/migrations/0029_pricing.sql
T
Chengdong ZhangandCursor 3188bf7672 feat(pricing): price configurable products before marketing
Versioned common and linear rules compute the payable price and snapshot it on the order, with the design reference for later strategies such as agreement pricing.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-18 14:25:06 +08:00

57 lines
2.0 KiB
SQL

-- Versioned pricing rules, configurable option groups, and order-item quote snapshots.
-- Existing products receive an immutable common rule so physical prices stay on the SKU.
CREATE TABLE pricing_rules (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
product_id UUID NOT NULL REFERENCES products (id) ON DELETE CASCADE,
version INT NOT NULL,
strategy TEXT NOT NULL CHECK (strategy IN ('common', 'linear')),
config JSONB NOT NULL,
created_by UUID REFERENCES users (id),
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (product_id, version)
);
ALTER TABLE products
ADD COLUMN active_pricing_rule_id UUID,
ADD COLUMN component_only BOOLEAN NOT NULL DEFAULT FALSE,
ADD COLUMN listing_price_minor BIGINT,
ADD COLUMN listing_currency TEXT;
INSERT INTO pricing_rules (product_id, version, strategy, config)
SELECT id, 1, 'common', '{"strategy":"common"}'::jsonb
FROM products;
UPDATE products p
SET active_pricing_rule_id = r.id
FROM pricing_rules r
WHERE r.product_id = p.id AND r.version = 1;
ALTER TABLE products
ADD CONSTRAINT products_active_pricing_rule_fk
FOREIGN KEY (active_pricing_rule_id) REFERENCES pricing_rules (id);
CREATE TABLE product_option_groups (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
product_id UUID NOT NULL REFERENCES products (id) ON DELETE CASCADE,
group_key TEXT NOT NULL,
group_type TEXT NOT NULL CHECK (group_type IN ('quantity', 'choice', 'component', 'term')),
label JSONB NOT NULL,
constraints JSONB NOT NULL DEFAULT '{}'::jsonb,
component_product_id UUID REFERENCES products (id),
sort_order INT NOT NULL,
UNIQUE (product_id, group_key)
);
ALTER TABLE skus
ADD COLUMN is_anchor BOOLEAN NOT NULL DEFAULT FALSE;
CREATE UNIQUE INDEX skus_one_anchor_per_product
ON skus (product_id)
WHERE is_anchor;
ALTER TABLE order_items
ADD COLUMN configuration JSONB,
ADD COLUMN quote_snapshot JSONB,
ADD COLUMN pricing_rule_id UUID REFERENCES pricing_rules (id);