mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat(product): migrate category images to image_ids array
This commit is contained in:
parent
a6bca494f4
commit
68d39e75f8
@ -103,14 +103,14 @@ type CategoryProductRecord struct {
|
|||||||
|
|
||||||
// CategoryRecord is the persistence model for product.product_category.
|
// CategoryRecord is the persistence model for product.product_category.
|
||||||
type CategoryRecord struct {
|
type CategoryRecord struct {
|
||||||
ID string
|
ID string
|
||||||
NodeType int32
|
NodeType int32
|
||||||
Name string
|
Name string
|
||||||
ParentID string
|
ParentID string
|
||||||
Level int32
|
Level int32
|
||||||
ImageJSON string
|
ImageIDs []string
|
||||||
SortNum int32
|
SortNum int32
|
||||||
Status int32
|
Status int32
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProductRepository provides CRUD access to product tables only [ARC-PROD-001].
|
// ProductRepository provides CRUD access to product tables only [ARC-PROD-001].
|
||||||
@ -132,6 +132,36 @@ ALTER TABLE product.products
|
|||||||
ALTER TABLE product.product_category_bindings
|
ALTER TABLE product.product_category_bindings
|
||||||
ALTER COLUMN id DROP DEFAULT;
|
ALTER COLUMN id DROP DEFAULT;
|
||||||
|
|
||||||
|
ALTER TABLE product.product_category
|
||||||
|
ADD COLUMN IF NOT EXISTS image_ids VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[];
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_schema = 'product'
|
||||||
|
AND table_name = 'product_category'
|
||||||
|
AND column_name = 'image_json'
|
||||||
|
) THEN
|
||||||
|
EXECUTE $migration$
|
||||||
|
UPDATE product.product_category
|
||||||
|
SET image_ids = CASE
|
||||||
|
WHEN jsonb_typeof(image_json) = 'array' THEN COALESCE(
|
||||||
|
(SELECT array_agg(image_id)
|
||||||
|
FROM jsonb_array_elements_text(image_json) AS image_id),
|
||||||
|
'{}'::VARCHAR[]
|
||||||
|
)
|
||||||
|
ELSE '{}'::VARCHAR[]
|
||||||
|
END
|
||||||
|
WHERE image_json IS NOT NULL
|
||||||
|
$migration$;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
ALTER TABLE product.product_category
|
||||||
|
DROP COLUMN IF EXISTS image_json;
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS product.product_items (
|
CREATE TABLE IF NOT EXISTS product.product_items (
|
||||||
id BIGINT PRIMARY KEY,
|
id BIGINT PRIMARY KEY,
|
||||||
product_id BIGINT NOT NULL REFERENCES product.products(id) ON DELETE CASCADE,
|
product_id BIGINT NOT NULL REFERENCES product.products(id) ON DELETE CASCADE,
|
||||||
@ -563,7 +593,7 @@ SELECT id,
|
|||||||
name,
|
name,
|
||||||
parent_id,
|
parent_id,
|
||||||
level,
|
level,
|
||||||
image_json,
|
COALESCE(to_json(image_ids)::text, '[]'),
|
||||||
sort_num,
|
sort_num,
|
||||||
status
|
status
|
||||||
FROM product.product_category
|
FROM product.product_category
|
||||||
@ -599,7 +629,7 @@ SELECT id,
|
|||||||
name,
|
name,
|
||||||
parent_id,
|
parent_id,
|
||||||
level,
|
level,
|
||||||
image_json,
|
COALESCE(to_json(image_ids)::text, '[]'),
|
||||||
sort_num,
|
sort_num,
|
||||||
status
|
status
|
||||||
FROM product.product_category
|
FROM product.product_category
|
||||||
@ -927,11 +957,6 @@ func insertCategoryNode(ctx context.Context, tx *sql.Tx, category CategoryRecord
|
|||||||
parentID = sql.NullInt64{Int64: parsedParentID, Valid: true}
|
parentID = sql.NullInt64{Int64: parsedParentID, Valid: true}
|
||||||
}
|
}
|
||||||
|
|
||||||
imageJSON, err := normalizeCategoryImageJSON(category.ImageJSON)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
row := tx.QueryRowContext(ctx, `
|
row := tx.QueryRowContext(ctx, `
|
||||||
INSERT INTO product.product_category (
|
INSERT INTO product.product_category (
|
||||||
id,
|
id,
|
||||||
@ -939,20 +964,20 @@ INSERT INTO product.product_category (
|
|||||||
name,
|
name,
|
||||||
parent_id,
|
parent_id,
|
||||||
level,
|
level,
|
||||||
image_json,
|
image_ids,
|
||||||
sort_num,
|
sort_num,
|
||||||
status
|
status
|
||||||
)
|
)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7, $8)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||||
RETURNING id,
|
RETURNING id,
|
||||||
node_type,
|
node_type,
|
||||||
name,
|
name,
|
||||||
parent_id,
|
parent_id,
|
||||||
level,
|
level,
|
||||||
image_json,
|
COALESCE(to_json(image_ids)::text, '[]'),
|
||||||
sort_num,
|
sort_num,
|
||||||
status
|
status
|
||||||
`, id, int16(category.NodeType), category.Name, parentID, int16(category.Level), imageJSON, category.SortNum, int16(category.Status))
|
`, id, int16(category.NodeType), category.Name, parentID, int16(category.Level), stringSliceOrEmpty(category.ImageIDs), category.SortNum, int16(category.Status))
|
||||||
|
|
||||||
createdCategory, err := scanCategory(row)
|
createdCategory, err := scanCategory(row)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1251,16 +1276,16 @@ func scanInventory(scanner rowScanner) (*InventoryRecord, error) {
|
|||||||
|
|
||||||
func scanCategory(scanner rowScanner) (*CategoryRecord, error) {
|
func scanCategory(scanner rowScanner) (*CategoryRecord, error) {
|
||||||
var (
|
var (
|
||||||
rec CategoryRecord
|
rec CategoryRecord
|
||||||
id int64
|
id int64
|
||||||
parentID sql.NullInt64
|
parentID sql.NullInt64
|
||||||
imageJSON []byte
|
imageIDsJSON []byte
|
||||||
nodeType int16
|
nodeType int16
|
||||||
level int16
|
level int16
|
||||||
status int16
|
status int16
|
||||||
)
|
)
|
||||||
if err := scanner.Scan(
|
if err := scanner.Scan(
|
||||||
&id, &nodeType, &rec.Name, &parentID, &level, &imageJSON, &rec.SortNum, &status,
|
&id, &nodeType, &rec.Name, &parentID, &level, &imageIDsJSON, &rec.SortNum, &status,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, fmt.Errorf("scan category: %w", err)
|
return nil, fmt.Errorf("scan category: %w", err)
|
||||||
}
|
}
|
||||||
@ -1271,10 +1296,11 @@ func scanCategory(scanner rowScanner) (*CategoryRecord, error) {
|
|||||||
rec.ParentID = formatID(parentID.Int64)
|
rec.ParentID = formatID(parentID.Int64)
|
||||||
}
|
}
|
||||||
rec.Level = int32(level)
|
rec.Level = int32(level)
|
||||||
rec.ImageJSON = string(imageJSON)
|
imageIDs, err := parseStringArrayJSON(string(imageIDsJSON))
|
||||||
if rec.ImageJSON == "" {
|
if err != nil {
|
||||||
rec.ImageJSON = "[]"
|
return nil, fmt.Errorf("unmarshal category image_ids: %w", err)
|
||||||
}
|
}
|
||||||
|
rec.ImageIDs = imageIDs
|
||||||
rec.Status = int32(status)
|
rec.Status = int32(status)
|
||||||
return &rec, nil
|
return &rec, nil
|
||||||
}
|
}
|
||||||
@ -1289,17 +1315,6 @@ func normalizeAttributesJSON(raw string) ([]byte, error) {
|
|||||||
return []byte(raw), nil
|
return []byte(raw), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeCategoryImageJSON(raw string) ([]byte, error) {
|
|
||||||
raw = strings.TrimSpace(raw)
|
|
||||||
if raw == "" {
|
|
||||||
return []byte("[]"), nil
|
|
||||||
}
|
|
||||||
if !json.Valid([]byte(raw)) {
|
|
||||||
return nil, fmt.Errorf("invalid image_json")
|
|
||||||
}
|
|
||||||
return []byte(raw), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func productCrossBorderJSON(product ProductRecord) ([]byte, error) {
|
func productCrossBorderJSON(product ProductRecord) ([]byte, error) {
|
||||||
if strings.TrimSpace(product.CrossBorderJSON) != "" {
|
if strings.TrimSpace(product.CrossBorderJSON) != "" {
|
||||||
attrs := []byte(product.CrossBorderJSON)
|
attrs := []byte(product.CrossBorderJSON)
|
||||||
|
|||||||
31
internal/server/category_image_ids_test.go
Normal file
31
internal/server/category_image_ids_test.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"google.golang.org/protobuf/encoding/protojson"
|
||||||
|
|
||||||
|
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/repository"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCategoryImageIDsRoundTripThroughExistingProtoField(t *testing.T) {
|
||||||
|
imageIDs, err := normalizeNodeImageIDs(`[" image-1 ", "image-1", "image-2"]`)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("normalizeNodeImageIDs: %v", err)
|
||||||
|
}
|
||||||
|
if len(imageIDs) != 2 || imageIDs[0] != "image-1" || imageIDs[1] != "image-2" {
|
||||||
|
t.Fatalf("image ids = %#v", imageIDs)
|
||||||
|
}
|
||||||
|
|
||||||
|
node := toProtoCategoryNode(&repository.CategoryRecord{ImageIDs: imageIDs})
|
||||||
|
if got := node.GetImageJson(); got != `["image-1","image-2"]` {
|
||||||
|
t.Fatalf("image_json = %q", got)
|
||||||
|
}
|
||||||
|
if _, err := protojson.Marshal(node); err != nil {
|
||||||
|
t.Fatalf("marshal category node: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := normalizeNodeImageIDs(`{"image_id":"image-1"}`); err == nil {
|
||||||
|
t.Fatal("expected non-array image_json to be rejected")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -350,7 +350,7 @@ func categoryTreeFromCreateRequest(req *productv1.CreateCategoryTreeRequest) (re
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return repository.CategoryRecord{}, nil, err
|
return repository.CategoryRecord{}, nil, err
|
||||||
}
|
}
|
||||||
imageJSON, err := normalizeNodeImageJSON(req.GetImageJson())
|
imageIDs, err := normalizeNodeImageIDs(req.GetImageJson())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return repository.CategoryRecord{}, nil, err
|
return repository.CategoryRecord{}, nil, err
|
||||||
}
|
}
|
||||||
@ -360,12 +360,12 @@ func categoryTreeFromCreateRequest(req *productv1.CreateCategoryTreeRequest) (re
|
|||||||
}
|
}
|
||||||
|
|
||||||
parent := repository.CategoryRecord{
|
parent := repository.CategoryRecord{
|
||||||
NodeType: categoryNodeType,
|
NodeType: categoryNodeType,
|
||||||
Name: name,
|
Name: name,
|
||||||
Level: 1,
|
Level: 1,
|
||||||
ImageJSON: imageJSON,
|
ImageIDs: imageIDs,
|
||||||
SortNum: req.GetSortNum(),
|
SortNum: req.GetSortNum(),
|
||||||
Status: statusValue,
|
Status: statusValue,
|
||||||
}
|
}
|
||||||
|
|
||||||
children := make([]repository.CategoryRecord, 0, len(req.GetChildren()))
|
children := make([]repository.CategoryRecord, 0, len(req.GetChildren()))
|
||||||
@ -383,7 +383,7 @@ func categoryTreeFromCreateRequest(req *productv1.CreateCategoryTreeRequest) (re
|
|||||||
}
|
}
|
||||||
childNames[childName] = struct{}{}
|
childNames[childName] = struct{}{}
|
||||||
|
|
||||||
childImageJSON, err := normalizeNodeImageJSON(child.GetImageJson())
|
childImageIDs, err := normalizeNodeImageIDs(child.GetImageJson())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return repository.CategoryRecord{}, nil, err
|
return repository.CategoryRecord{}, nil, err
|
||||||
}
|
}
|
||||||
@ -393,12 +393,12 @@ func categoryTreeFromCreateRequest(req *productv1.CreateCategoryTreeRequest) (re
|
|||||||
}
|
}
|
||||||
|
|
||||||
children = append(children, repository.CategoryRecord{
|
children = append(children, repository.CategoryRecord{
|
||||||
NodeType: categoryNodeType,
|
NodeType: categoryNodeType,
|
||||||
Name: childName,
|
Name: childName,
|
||||||
Level: 2,
|
Level: 2,
|
||||||
ImageJSON: childImageJSON,
|
ImageIDs: childImageIDs,
|
||||||
SortNum: child.GetSortNum(),
|
SortNum: child.GetSortNum(),
|
||||||
Status: childStatus,
|
Status: childStatus,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -410,7 +410,7 @@ func groupFromCreateRequest(req *productv1.CreateGroupRequest) (repository.Categ
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return repository.CategoryRecord{}, err
|
return repository.CategoryRecord{}, err
|
||||||
}
|
}
|
||||||
imageJSON, err := normalizeNodeImageJSON(req.GetImageJson())
|
imageIDs, err := normalizeNodeImageIDs(req.GetImageJson())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return repository.CategoryRecord{}, err
|
return repository.CategoryRecord{}, err
|
||||||
}
|
}
|
||||||
@ -420,12 +420,12 @@ func groupFromCreateRequest(req *productv1.CreateGroupRequest) (repository.Categ
|
|||||||
}
|
}
|
||||||
|
|
||||||
return repository.CategoryRecord{
|
return repository.CategoryRecord{
|
||||||
NodeType: groupNodeType,
|
NodeType: groupNodeType,
|
||||||
Name: name,
|
Name: name,
|
||||||
Level: 1,
|
Level: 1,
|
||||||
ImageJSON: imageJSON,
|
ImageIDs: imageIDs,
|
||||||
SortNum: req.GetSortNum(),
|
SortNum: req.GetSortNum(),
|
||||||
Status: statusValue,
|
Status: statusValue,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -440,15 +440,16 @@ func normalizeNodeName(raw, field string) (string, error) {
|
|||||||
return name, nil
|
return name, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeNodeImageJSON(raw string) (string, error) {
|
func normalizeNodeImageIDs(raw string) ([]string, error) {
|
||||||
raw = strings.TrimSpace(raw)
|
raw = strings.TrimSpace(raw)
|
||||||
if raw == "" {
|
if raw == "" {
|
||||||
return "[]", nil
|
return []string{}, nil
|
||||||
}
|
}
|
||||||
if !json.Valid([]byte(raw)) {
|
var imageIDs []string
|
||||||
return "", errors.New("image_json must be valid json")
|
if err := json.Unmarshal([]byte(raw), &imageIDs); err != nil {
|
||||||
|
return nil, errors.New("image_json must be a json string array")
|
||||||
}
|
}
|
||||||
return raw, nil
|
return normalizeStringList(imageIDs), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeNodeStatus(statusValue *int32) (int32, error) {
|
func normalizeNodeStatus(statusValue *int32) (int32, error) {
|
||||||
@ -839,7 +840,7 @@ func toProtoCategoryNode(category *repository.CategoryRecord) *productv1.Categor
|
|||||||
Name: category.Name,
|
Name: category.Name,
|
||||||
ParentId: category.ParentID,
|
ParentId: category.ParentID,
|
||||||
Level: category.Level,
|
Level: category.Level,
|
||||||
ImageJson: category.ImageJSON,
|
ImageJson: categoryImageIDsJSON(category.ImageIDs),
|
||||||
SortNum: category.SortNum,
|
SortNum: category.SortNum,
|
||||||
Status: category.Status,
|
Status: category.Status,
|
||||||
Children: []*productv1.CategoryNode{},
|
Children: []*productv1.CategoryNode{},
|
||||||
@ -856,8 +857,16 @@ func toProtoGroupNode(group *repository.CategoryRecord) *productv1.GroupNode {
|
|||||||
Name: group.Name,
|
Name: group.Name,
|
||||||
ParentId: group.ParentID,
|
ParentId: group.ParentID,
|
||||||
Level: group.Level,
|
Level: group.Level,
|
||||||
ImageJson: group.ImageJSON,
|
ImageJson: categoryImageIDsJSON(group.ImageIDs),
|
||||||
SortNum: group.SortNum,
|
SortNum: group.SortNum,
|
||||||
Status: group.Status,
|
Status: group.Status,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func categoryImageIDsJSON(imageIDs []string) string {
|
||||||
|
encoded, err := json.Marshal(imageIDs)
|
||||||
|
if err != nil {
|
||||||
|
return "[]"
|
||||||
|
}
|
||||||
|
return string(encoded)
|
||||||
|
}
|
||||||
|
|||||||
35
scripts/sql/migrate-category-image-ids.sql
Normal file
35
scripts/sql/migrate-category-image-ids.sql
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
-- Migrate product.product_category.image_json (JSONB) to image_ids (VARCHAR[]).
|
||||||
|
-- Safe to run multiple times. The ids-svc-product startup schema check performs
|
||||||
|
-- the same migration for service-managed deployments.
|
||||||
|
|
||||||
|
ALTER TABLE product.product_category
|
||||||
|
ADD COLUMN IF NOT EXISTS image_ids VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[];
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_schema = 'product'
|
||||||
|
AND table_name = 'product_category'
|
||||||
|
AND column_name = 'image_json'
|
||||||
|
) THEN
|
||||||
|
EXECUTE $migration$
|
||||||
|
UPDATE product.product_category
|
||||||
|
SET image_ids = CASE
|
||||||
|
WHEN jsonb_typeof(image_json) = 'array' THEN COALESCE(
|
||||||
|
(SELECT array_agg(image_id)
|
||||||
|
FROM jsonb_array_elements_text(image_json) AS image_id),
|
||||||
|
'{}'::VARCHAR[]
|
||||||
|
)
|
||||||
|
ELSE '{}'::VARCHAR[]
|
||||||
|
END
|
||||||
|
WHERE image_json IS NOT NULL
|
||||||
|
$migration$;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
ALTER TABLE product.product_category
|
||||||
|
DROP COLUMN IF EXISTS image_json;
|
||||||
|
|
||||||
|
COMMENT ON COLUMN product.product_category.image_ids IS '图片 ID 列表,用于小程序分类入口、分组入口展示';
|
||||||
@ -111,7 +111,7 @@ CREATE TABLE IF NOT EXISTS product.product_category (
|
|||||||
name VARCHAR(100) NOT NULL,
|
name VARCHAR(100) NOT NULL,
|
||||||
parent_id BIGINT REFERENCES product.product_category(id),
|
parent_id BIGINT REFERENCES product.product_category(id),
|
||||||
level SMALLINT NOT NULL,
|
level SMALLINT NOT NULL,
|
||||||
image_json JSONB NOT NULL DEFAULT '[]'::JSONB,
|
image_ids VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[],
|
||||||
sort_num INTEGER NOT NULL DEFAULT 0,
|
sort_num INTEGER NOT NULL DEFAULT 0,
|
||||||
status SMALLINT NOT NULL DEFAULT 1,
|
status SMALLINT NOT NULL DEFAULT 1,
|
||||||
CONSTRAINT chk_product_category_node_type
|
CONSTRAINT chk_product_category_node_type
|
||||||
@ -128,7 +128,7 @@ COMMENT ON COLUMN product.product_category.node_type IS '节点类型:0 分类
|
|||||||
COMMENT ON COLUMN product.product_category.name IS '节点展示名称';
|
COMMENT ON COLUMN product.product_category.name IS '节点展示名称';
|
||||||
COMMENT ON COLUMN product.product_category.parent_id IS '上级节点 ID,顶级节点为空;子节点通过 parent_id 反向查询';
|
COMMENT ON COLUMN product.product_category.parent_id IS '上级节点 ID,顶级节点为空;子节点通过 parent_id 反向查询';
|
||||||
COMMENT ON COLUMN product.product_category.level IS '节点层级,如 1、2、3';
|
COMMENT ON COLUMN product.product_category.level IS '节点层级,如 1、2、3';
|
||||||
COMMENT ON COLUMN product.product_category.image_json IS '图片列表,用于小程序分类入口、分组入口展示';
|
COMMENT ON COLUMN product.product_category.image_ids IS '图片 ID 列表,用于小程序分类入口、分组入口展示';
|
||||||
COMMENT ON COLUMN product.product_category.sort_num IS '排序值,数值越小越靠前';
|
COMMENT ON COLUMN product.product_category.sort_num IS '排序值,数值越小越靠前';
|
||||||
COMMENT ON COLUMN product.product_category.status IS '节点状态:0 隐藏,1 显示';
|
COMMENT ON COLUMN product.product_category.status IS '节点状态:0 隐藏,1 显示';
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user