feat: add priority column to product_category and update related indexes

This commit is contained in:
robinj0726 2026-10-24 21:29:15 +08:00
parent 791e09b35b
commit 6074355b39
2 changed files with 12 additions and 2 deletions

View File

@ -529,8 +529,11 @@ CREATE TABLE IF NOT EXISTS product.product_category (
); );
-- CREATE TABLE IF NOT EXISTS does not alter a table created by an earlier -- CREATE TABLE IF NOT EXISTS does not alter a table created by an earlier
-- service version. The taxonomy is rebuilt before this version is deployed, -- service version. Add columns required by indexes / newer code before use.
-- so these mandatory source columns can be added without a legacy backfill. ALTER TABLE product.product_category
ADD COLUMN IF NOT EXISTS priority INTEGER NOT NULL DEFAULT 0;
-- Taxonomy is rebuilt before this version is deployed, so these mandatory
-- source columns can be added without a legacy backfill.
ALTER TABLE product.product_category ALTER TABLE product.product_category
ADD COLUMN IF NOT EXISTS platform_category_id BIGINT NOT NULL CHECK (platform_category_id > 0), ADD COLUMN IF NOT EXISTS platform_category_id BIGINT NOT NULL CHECK (platform_category_id > 0),
ADD COLUMN IF NOT EXISTS platform_parent_category_id BIGINT NOT NULL CHECK (platform_parent_category_id >= 0); ADD COLUMN IF NOT EXISTS platform_parent_category_id BIGINT NOT NULL CHECK (platform_parent_category_id >= 0);
@ -539,6 +542,8 @@ CREATE INDEX IF NOT EXISTS idx_product_category_active_parent_name
ON product.product_category (COALESCE(parent_id, 0), lower(btrim(name))) WHERE deleted_at IS NULL; ON product.product_category (COALESCE(parent_id, 0), lower(btrim(name))) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_product_category_active_parent_priority CREATE INDEX IF NOT EXISTS idx_product_category_active_parent_priority
ON product.product_category (parent_id, priority, id) WHERE deleted_at IS NULL; ON product.product_category (parent_id, priority, id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_product_category_active_status_parent_priority
ON product.product_category (status, parent_id, priority, id) WHERE deleted_at IS NULL;
CREATE UNIQUE INDEX IF NOT EXISTS uq_product_category_active_platform_category CREATE UNIQUE INDEX IF NOT EXISTS uq_product_category_active_platform_category
ON product.product_category (platform_category_id) WHERE deleted_at IS NULL; ON product.product_category (platform_category_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_product_category_active_platform_parent_category CREATE INDEX IF NOT EXISTS idx_product_category_active_platform_parent_category

View File

@ -205,6 +205,11 @@ COMMENT ON COLUMN product.product_category.platform_parent_category_id IS '外
COMMENT ON COLUMN product.product_category.has_children IS '上游声明的是否存在下级类目,不以本地直接子节点数量推断'; COMMENT ON COLUMN product.product_category.has_children IS '上游声明的是否存在下级类目,不以本地直接子节点数量推断';
COMMENT ON COLUMN product.product_category.deleted_at IS '软删除时间;NULL 表示未删除'; COMMENT ON COLUMN product.product_category.deleted_at IS '软删除时间;NULL 表示未删除';
-- Older installs may already have product_category without priority;
-- CREATE TABLE IF NOT EXISTS will not add it.
ALTER TABLE product.product_category
ADD COLUMN IF NOT EXISTS priority INTEGER NOT NULL DEFAULT 0;
CREATE INDEX IF NOT EXISTS idx_product_category_active_parent_name CREATE INDEX IF NOT EXISTS idx_product_category_active_parent_name
ON product.product_category (COALESCE(parent_id, 0), lower(btrim(name))) WHERE deleted_at IS NULL; ON product.product_category (COALESCE(parent_id, 0), lower(btrim(name))) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_product_category_active_parent_priority CREATE INDEX IF NOT EXISTS idx_product_category_active_parent_priority