mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-10 17:26:38 +08:00
refactor(migrator): use goose SQL only and auto-import zones on upgrade
移除 Go goose 迁移(bridge/legacy data/zone import),改为 goose SQL 占位 与结构迁移脚本;启动时 Migrate 在删旧列前自动导入历史域名,去掉 migrate-zones 手动命令及文档中的人工导入步骤。
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/internal/apps/openflare/zone"
|
||||
"github.com/Rain-kl/Wavelet/internal/config"
|
||||
"github.com/Rain-kl/Wavelet/internal/db"
|
||||
"github.com/Rain-kl/Wavelet/internal/repository"
|
||||
@@ -34,7 +35,9 @@ func dbType() string {
|
||||
const (
|
||||
dialectSqlite = "sqlite3"
|
||||
dialectPostgres = "postgres"
|
||||
cascadeSuffix = " CASCADE"
|
||||
// zoneImportSQLVersion is the goose SQL marker after of_zones creation and
|
||||
// before drop of legacy route domain columns. Zone data import runs here.
|
||||
zoneImportSQLVersion int64 = 202607120002
|
||||
)
|
||||
|
||||
func gooseDialect() string {
|
||||
@@ -51,7 +54,7 @@ func migrationDir() string {
|
||||
return "goose/postgres"
|
||||
}
|
||||
|
||||
// Migrate 执行数据库迁移
|
||||
// Migrate 执行数据库迁移:全部结构变更走 goose SQL;Zone 历史域名导入在 SQL 之后自动执行。
|
||||
func Migrate() {
|
||||
gormDB := db.DB(context.Background())
|
||||
if gormDB == nil {
|
||||
@@ -70,6 +73,15 @@ func Migrate() {
|
||||
if err := resyncGooseVersionSequence(sqlDB); err != nil {
|
||||
log.Fatalf("[%s] resync goose_db_version sequence failed: %v\n", dbType(), err)
|
||||
}
|
||||
// 1) SQL up to zone-import marker (includes of_zones DDL; still has legacy columns).
|
||||
if err := goose.UpTo(sqlDB, migrationDir(), zoneImportSQLVersion); err != nil {
|
||||
log.Fatalf("[%s] goose migrate (up to zone import) failed: %v\n", dbType(), err)
|
||||
}
|
||||
// 2) Auto-import legacy domains (publicsuffix; idempotent; no-op after phase-2 drop).
|
||||
if err := importZoneDomainsAfterGoose(sqlDB); err != nil {
|
||||
log.Fatalf("[%s] zone domain import failed: %v\n", dbType(), err)
|
||||
}
|
||||
// 3) Remaining SQL (drop legacy columns / managed_domains, later migrations).
|
||||
if err := goose.Up(sqlDB, migrationDir()); err != nil {
|
||||
log.Fatalf("[%s] goose migrate failed: %v\n", dbType(), err)
|
||||
}
|
||||
@@ -79,6 +91,31 @@ func Migrate() {
|
||||
log.Printf("[%s] goose migrate success\n", dbType())
|
||||
}
|
||||
|
||||
// importZoneDomainsAfterGoose 在 SQL 迁移完成后自动导入旧路由/托管域名。
|
||||
// 必须使用 publicsuffix 解析注册根域,故不能放在纯 SQL 中;幂等且在旧列删除后为空操作。
|
||||
func importZoneDomainsAfterGoose(sqlDB *sql.DB) error {
|
||||
ctx := context.Background()
|
||||
tx, err := sqlDB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin zone import transaction: %w", err)
|
||||
}
|
||||
report, err := zone.ImportLegacyTx(ctx, tx, gooseDialect() == dialectPostgres)
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return report.LogAndReturn(err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return fmt.Errorf("commit zone import: %w", err)
|
||||
}
|
||||
if report.Zones > 0 || report.Domains > 0 {
|
||||
log.Printf(
|
||||
"[%s] imported zone domains automatically: zones=%d domains=%d\n",
|
||||
dbType(), report.Zones, report.Domains,
|
||||
)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// resyncGooseVersionSequence 修复 PostgreSQL 下 goose_db_version.id 自增序列落后于
|
||||
// MAX(id) 的问题(常见于从 dump 恢复或历史迁移以显式 id 复制数据后)。序列落后会
|
||||
// 导致 goose 记录新版本号时 INSERT 命中 goose_db_version_pkey 唯一约束冲突。
|
||||
@@ -115,17 +152,3 @@ func clearSystemConfigCache() {
|
||||
log.Printf("[%s] clear system config cache failed: %v\n", dbType(), err)
|
||||
}
|
||||
}
|
||||
|
||||
func tableExistsSQL(dialect string) string {
|
||||
if dialect == dialectPostgres {
|
||||
return "SELECT count(*) FROM information_schema.tables WHERE table_schema='public' AND table_name=$1"
|
||||
}
|
||||
return "SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?"
|
||||
}
|
||||
|
||||
func tablesWithPrefixSQL(dialect string) string {
|
||||
if dialect == dialectPostgres {
|
||||
return "SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_name LIKE $1"
|
||||
}
|
||||
return "SELECT name FROM sqlite_master WHERE type='table' AND name LIKE ?"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user