fix(realname): DB 迁移 + Model + Store 清理 REALNAME-01
- 新增迁移 000089: 移除 enable_realname_activation,添加 expiry_base 字段 - package.go: 删除 EnableRealnameActivation 字段,新增 ExpiryBase 字段 - package_store.go: 移除两步写入特殊处理,改为直接一步 Create
This commit is contained in:
@@ -45,7 +45,8 @@ type Package struct {
|
|||||||
CalendarType string `gorm:"column:calendar_type;type:varchar(20);default:'by_day';comment:套餐周期类型 natural_month-自然月 by_day-按天" json:"calendar_type"`
|
CalendarType string `gorm:"column:calendar_type;type:varchar(20);default:'by_day';comment:套餐周期类型 natural_month-自然月 by_day-按天" json:"calendar_type"`
|
||||||
DurationDays int `gorm:"column:duration_days;type:int;comment:套餐天数(calendar_type=by_day时必填)" json:"duration_days"`
|
DurationDays int `gorm:"column:duration_days;type:int;comment:套餐天数(calendar_type=by_day时必填)" json:"duration_days"`
|
||||||
DataResetCycle string `gorm:"column:data_reset_cycle;type:varchar(20);default:'monthly';comment:流量重置周期 daily-每日 monthly-每月 yearly-每年 none-不重置" json:"data_reset_cycle"`
|
DataResetCycle string `gorm:"column:data_reset_cycle;type:varchar(20);default:'monthly';comment:流量重置周期 daily-每日 monthly-每月 yearly-每年 none-不重置" json:"data_reset_cycle"`
|
||||||
EnableRealnameActivation bool `gorm:"column:enable_realname_activation;type:boolean;default:true;comment:是否启用实名激活 true-需实名后激活 false-立即激活" json:"enable_realname_activation"`
|
// ExpiryBase 到期时间基准:from_activation-实名激活时起算,from_purchase-购买时起算
|
||||||
|
ExpiryBase string `gorm:"column:expiry_base;type:varchar(30);not null;default:'from_activation';comment:到期时间基准 from_activation-实名激活时起算 from_purchase-购买时起算" json:"expiry_base"`
|
||||||
VirtualRatio float64 `gorm:"column:virtual_ratio;type:decimal(18,6);default:1.0;comment:虚流量比例(real_data_mb/virtual_data_mb),创建套餐时计算存储" json:"virtual_ratio"`
|
VirtualRatio float64 `gorm:"column:virtual_ratio;type:decimal(18,6);default:1.0;comment:虚流量比例(real_data_mb/virtual_data_mb),创建套餐时计算存储" json:"virtual_ratio"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,12 +20,7 @@ func NewPackageStore(db *gorm.DB) *PackageStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *PackageStore) Create(ctx context.Context, pkg *model.Package) error {
|
func (s *PackageStore) Create(ctx context.Context, pkg *model.Package) error {
|
||||||
// GORM 对零值字段有特殊处理,先创建然后立即更新 enable_realname_activation 字段确保正确设置
|
return s.db.WithContext(ctx).Create(pkg).Error
|
||||||
if err := s.db.WithContext(ctx).Omit("enable_realname_activation").Create(pkg).Error; err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// 明确更新 enable_realname_activation 字段(包括零值 false)
|
|
||||||
return s.db.WithContext(ctx).Model(pkg).Update("enable_realname_activation", pkg.EnableRealnameActivation).Error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PackageStore) GetByID(ctx context.Context, id uint) (*model.Package, error) {
|
func (s *PackageStore) GetByID(ctx context.Context, id uint) (*model.Package, error) {
|
||||||
|
|||||||
7
migrations/000089_realname_expiry_base.down.sql
Normal file
7
migrations/000089_realname_expiry_base.down.sql
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
-- 回滚:恢复旧字段,移除新字段
|
||||||
|
ALTER TABLE tb_package DROP COLUMN IF EXISTS expiry_base;
|
||||||
|
|
||||||
|
ALTER TABLE tb_package
|
||||||
|
ADD COLUMN IF NOT EXISTS enable_realname_activation BOOLEAN NOT NULL DEFAULT TRUE;
|
||||||
|
|
||||||
|
COMMENT ON COLUMN tb_package.enable_realname_activation IS '是否启用实名激活 true-需实名后激活 false-立即激活';
|
||||||
13
migrations/000089_realname_expiry_base.up.sql
Normal file
13
migrations/000089_realname_expiry_base.up.sql
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
-- REALNAME-01: 实名激活架构重构
|
||||||
|
-- 移除旧的布尔字段 enable_realname_activation,
|
||||||
|
-- 新增 expiry_base 枚举字段控制套餐计时起点
|
||||||
|
-- 注意:不做旧字段到新字段的数据迁移,现有记录全部默认 from_activation
|
||||||
|
|
||||||
|
-- 移除旧字段
|
||||||
|
ALTER TABLE tb_package DROP COLUMN IF EXISTS enable_realname_activation;
|
||||||
|
|
||||||
|
-- 新增到期时间基准字段
|
||||||
|
ALTER TABLE tb_package
|
||||||
|
ADD COLUMN IF NOT EXISTS expiry_base VARCHAR(30) NOT NULL DEFAULT 'from_activation';
|
||||||
|
|
||||||
|
COMMENT ON COLUMN tb_package.expiry_base IS '到期时间基准 from_activation-实名激活时起算 from_purchase-购买时起算';
|
||||||
Reference in New Issue
Block a user