1
This commit is contained in:
@@ -52,6 +52,24 @@ func ClearCache(ctx context.Context, keys ...string) {
|
||||
}
|
||||
}
|
||||
|
||||
// EnsureColumn 存量库迁移:列缺失时 ALTER TABLE ADD COLUMN(SQLite 支持表尾追加)。
|
||||
// 新库由 CREATE TABLE 直接含列、已迁移库列已存在,均跳过;失败 panic(启动即暴露)。
|
||||
func EnsureColumn(ctx context.Context, table, column, ddl string) {
|
||||
res, err := g.DB().GetAll(ctx, "PRAGMA table_info("+table+")")
|
||||
if err != nil {
|
||||
panic("检查表结构失败 " + table + ": " + err.Error())
|
||||
}
|
||||
for _, r := range res {
|
||||
if gconv.String(r["name"]) == column {
|
||||
return
|
||||
}
|
||||
}
|
||||
if _, err := g.DB().Exec(ctx, "ALTER TABLE "+table+" ADD COLUMN "+ddl); err != nil {
|
||||
panic("迁移加列失败 " + table + "." + column + ": " + err.Error())
|
||||
}
|
||||
g.Log().Warningf(ctx, "存量表 %s 已迁移:新增列 %s", table, column)
|
||||
}
|
||||
|
||||
// DropLegacyTableIfHasColumn 存量库迁移:表存在旧版本废弃列(账号体系上线前的 device_id)
|
||||
// 时 DROP 整表(存量数据作废,用户决策),由 dao init 以新结构重建。
|
||||
func DropLegacyTableIfHasColumn(ctx context.Context, table, column string) {
|
||||
|
||||
Reference in New Issue
Block a user