ids-svc-product/scripts/sql/recreate-product-tables.sql

376 lines
19 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Manual one-shot SQL for recreating the 5 product-domain tables
-- without rebuilding the PostgreSQL data volume.
--
-- Tables:
-- product.products
-- product.catalog_category
-- product.product_catalog_bindings
-- product.product_category
-- product.product_category_bindings
-- product.product_items
-- product.product_item_inventory
--
-- Intended use:
-- 1. Drop the product tables manually if they still exist.
-- 2. Execute this file against the existing ids_ecommerce database.
--
-- Drop order, if needed:
-- DROP TABLE IF EXISTS product.product_item_inventory;
-- DROP TABLE IF EXISTS product.product_category_bindings;
-- DROP TABLE IF EXISTS product.product_catalog_bindings;
-- DROP TABLE IF EXISTS product.product_items;
-- DROP TABLE IF EXISTS product.product_category;
-- DROP TABLE IF EXISTS product.products;
--
-- Example from ids-svc-product/:
-- docker cp scripts/sql/recreate-product-tables.sql ids-postgres:/tmp/recreate-product-tables.sql
-- docker exec -it ids-postgres psql -U postgres -d ids_ecommerce -f /tmp/recreate-product-tables.sql
CREATE EXTENSION IF NOT EXISTS "pg_trgm";
CREATE SCHEMA IF NOT EXISTS product;
CREATE OR REPLACE FUNCTION public.set_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TABLE IF NOT EXISTS product.products (
id BIGINT PRIMARY KEY,
spu_no VARCHAR(100) NOT NULL,
title VARCHAR(200) NOT NULL,
selling_point VARCHAR(300),
brand_id BIGINT,
supplier_id BIGINT,
image_ids VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[],
detail_html TEXT,
member_discount_enabled BOOLEAN NOT NULL DEFAULT TRUE,
goods_type SMALLINT NOT NULL DEFAULT 1,
service_guarantees VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[],
status SMALLINT NOT NULL DEFAULT 1,
cross_border JSONB NOT NULL DEFAULT '{}'::JSONB,
category_properties JSONB NOT NULL DEFAULT '{}'::JSONB,
freight_charge_type SMALLINT NOT NULL DEFAULT 1,
freight_fixed_amount BIGINT NOT NULL DEFAULT 0,
freight_template_id BIGINT,
sale_time_type SMALLINT NOT NULL DEFAULT 1,
scheduled_sale_time TIMESTAMPTZ,
presale_type SMALLINT NOT NULL DEFAULT 0,
delivery_time_type SMALLINT NOT NULL DEFAULT 0,
delivery_start_at TIMESTAMPTZ,
delivery_after_paid_days INTEGER,
purchase_limit_quantity INTEGER NOT NULL DEFAULT 0,
version INTEGER NOT NULL DEFAULT 0,
deleted_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT chk_products_goods_type
CHECK (goods_type IN (1, 2, 3, 4)),
CONSTRAINT chk_products_status
CHECK (status IN (1, 2, 3)),
CONSTRAINT chk_products_freight_charge_type
CHECK (freight_charge_type IN (1, 2)),
CONSTRAINT chk_products_freight_fixed_amount
CHECK (freight_fixed_amount >= 0),
CONSTRAINT chk_products_freight_template_required
CHECK (freight_charge_type <> 2 OR freight_template_id IS NOT NULL),
CONSTRAINT chk_products_sale_time_type
CHECK (sale_time_type IN (1, 2)),
CONSTRAINT chk_products_scheduled_sale_time_required
CHECK (sale_time_type <> 2 OR scheduled_sale_time IS NOT NULL),
CONSTRAINT chk_products_presale_configuration
CHECK (
presale_type IN (0, 1, 2)
AND purchase_limit_quantity >= 0
AND (
(delivery_time_type = 0 AND delivery_start_at IS NULL AND delivery_after_paid_days IS NULL)
OR (delivery_time_type = 1 AND delivery_start_at IS NOT NULL AND delivery_after_paid_days IS NULL)
OR (delivery_time_type = 2 AND delivery_start_at IS NULL AND delivery_after_paid_days > 0)
)
),
CONSTRAINT chk_products_version
CHECK (version >= 0)
);
COMMENT ON TABLE product.products IS '商品 SPU 主表:存储商品公共信息、分类分组、品牌、供应商、图片、商品类型、运费、开售配置和软删除状态';
COMMENT ON COLUMN product.products.service_guarantees IS '服务保障展示快照;服务保障主数据变更时需通过业务流程刷新';
COMMENT ON COLUMN product.products.detail_html IS '商品详情 HTML;写入或渲染前必须做 HTML Sanitization,列表接口不得读取该字段';
COMMENT ON COLUMN product.products.version IS '乐观锁版本号,用于后台并发编辑冲突检测';
COMMENT ON COLUMN product.products.deleted_at IS '软删除时间;NULL 表示未删除,商品禁止物理删除以保护订单和财务引用';
CREATE INDEX IF NOT EXISTS idx_products_title_trgm
ON product.products USING GIN (title gin_trgm_ops);
CREATE INDEX IF NOT EXISTS idx_products_brand_status
ON product.products (brand_id, status);
CREATE INDEX IF NOT EXISTS idx_products_status_goods_type
ON product.products (status, goods_type)
WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_products_supplier_id
ON product.products (supplier_id);
CREATE INDEX IF NOT EXISTS idx_products_goods_type
ON product.products (goods_type);
CREATE INDEX IF NOT EXISTS idx_products_deleted_at
ON product.products (deleted_at);
CREATE INDEX IF NOT EXISTS idx_products_active_created_id
ON product.products (created_at DESC, id DESC)
WHERE deleted_at IS NULL;
CREATE UNIQUE INDEX IF NOT EXISTS uq_products_spu_no
ON product.products (spu_no)
WHERE deleted_at IS NULL AND spu_no <> '';
CREATE TABLE IF NOT EXISTS product.catalog_category (
id BIGINT PRIMARY KEY,
node_type SMALLINT NOT NULL DEFAULT 1,
name VARCHAR(100) NOT NULL,
parent_id BIGINT REFERENCES product.catalog_category(id),
level SMALLINT NOT NULL,
image_ids VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[],
sort_num INTEGER NOT NULL DEFAULT 0,
status SMALLINT NOT NULL DEFAULT 1,
version INTEGER NOT NULL DEFAULT 0,
tenant_id BIGINT,
store_id BIGINT,
campaign_id BIGINT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMPTZ,
CONSTRAINT chk_product_category_node_type
CHECK (node_type IN (1, 2)),
CONSTRAINT chk_product_category_level
CHECK (level > 0),
CONSTRAINT chk_product_category_status
CHECK (status IN (0, 1)),
CONSTRAINT chk_product_category_version
CHECK (version >= 0)
);
COMMENT ON TABLE product.catalog_category IS '商品目录分类表:存储原商品分类和商品分组,通过 node_type 区分分类或分组';
COMMENT ON COLUMN product.catalog_category.id IS '类别/分组节点主键';
COMMENT ON COLUMN product.catalog_category.node_type IS '节点类型:1 分类,2 分组';
COMMENT ON COLUMN product.catalog_category.name IS '节点展示名称';
COMMENT ON COLUMN product.catalog_category.parent_id IS '上级节点 ID,顶级节点为空;子节点通过 parent_id 反向查询';
COMMENT ON COLUMN product.catalog_category.level IS '节点层级,如 1、2、3';
COMMENT ON COLUMN product.catalog_category.image_ids IS '图片 ID 列表,用于小程序分类入口、分组入口展示';
COMMENT ON COLUMN product.catalog_category.sort_num IS '排序值,数值越小越靠前';
COMMENT ON COLUMN product.catalog_category.status IS '节点状态:0 隐藏,1 显示';
COMMENT ON COLUMN product.catalog_category.version IS '节点乐观锁版本号,更新时递增';
CREATE INDEX IF NOT EXISTS idx_catalog_category_parent_id
ON product.catalog_category (parent_id);
CREATE INDEX IF NOT EXISTS idx_catalog_category_node_status_sort
ON product.catalog_category (node_type, status, sort_num) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_catalog_category_store_type_created_id
ON product.catalog_category (store_id, node_type, created_at DESC, id DESC)
WHERE deleted_at IS NULL;
CREATE TABLE IF NOT EXISTS product.product_catalog_bindings (
id BIGINT PRIMARY KEY,
product_id BIGINT NOT NULL REFERENCES product.products(id) ON DELETE CASCADE,
category_id BIGINT NOT NULL REFERENCES product.catalog_category(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
COMMENT ON TABLE product.product_catalog_bindings IS '商品目录分类/分组绑定表:存储商品 SPU 与分类/分组节点的多对多绑定关系';
COMMENT ON COLUMN product.product_catalog_bindings.id IS '商品类别/分组绑定主键';
COMMENT ON COLUMN product.product_catalog_bindings.product_id IS '绑定商品 SPU ID,对应 product.products.id';
COMMENT ON COLUMN product.product_catalog_bindings.category_id IS '绑定分类/分组节点 ID,对应 product.catalog_category.id';
CREATE INDEX IF NOT EXISTS idx_product_catalog_bindings_category_id
ON product.product_catalog_bindings (category_id) WHERE deleted_at IS NULL;
CREATE UNIQUE INDEX IF NOT EXISTS uq_product_catalog_bindings_active_product_category
ON product.product_catalog_bindings (product_id, category_id) WHERE deleted_at IS NULL;
CREATE TABLE IF NOT EXISTS product.product_category (
id BIGINT PRIMARY KEY,
tenant_id BIGINT NOT NULL,
store_id BIGINT NOT NULL,
category_code VARCHAR(64) NOT NULL,
name VARCHAR(100) NOT NULL,
parent_id BIGINT REFERENCES product.product_category(id) ON DELETE RESTRICT,
level SMALLINT NOT NULL CHECK (level BETWEEN 1 AND 4),
is_leaf BOOLEAN NOT NULL DEFAULT TRUE,
sort_num INTEGER NOT NULL DEFAULT 0,
status SMALLINT NOT NULL DEFAULT 1 CHECK (status IN (0, 1)),
remark VARCHAR(500) NOT NULL DEFAULT '',
version INTEGER NOT NULL DEFAULT 0 CHECK (version >= 0),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
CREATE UNIQUE INDEX IF NOT EXISTS uq_product_category_store_code_active
ON product.product_category (store_id, category_code) WHERE deleted_at IS NULL;
CREATE UNIQUE INDEX IF NOT EXISTS uq_product_category_store_parent_name_active
ON product.product_category (store_id, COALESCE(parent_id, 0), lower(btrim(name))) WHERE deleted_at IS NULL;
CREATE TABLE IF NOT EXISTS product.product_category_bindings (
id BIGINT PRIMARY KEY,
tenant_id BIGINT NOT NULL,
store_id BIGINT NOT NULL,
product_id BIGINT NOT NULL REFERENCES product.products(id) ON DELETE CASCADE,
product_category_id BIGINT NOT NULL REFERENCES product.product_category(id) ON DELETE RESTRICT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
CREATE UNIQUE INDEX IF NOT EXISTS uq_product_category_bindings_active
ON product.product_category_bindings (product_id, product_category_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_product_category_bindings_category_product
ON product.product_category_bindings (store_id, product_category_id, product_id) WHERE deleted_at IS NULL;
CREATE TABLE IF NOT EXISTS product.product_items (
id BIGINT PRIMARY KEY,
product_id BIGINT NOT NULL REFERENCES product.products(id) ON DELETE CASCADE,
sku_no VARCHAR(100),
name VARCHAR(256) NOT NULL,
price BIGINT NOT NULL CHECK (price >= 0),
supply_price BIGINT CONSTRAINT chk_product_items_supply_price CHECK (supply_price >= 0),
image_ids VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[],
attributes JSONB NOT NULL DEFAULT '{}',
status SMALLINT NOT NULL DEFAULT 1 CHECK (status IN (0, 1)),
is_archived BOOLEAN NOT NULL DEFAULT FALSE,
archived_at TIMESTAMPTZ,
version INTEGER NOT NULL DEFAULT 0 CHECK (version >= 0),
deleted_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT chk_product_items_archived
CHECK ((is_archived = FALSE AND archived_at IS NULL) OR (is_archived = TRUE AND archived_at IS NOT NULL))
);
COMMENT ON TABLE product.product_items IS '商品 SKU 明细表:存储具体可售规格、价格和规格属性;库存统一存储在 product.product_item_inventory';
COMMENT ON COLUMN product.product_items.price IS 'SKU 售价,单位:分';
COMMENT ON COLUMN product.product_items.supply_price IS 'SKU 供货价,单位:分;NULL 表示未维护';
COMMENT ON COLUMN product.product_items.image_ids IS 'SKU 图片 ID 列表';
COMMENT ON COLUMN product.product_items.attributes IS 'SKU 规格属性 JSONB,如颜色、尺寸等';
COMMENT ON COLUMN product.product_items.status IS 'SKU 状态:0 停用,1 启用';
COMMENT ON COLUMN product.product_items.is_archived IS 'SKU 是否归档;归档 SKU 不参与当前运营、展示和新订单销售';
COMMENT ON COLUMN product.product_items.archived_at IS 'SKU 归档时间;归档后仅保留供历史订单结算与展示';
COMMENT ON COLUMN product.product_items.version IS 'SKU 乐观锁版本号';
COMMENT ON COLUMN product.product_items.deleted_at IS 'SKU 软删除时间;NULL 表示未删除';
CREATE INDEX IF NOT EXISTS idx_product_items_product_id
ON product.product_items (product_id)
WHERE deleted_at IS NULL AND is_archived = FALSE;
CREATE INDEX IF NOT EXISTS idx_product_items_price
ON product.product_items (price)
WHERE deleted_at IS NULL;
-- SKU business codes are not unique; use the SKU primary key for identity.
CREATE TABLE IF NOT EXISTS product.product_item_inventory (
id BIGINT PRIMARY KEY,
sku_id BIGINT NOT NULL REFERENCES product.product_items(id) ON DELETE CASCADE,
spu_no VARCHAR(100),
sku_no VARCHAR(100),
stock_quantity INTEGER NOT NULL DEFAULT 0 CHECK (stock_quantity >= 0),
locked_quantity INTEGER NOT NULL DEFAULT 0 CHECK (locked_quantity >= 0),
sold_quantity INTEGER NOT NULL DEFAULT 0 CHECK (sold_quantity >= 0),
version INTEGER NOT NULL DEFAULT 0 CHECK (version >= 0),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT uq_product_item_inventory_sku_id UNIQUE (sku_id)
);
CREATE TABLE IF NOT EXISTS product.inventory_adjustments (
order_id BIGINT NOT NULL,
action VARCHAR(20) NOT NULL,
sku_id BIGINT NOT NULL REFERENCES product.product_items(id) ON DELETE CASCADE,
quantity INTEGER NOT NULL CHECK (quantity > 0),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
PRIMARY KEY (order_id, action, sku_id),
CONSTRAINT chk_inventory_adjustments_action CHECK (action IN ('confirm', 'release'))
);
COMMENT ON TABLE product.product_item_inventory IS '商品 SKU 库存销量表:存储库存池、锁定库存和已售数量';
COMMENT ON COLUMN product.product_item_inventory.id IS '库存记录主键,由商品服务应用层生成雪花 ID';
COMMENT ON COLUMN product.product_item_inventory.spu_no IS '商品 SPU 编码冗余字段,用于库存查询和对账';
COMMENT ON COLUMN product.product_item_inventory.sku_no IS '商品 SKU 编码冗余字段,用于库存查询和对账';
COMMENT ON COLUMN product.product_item_inventory.stock_quantity IS 'SKU 当前库存池数量,净可售=stock_quantity-locked_quantity';
COMMENT ON COLUMN product.product_item_inventory.locked_quantity IS '待支付订单锁定库存';
COMMENT ON COLUMN product.product_item_inventory.sold_quantity IS '支付成功后的累计销量';
COMMENT ON COLUMN product.product_item_inventory.version IS '库存乐观锁版本号';
COMMENT ON TABLE product.inventory_adjustments IS '订单库存动作幂等表:记录确认成交和释放锁定库存';
COMMENT ON COLUMN product.inventory_adjustments.action IS '库存动作:confirm 支付成功确认成交;release 待支付取消/超时释放锁定';
CREATE TABLE IF NOT EXISTS product.suppliers (
id BIGINT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
alias VARCHAR(100) NOT NULL DEFAULT '',
supplier_code VARCHAR(64) NOT NULL,
attachments JSONB NOT NULL DEFAULT '[]'::JSONB,
contact_name VARCHAR(100) NOT NULL DEFAULT '',
contact_phone VARCHAR(32) NOT NULL DEFAULT '',
remark VARCHAR(1000) NOT NULL DEFAULT '',
status SMALLINT NOT NULL DEFAULT 1 CHECK (status IN (0, 1)),
tenant_id BIGINT NOT NULL,
store_id BIGINT NOT NULL,
version INTEGER NOT NULL DEFAULT 0 CHECK (version >= 0),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
CREATE UNIQUE INDEX IF NOT EXISTS uq_suppliers_store_code_active ON product.suppliers (store_id, supplier_code) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_suppliers_store_list ON product.suppliers (store_id, created_at DESC, id DESC) WHERE deleted_at IS NULL;
CREATE TABLE IF NOT EXISTS product.brands (
id BIGINT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
alias VARCHAR(100) NOT NULL DEFAULT '',
brand_code VARCHAR(64) NOT NULL,
supplier_id BIGINT NOT NULL,
status SMALLINT NOT NULL DEFAULT 1 CHECK (status IN (0, 1)),
tenant_id BIGINT NOT NULL,
store_id BIGINT NOT NULL,
version INTEGER NOT NULL DEFAULT 0 CHECK (version >= 0),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
CREATE UNIQUE INDEX IF NOT EXISTS uq_brands_store_code_active ON product.brands (store_id, brand_code) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_brands_store_list ON product.brands (store_id, created_at DESC, id DESC) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_brands_supplier ON product.brands (store_id, supplier_id) WHERE deleted_at IS NULL;
DROP TRIGGER IF EXISTS set_suppliers_updated_at ON product.suppliers;
CREATE TRIGGER set_suppliers_updated_at BEFORE UPDATE ON product.suppliers FOR EACH ROW EXECUTE FUNCTION public.set_updated_at();
DROP TRIGGER IF EXISTS set_brands_updated_at ON product.brands;
CREATE TRIGGER set_brands_updated_at BEFORE UPDATE ON product.brands FOR EACH ROW EXECUTE FUNCTION public.set_updated_at();
DROP TRIGGER IF EXISTS set_products_updated_at ON product.products;
CREATE TRIGGER set_products_updated_at
BEFORE UPDATE ON product.products
FOR EACH ROW EXECUTE FUNCTION public.set_updated_at();
DROP TRIGGER IF EXISTS set_product_items_updated_at ON product.product_items;
CREATE TRIGGER set_product_items_updated_at
BEFORE UPDATE ON product.product_items
FOR EACH ROW EXECUTE FUNCTION public.set_updated_at();
DROP TRIGGER IF EXISTS set_product_item_inventory_updated_at ON product.product_item_inventory;
CREATE TRIGGER set_product_item_inventory_updated_at
BEFORE UPDATE ON product.product_item_inventory
FOR EACH ROW EXECUTE FUNCTION public.set_updated_at();
GRANT SELECT, INSERT, UPDATE, DELETE ON product.products TO svc_product;
GRANT SELECT, INSERT, UPDATE, DELETE ON product.catalog_category TO svc_product;
GRANT SELECT, INSERT, UPDATE, DELETE ON product.product_catalog_bindings TO svc_product;
GRANT SELECT, INSERT, UPDATE, DELETE ON product.product_category TO svc_product;
GRANT SELECT, INSERT, UPDATE, DELETE ON product.product_category_bindings TO svc_product;
GRANT SELECT, INSERT, UPDATE, DELETE ON product.product_items TO svc_product;
GRANT SELECT, INSERT, UPDATE, DELETE ON product.product_item_inventory TO svc_product;
GRANT SELECT, INSERT, UPDATE, DELETE ON product.inventory_adjustments TO svc_product;
GRANT SELECT, INSERT, UPDATE, DELETE ON product.suppliers TO svc_product;
GRANT SELECT, INSERT, UPDATE, DELETE ON product.brands TO svc_product;