完善商品发布位置与 Docker 运行环境

This commit is contained in:
Christ committed 2026-08-13 09:12:22 +08:00
1 parent 9adc77df73
commit 7ec0b4a078
21 files changed
+431 -176

No files matched your search

+13 -9
View File
@@ -25,6 +25,7 @@ type ItemPublishBatch struct {
DefaultCookieID string // 默认发布账号(明细行未指定账号时回退到此)
Filename string // 原始上传文件名
UploadDir string // 图片资源目录(发布时读取商品图片的根目录)
LocationJSON string // 批次统一使用的发货地 JSON
Status string // 批次状态:pending/running/completed/partially_failed/failed
TotalCount int // 明细行总数(Recount 维护)
SuccessCount int // 成功数(Recount 维护)
@@ -66,6 +67,9 @@ type ItemPublishBatchRow struct {
// 明细行的 quantity/postage_mode/status/images_json/raw_json/automation_json 缺省值在此补齐。
// total_count 取 len(rows),success/failed 初始为 0。
func (b *ItemPublishBatches) Create(ctx context.Context, batch *ItemPublishBatch, rows []ItemPublishBatchRow) error {
if strings.TrimSpace(batch.LocationJSON) == "" {
batch.LocationJSON = "{}"
}
tx, err := b.DB.BeginTx(ctx, nil)
if err != nil {
return err
@@ -73,10 +77,10 @@ func (b *ItemPublishBatches) Create(ctx context.Context, batch *ItemPublishBatch
defer tx.Rollback()
if _, err := tx.ExecContext(ctx,
`INSERT INTO item_publish_batches
(id,user_id,default_cookie_id,filename,upload_dir,status,total_count,success_count,failed_count)
VALUES (?,?,?,?,?,?,?,?,?)`,
(id,user_id,default_cookie_id,filename,upload_dir,location_json,status,total_count,success_count,failed_count)
VALUES (?,?,?,?,?,?,?,?,?,?)`,
batch.ID, batch.UserID, batch.DefaultCookieID, batch.Filename, batch.UploadDir,
batch.Status, len(rows), 0, 0); err != nil {
batch.LocationJSON, batch.Status, len(rows), 0, 0); err != nil {
return err
}
for _, row := range rows {
@@ -119,11 +123,11 @@ func (b *ItemPublishBatches) Create(ctx context.Context, batch *ItemPublishBatch
func (b *ItemPublishBatches) Get(ctx context.Context, userID int64, id string) (*ItemPublishBatch, error) {
var out ItemPublishBatch
err := b.DB.QueryRowContext(ctx,
`SELECT id,user_id,default_cookie_id,filename,upload_dir,status,total_count,success_count,failed_count,
`SELECT id,user_id,default_cookie_id,filename,upload_dir,COALESCE(location_json,'{}'),status,total_count,success_count,failed_count,
COALESCE(worker_token,''),COALESCE(lease_expires_at,0),
created_at,updated_at
FROM item_publish_batches WHERE id=? AND user_id=?`, id, userID).Scan(
&out.ID, &out.UserID, &out.DefaultCookieID, &out.Filename, &out.UploadDir, &out.Status,
&out.ID, &out.UserID, &out.DefaultCookieID, &out.Filename, &out.UploadDir, &out.LocationJSON, &out.Status,
&out.TotalCount, &out.SuccessCount, &out.FailedCount, &out.WorkerToken, &out.LeaseExpiresAt,
&out.CreatedAt, &out.UpdatedAt)
if err != nil {
@@ -140,7 +144,7 @@ func (b *ItemPublishBatches) ListForUser(ctx context.Context, userID int64, limi
if limit <= 0 || limit > 100 {
limit = 20
}
rows, err := b.DB.QueryContext(ctx, `SELECT id,user_id,default_cookie_id,filename,upload_dir,status,
rows, err := b.DB.QueryContext(ctx, `SELECT id,user_id,default_cookie_id,filename,upload_dir,COALESCE(location_json,'{}'),status,
total_count,success_count,failed_count,COALESCE(worker_token,''),COALESCE(lease_expires_at,0),created_at,updated_at
FROM item_publish_batches WHERE user_id=? ORDER BY created_at DESC,id DESC LIMIT ?`, userID, limit)
if err != nil {
@@ -150,7 +154,7 @@ func (b *ItemPublishBatches) ListForUser(ctx context.Context, userID int64, limi
var out []ItemPublishBatch
for rows.Next() {
var batch ItemPublishBatch
if err := rows.Scan(&batch.ID, &batch.UserID, &batch.DefaultCookieID, &batch.Filename, &batch.UploadDir,
if err := rows.Scan(&batch.ID, &batch.UserID, &batch.DefaultCookieID, &batch.Filename, &batch.UploadDir, &batch.LocationJSON,
&batch.Status, &batch.TotalCount, &batch.SuccessCount, &batch.FailedCount, &batch.WorkerToken,
&batch.LeaseExpiresAt, &batch.CreatedAt, &batch.UpdatedAt); err != nil {
return nil, err
@@ -165,7 +169,7 @@ func (b *ItemPublishBatches) Recoverable(ctx context.Context, now int64, limit i
if limit <= 0 {
limit = 20
}
rows, err := b.DB.QueryContext(ctx, `SELECT id,user_id,default_cookie_id,filename,upload_dir,status,
rows, err := b.DB.QueryContext(ctx, `SELECT id,user_id,default_cookie_id,filename,upload_dir,COALESCE(location_json,'{}'),status,
total_count,success_count,failed_count,COALESCE(worker_token,''),COALESCE(lease_expires_at,0),created_at,updated_at
FROM item_publish_batches b
WHERE (b.status IN ('running','canceling') AND (b.lease_expires_at=0 OR b.lease_expires_at<?))
@@ -179,7 +183,7 @@ func (b *ItemPublishBatches) Recoverable(ctx context.Context, now int64, limit i
var out []ItemPublishBatch
for rows.Next() {
var batch ItemPublishBatch
if err := rows.Scan(&batch.ID, &batch.UserID, &batch.DefaultCookieID, &batch.Filename, &batch.UploadDir,
if err := rows.Scan(&batch.ID, &batch.UserID, &batch.DefaultCookieID, &batch.Filename, &batch.UploadDir, &batch.LocationJSON,
&batch.Status, &batch.TotalCount, &batch.SuccessCount, &batch.FailedCount, &batch.WorkerToken,
&batch.LeaseExpiresAt, &batch.CreatedAt, &batch.UpdatedAt); err != nil {
return nil, err
+5 -1
View File
@@ -128,7 +128,7 @@ func TestLatestMigrationsDownUpSQLite(t *testing.T) {
}
goose.SetBaseFS(migrationsFS)
// 依次回滚最新版本到 13,再整体升级。
for i := 0; i < 13; i++ {
for i := 0; i < 14; i++ {
if err := goose.Down(d, "migrations/sqlite"); err != nil {
t.Fatalf("down migration #%d: %v", i+1, err)
}
@@ -151,6 +151,9 @@ func TestLatestMigrationsDownUpSQLite(t *testing.T) {
if columnExists(t, d, "item_info", "deleted_at") || columnExists(t, d, "automation_rules", "deleted_at") {
t.Fatal("soft-delete columns should be removed after migration 26 down")
}
if columnExists(t, d, "item_publish_batches", "location_json") {
t.Fatal("item_publish_batches.location_json should be removed after migration 27 down")
}
for _, table := range []string{"account_task_settings", "account_task_runs", "chat_sessions", "chat_messages"} {
if tableExists(t, d, table) {
t.Fatalf("table should be removed after migration 24 down: %s", table)
@@ -174,6 +177,7 @@ func TestLatestMigrationsDownUpSQLite(t *testing.T) {
{"default_reply_records", "text_sent"},
{"account_tokens", "cookie_fingerprint"},
{"item_publish_batch_rows", "category_json"},
{"item_publish_batches", "location_json"},
{"account_task_settings", "auto_rate_enabled"},
{"account_task_runs", "run_key"},
{"chat_sessions", "unread_count"},
@@ -0,0 +1,7 @@
-- +goose Up
ALTER TABLE item_publish_batches ADD COLUMN location_json LONGTEXT NULL;
UPDATE item_publish_batches SET location_json='{}' WHERE location_json IS NULL;
ALTER TABLE item_publish_batches MODIFY COLUMN location_json LONGTEXT NOT NULL;
-- +goose Down
ALTER TABLE item_publish_batches DROP COLUMN location_json;
@@ -0,0 +1,5 @@
-- +goose Up
ALTER TABLE item_publish_batches ADD COLUMN location_json TEXT NOT NULL DEFAULT '{}';
-- +goose Down
ALTER TABLE item_publish_batches DROP COLUMN location_json;
@@ -0,0 +1,5 @@
-- +goose Up
ALTER TABLE item_publish_batches ADD COLUMN location_json TEXT NOT NULL DEFAULT '{}';
-- +goose Down
ALTER TABLE item_publish_batches DROP COLUMN location_json;
+5 -1
View File
@@ -771,7 +771,7 @@ func TestMultiDB_LatestMigrationsDownUp(t *testing.T) {
t.Fatalf("set goose dialect: %v", err)
}
goose.SetBaseFS(migrationsFS)
for i := 0; i < 13; i++ {
for i := 0; i < 14; i++ {
if err := goose.Down(tg.store.DB, "migrations/"+subdir); err != nil {
t.Fatalf("migration down #%d: %v", i+1, err)
}
@@ -791,6 +791,9 @@ func TestMultiDB_LatestMigrationsDownUp(t *testing.T) {
if columnExistsForDialect(t, tg.store.DB, tg.dialect, "item_publish_batch_rows", "category_json") {
t.Fatal("item_publish_batch_rows.category_json should be removed after down")
}
if columnExistsForDialect(t, tg.store.DB, tg.dialect, "item_publish_batches", "location_json") {
t.Fatal("item_publish_batches.location_json should be removed after down")
}
for _, table := range []string{"account_task_settings", "account_task_runs", "chat_sessions", "chat_messages"} {
if tableExistsForDialect(t, tg.store.DB, tg.dialect, table) {
t.Fatalf("table should be removed after migration 24 down: %s", table)
@@ -816,6 +819,7 @@ func TestMultiDB_LatestMigrationsDownUp(t *testing.T) {
{"automation_runs", "action_started"},
{"account_tokens", "cookie_fingerprint"},
{"item_publish_batch_rows", "category_json"},
{"item_publish_batches", "location_json"},
{"item_info", "deleted_at"},
{"automation_rules", "deleted_at"},
{"account_task_settings", "auto_rate_enabled"},