feat(publish): 正式表加发布状态与公开只读视图
This commit is contained in:
51
db/migrations/2026-09-22-01-single-table-publish.sql
Normal file
51
db/migrations/2026-09-22-01-single-table-publish.sql
Normal file
@ -0,0 +1,51 @@
|
||||
-- 单表发布模型(方案 2):取消草稿表,审核态由 status 承载,公开读走只读视图。
|
||||
--
|
||||
-- 本文件只做三件事:加列 / 存量行置已发布 / 建视图。幂等,可重复执行。
|
||||
-- 数据搬迁见 scripts/migrate_single_table;删草稿表见 2026-09-22-02-drop-draft-tables.sql。
|
||||
|
||||
-- 1) 正式表补审核态与溯源列。默认 'pending' 是刻意的(fail-closed):
|
||||
-- 任何漏赋值的行默认不可见,而不是意外对外发布。
|
||||
ALTER TABLE brand_runways
|
||||
ADD COLUMN IF NOT EXISTS status varchar(16) NOT NULL DEFAULT 'pending',
|
||||
ADD COLUMN IF NOT EXISTS job_id bigint NOT NULL DEFAULT 0,
|
||||
ADD COLUMN IF NOT EXISTS reviewer varchar(64) NOT NULL DEFAULT '',
|
||||
ADD COLUMN IF NOT EXISTS reject_reason varchar(255) NOT NULL DEFAULT '';
|
||||
|
||||
ALTER TABLE street_snaps
|
||||
ADD COLUMN IF NOT EXISTS status varchar(16) NOT NULL DEFAULT 'pending',
|
||||
ADD COLUMN IF NOT EXISTS job_id bigint NOT NULL DEFAULT 0,
|
||||
ADD COLUMN IF NOT EXISTS reviewer varchar(64) NOT NULL DEFAULT '',
|
||||
ADD COLUMN IF NOT EXISTS reject_reason varchar(255) NOT NULL DEFAULT '';
|
||||
|
||||
-- 2) 存量行置已发布**不在本文件里**:它是数据变更且只能执行一次,见同目录
|
||||
-- 2026-09-22-01b-publish-existing-rows.sql。
|
||||
-- 为什么必须拆开:数据搬迁(scripts/migrate_single_table)会把 pending 草稿连原 created_at
|
||||
-- 一起搬进正式表;若本文件含那条 UPDATE 且被重复执行(测试每次都会跑),
|
||||
-- 这些待审内容会被误刷成已发布 —— 恰好是本设计要防的泄漏。
|
||||
|
||||
-- 3) 状态索引:审核列表与公开视图都按 status 过滤。
|
||||
CREATE INDEX IF NOT EXISTS idx_br_status ON brand_runways (status);
|
||||
CREATE INDEX IF NOT EXISTS idx_ss_status ON street_snaps (status);
|
||||
|
||||
-- 4) 公开只读视图:把「必须是已发布且未删除」固化成结构性保证,公开查询漏写过滤成为不可能。
|
||||
-- 注意:视图列集被 SELECT * 冻结 —— 以后给这 4 张基表加列,必须同批重建视图。
|
||||
DROP VIEW IF EXISTS public_brand_runway_images;
|
||||
DROP VIEW IF EXISTS public_brand_runways;
|
||||
DROP VIEW IF EXISTS public_street_snap_images;
|
||||
DROP VIEW IF EXISTS public_street_snaps;
|
||||
|
||||
CREATE VIEW public_brand_runways AS
|
||||
SELECT * FROM brand_runways WHERE status = 'published' AND is_deleted = 0;
|
||||
|
||||
CREATE VIEW public_brand_runway_images AS
|
||||
SELECT i.* FROM brand_runway_images i
|
||||
JOIN brand_runways r ON r.id = i.runway_id
|
||||
WHERE i.is_deleted = 0 AND r.status = 'published' AND r.is_deleted = 0;
|
||||
|
||||
CREATE VIEW public_street_snaps AS
|
||||
SELECT * FROM street_snaps WHERE status = 'published' AND is_deleted = 0;
|
||||
|
||||
CREATE VIEW public_street_snap_images AS
|
||||
SELECT i.* FROM street_snap_images i
|
||||
JOIN street_snaps s ON s.id = i.snap_id
|
||||
WHERE i.is_deleted = 0 AND s.status = 'published' AND s.is_deleted = 0;
|
||||
9
db/migrations/2026-09-22-01b-publish-existing-rows.sql
Normal file
9
db/migrations/2026-09-22-01b-publish-existing-rows.sql
Normal file
@ -0,0 +1,9 @@
|
||||
-- 一次性数据迁移:把存量正式行视为已发布。
|
||||
--
|
||||
-- ⚠️ 只执行一次;必须在部署本计划的新代码之前、且在 scripts/migrate_single_table 之前执行。
|
||||
-- 为什么单独成文件而不放进可重复执行的 2026-09-22-01:
|
||||
-- 数据搬迁会把 pending 草稿连同旧的 created_at 一起搬进正式表;
|
||||
-- 若这条 UPDATE 可被重复执行(集成测试每次都会跑 01 号文件),
|
||||
-- 这些待审内容会被误刷成 published —— 恰好是本设计要防的泄漏。
|
||||
UPDATE brand_runways SET status = 'published' WHERE status = 'pending';
|
||||
UPDATE street_snaps SET status = 'published' WHERE status = 'pending';
|
||||
17
internal/model/content_status.go
Normal file
17
internal/model/content_status.go
Normal file
@ -0,0 +1,17 @@
|
||||
package model
|
||||
|
||||
// 内容发布状态(单表发布模型)。
|
||||
//
|
||||
// 历史上「爬虫草稿」与「正式记录」是两张表,审核 = 从草稿晋升到正式表。
|
||||
// 现在合并为一张表,审核态由本字段承载:
|
||||
// - pending:刚入库,等待审核;公开视图不可见
|
||||
// - published:已通过,对外可见
|
||||
// - rejected:已驳回,留在库中备查;公开视图不可见
|
||||
//
|
||||
// 新增记录默认 pending(见各自模型的 gorm default),漏赋值的结果是「不可见」,
|
||||
// 即 fail-closed,而不是意外对外发布。
|
||||
const (
|
||||
StatusPending = "pending"
|
||||
StatusPublished = "published"
|
||||
StatusRejected = "rejected"
|
||||
)
|
||||
@ -22,6 +22,12 @@ type BrandRunway struct {
|
||||
CollectionType string `gorm:"column:collection_type;size:16;not null;default:''" json:"collection_type"` // rtw/menswear/couture/resort/pre_fall
|
||||
Season string `gorm:"column:season;size:8" json:"season"` // spring/fall/null
|
||||
SeasonCode string `gorm:"column:season_code;size:8;not null;default:''" json:"season_code"` // SS26/FW25/RES26/PF25
|
||||
|
||||
// 审核态与溯源(单表发布模型;列由 db/migrations/2026-09-22-01-single-table-publish.sql 添加)。
|
||||
Status string `gorm:"column:status;size:16;not null;default:'pending'" json:"status"`
|
||||
JobID uint32 `gorm:"column:job_id;not null;default:0" json:"job_id"` // 关联 ingest_jobs.id(重跑溯源)
|
||||
Reviewer string `gorm:"column:reviewer;size:64;not null;default:''" json:"reviewer"` // 审核人(admin 账号)
|
||||
RejectReason string `gorm:"column:reject_reason;size:255;not null;default:''" json:"reject_reason"`
|
||||
}
|
||||
|
||||
// TableName 指定表名。
|
||||
@ -43,6 +49,7 @@ type RunwayRow struct {
|
||||
CollectionType string `gorm:"column:collection_type"`
|
||||
Season string `gorm:"column:season"`
|
||||
SeasonCode string `gorm:"column:season_code"`
|
||||
Status string `gorm:"column:status"`
|
||||
CreatedAt uint32 `gorm:"column:created_at"`
|
||||
BrandNameEn string `gorm:"column:brand_name_en"`
|
||||
BrandNameCn string `gorm:"column:brand_name_cn"`
|
||||
|
||||
@ -15,7 +15,14 @@ type StreetSnap struct {
|
||||
ImageCount uint16 `gorm:"column:image_count" json:"image_count"`
|
||||
IsDeleted uint8 `gorm:"column:is_deleted" json:"is_deleted"`
|
||||
CreatedAt uint32 `gorm:"column:created_at" json:"created_at"`
|
||||
UpdatedAt uint32 `gorm:"column:updated_at" json:"updated_at"`
|
||||
|
||||
// 审核态与溯源(单表发布模型;列由 db/migrations/2026-09-22-01-single-table-publish.sql 添加)。
|
||||
Status string `gorm:"column:status;size:16;not null;default:'pending'" json:"status"`
|
||||
JobID uint32 `gorm:"column:job_id;not null;default:0" json:"job_id"`
|
||||
Reviewer string `gorm:"column:reviewer;size:64;not null;default:''" json:"reviewer"`
|
||||
RejectReason string `gorm:"column:reject_reason;size:255;not null;default:''" json:"reject_reason"`
|
||||
|
||||
UpdatedAt uint32 `gorm:"column:updated_at" json:"updated_at"`
|
||||
}
|
||||
|
||||
// TableName 指定主表名。
|
||||
|
||||
133
internal/repository/publish_status_integration_test.go
Normal file
133
internal/repository/publish_status_integration_test.go
Normal file
@ -0,0 +1,133 @@
|
||||
//go:build integration
|
||||
|
||||
// 集成测试:公开视图必须屏蔽未发布内容 —— 这是「单表发布模型」的核心保证。
|
||||
//
|
||||
// 运行(库需先就绪,见 internal/repository/dedup_integration_test.go 顶部说明):
|
||||
//
|
||||
// go test -tags integration ./internal/repository/ -run TestPublicViews -v
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"fashionapi/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// applyMigration 读取并执行 db/migrations 下的迁移文件。
|
||||
// 迁移 SQL 因此每次测试都被真实跑一遍:语法错、视图列集漂移会立刻暴露。
|
||||
// 迁移文件必须幂等,重复调用安全。
|
||||
func applyMigration(t *testing.T, db *gorm.DB, name string) {
|
||||
t.Helper()
|
||||
path := filepath.Join("..", "..", "db", "migrations", name)
|
||||
sqlBytes, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("读取迁移失败 %s: %v", path, err)
|
||||
}
|
||||
if err := db.Exec(string(sqlBytes)).Error; err != nil {
|
||||
t.Fatalf("执行迁移失败 %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// seedRunway 插入一条指定状态的走秀记录 + 一张主图,返回记录 id。
|
||||
// year 用测试专用值(入参),便于列表按年份隔离断言;created_at 取未来时间戳,
|
||||
// 保证不会被迁移里「存量行置已发布」的时间戳护栏误刷。
|
||||
func seedRunway(t *testing.T, db *gorm.DB, status string, year uint16) uint32 {
|
||||
t.Helper()
|
||||
now := uint32(1_800_000_000)
|
||||
rw := model.BrandRunway{TitleEn: "view-test", Year: year, Status: status, CreatedAt: now, UpdatedAt: now}
|
||||
if err := db.Create(&rw).Error; err != nil {
|
||||
t.Fatalf("插入走秀失败: %v", err)
|
||||
}
|
||||
img := model.BrandRunwayImage{
|
||||
RunwayID: rw.ID, BrandID: 1, Image: "view-test.jpg", Name: "look",
|
||||
SortOrder: 1, CreatedAt: now, UpdatedAt: now,
|
||||
}
|
||||
if err := db.Create(&img).Error; err != nil {
|
||||
t.Fatalf("插入走秀图片失败: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
db.Exec("DELETE FROM brand_runway_images WHERE runway_id = ?", rw.ID)
|
||||
db.Exec("DELETE FROM brand_runways WHERE id = ?", rw.ID)
|
||||
})
|
||||
return rw.ID
|
||||
}
|
||||
|
||||
// seedStreetSnap 插入一条指定状态的街拍记录 + 一张图,返回记录 id。
|
||||
func seedStreetSnap(t *testing.T, db *gorm.DB, status string, year uint16) uint32 {
|
||||
t.Helper()
|
||||
now := uint32(1_800_000_000)
|
||||
snap := model.StreetSnap{Title: "view-test", City: "ViewTestCity", Year: year, Status: status, CreatedAt: now, UpdatedAt: now}
|
||||
if err := db.Create(&snap).Error; err != nil {
|
||||
t.Fatalf("插入街拍失败: %v", err)
|
||||
}
|
||||
img := model.StreetSnapImage{SnapID: snap.ID, Image: "view-test.jpg", Name: "look", SortOrder: 1, CreatedAt: now, UpdatedAt: now}
|
||||
if err := db.Create(&img).Error; err != nil {
|
||||
t.Fatalf("插入街拍图片失败: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
db.Exec("DELETE FROM street_snap_images WHERE snap_id = ?", snap.ID)
|
||||
db.Exec("DELETE FROM street_snaps WHERE id = ?", snap.ID)
|
||||
})
|
||||
return snap.ID
|
||||
}
|
||||
|
||||
// TestPublicViewsHideUnpublished 走秀:pending / rejected 的记录与图片都不得出现在公开视图,published 才可见。
|
||||
func TestPublicViewsHideUnpublished(t *testing.T) {
|
||||
db := testDB(t)
|
||||
applyMigration(t, db, "2026-09-22-01-single-table-publish.sql")
|
||||
ctx := context.Background()
|
||||
|
||||
id := seedRunway(t, db, model.StatusPending, 1999)
|
||||
|
||||
count := func() (records, images int64) {
|
||||
db.WithContext(ctx).Table("public_brand_runways").Where("id = ?", id).Count(&records)
|
||||
db.WithContext(ctx).Table("public_brand_runway_images").Where("runway_id = ?", id).Count(&images)
|
||||
return
|
||||
}
|
||||
|
||||
if r, i := count(); r != 0 || i != 0 {
|
||||
t.Fatalf("pending 不应出现在公开视图,实际 records=%d images=%d", r, i)
|
||||
}
|
||||
if err := db.Model(&model.BrandRunway{}).Where("id = ?", id).Update("status", model.StatusPublished).Error; err != nil {
|
||||
t.Fatalf("置为已发布失败: %v", err)
|
||||
}
|
||||
if r, i := count(); r != 1 || i != 1 {
|
||||
t.Fatalf("published 应出现在公开视图,实际 records=%d images=%d", r, i)
|
||||
}
|
||||
if err := db.Model(&model.BrandRunway{}).Where("id = ?", id).Update("status", model.StatusRejected).Error; err != nil {
|
||||
t.Fatalf("置为已驳回失败: %v", err)
|
||||
}
|
||||
if r, i := count(); r != 0 || i != 0 {
|
||||
t.Fatalf("rejected 不应出现在公开视图,实际 records=%d images=%d", r, i)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPublicViewsHideUnpublishedStreet 街拍:同上,覆盖另外两个视图。
|
||||
func TestPublicViewsHideUnpublishedStreet(t *testing.T) {
|
||||
db := testDB(t)
|
||||
applyMigration(t, db, "2026-09-22-01-single-table-publish.sql")
|
||||
ctx := context.Background()
|
||||
|
||||
id := seedStreetSnap(t, db, model.StatusPending, 1998)
|
||||
|
||||
count := func() (records, images int64) {
|
||||
db.WithContext(ctx).Table("public_street_snaps").Where("id = ?", id).Count(&records)
|
||||
db.WithContext(ctx).Table("public_street_snap_images").Where("snap_id = ?", id).Count(&images)
|
||||
return
|
||||
}
|
||||
|
||||
if r, i := count(); r != 0 || i != 0 {
|
||||
t.Fatalf("pending 不应出现在公开视图,实际 records=%d images=%d", r, i)
|
||||
}
|
||||
if err := db.Model(&model.StreetSnap{}).Where("id = ?", id).Update("status", model.StatusPublished).Error; err != nil {
|
||||
t.Fatalf("置为已发布失败: %v", err)
|
||||
}
|
||||
if r, i := count(); r != 1 || i != 1 {
|
||||
t.Fatalf("published 应出现在公开视图,实际 records=%d images=%d", r, i)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user