chore(db): 合并迁移文件为单一 001_init.sql

- 将 002-006 迁移合并进 001_init.sql,只保留一个 init 文件
- 表结构直接采用最终态:移除旧 skins/orders/ship_logs 表、users 的 invite_code/parent_id 列,货币默认 POINT,手续费 fee_type 二选一
- 修复鸡生蛋问题:自营商户在 SQL 内直接插入,商品种子不再依赖 Go 代码后续创建的商户
- 商品价格一步到位,消除 006 价格补丁
- 简化 migrate.go:移除 ensureSelfMerchant/ensureWallet,清理无用 import
- 编译与 go vet 通过,数据库重置后全新初始化验证通过
This commit is contained in:
yml2213
2026-07-30 15:09:39 +08:00
parent 8c4fcb18ad
commit 108fd834aa
6 changed files with 154 additions and 252 deletions
+2 -38
View File
@@ -11,10 +11,7 @@ import (
"strings"
"time"
"affiliate_dash/internal/model"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
//go:embed migrations/*.sql
@@ -32,6 +29,7 @@ func (schemaMigration) TableName() string {
}
// Migrate 使用显式 SQL 迁移管理表结构,避免大型项目依赖隐式结构同步。
// 自营商户、默认钱包与商品目录种子数据均在 001_init.sql 中完成,无需在 Go 层补充。
func Migrate(db *gorm.DB) error {
return db.Transaction(func(tx *gorm.DB) error {
if err := tx.Exec("SELECT pg_advisory_xact_lock(hashtext(?))", "affiliate_dash_schema_migrations").Error; err != nil {
@@ -40,14 +38,7 @@ func Migrate(db *gorm.DB) error {
if err := ensureMigrationTable(tx); err != nil {
return err
}
if err := applySQLMigrations(tx); err != nil {
return err
}
merchant, err := ensureSelfMerchant(tx)
if err != nil {
return err
}
return ensureWallet(tx, merchant.ID)
return applySQLMigrations(tx)
})
}
@@ -139,30 +130,3 @@ func migrationChecksum(content []byte) string {
sum := sha256.Sum256(content)
return hex.EncodeToString(sum[:])
}
func ensureSelfMerchant(tx *gorm.DB) (*model.Merchant, error) {
var merchant model.Merchant
err := tx.Where("code = ?", "self-operated").First(&merchant).Error
if err == nil {
return &merchant, nil
}
if err != gorm.ErrRecordNotFound {
return nil, err
}
merchant = model.Merchant{
Code: "self-operated",
Name: "自营商户",
Status: model.MerchantStatusActive,
}
if err := tx.Create(&merchant).Error; err != nil {
return nil, err
}
return &merchant, nil
}
func ensureWallet(tx *gorm.DB, merchantID uint) error {
return tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&model.WalletAccount{
MerchantID: merchantID,
Currency: "POINT",
}).Error
}