diff --git a/internal/repository/product_repository.go b/internal/repository/product_repository.go index dfb7e99..f4ea06d 100644 --- a/internal/repository/product_repository.go +++ b/internal/repository/product_repository.go @@ -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 --- service version. The taxonomy is rebuilt before this version is deployed, --- so these mandatory source columns can be added without a legacy backfill. +-- service version. Add columns required by indexes / newer code before use. +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 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); @@ -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; CREATE INDEX IF NOT EXISTS idx_product_category_active_parent_priority 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 ON product.product_category (platform_category_id) WHERE deleted_at IS NULL; CREATE INDEX IF NOT EXISTS idx_product_category_active_platform_parent_category diff --git a/scripts/sql/02-product-schema.sql b/scripts/sql/02-product-schema.sql index ceb6b43..2cf9e90 100644 --- a/scripts/sql/02-product-schema.sql +++ b/scripts/sql/02-product-schema.sql @@ -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.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 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