调整迁移的逻辑

This commit is contained in:
2025-12-06 21:38:53 +08:00
parent 6146178111
commit 6547e7bca8
5 changed files with 515 additions and 88 deletions

View File

@@ -69,6 +69,9 @@ func main() {
}
// 获取命令默认up
// 支持两种方式:
// 1. 位置参数:./migrate up
// 2. 标志参数:./migrate -cmd=up向后兼容
command := "up"
args := flag.Args()
if len(args) > 0 {
@@ -83,16 +86,18 @@ func main() {
}
// 获取配置文件路径(优先级:命令行 > 环境变量 > 默认值)
// 如果未指定RunMigrationsFromConfigWithCommand 会自动查找
if configFile == "" {
configFile = getEnv("CONFIG_FILE", "config.json")
configFile = getEnv("CONFIG_FILE", "")
}
// 获取迁移目录(优先级:命令行 > 环境变量 > 默认值)
// 如果未指定RunMigrationsFromConfigWithCommand 会使用默认值 "migrations"
if migrationsDir == "" {
migrationsDir = getEnv("MIGRATIONS_DIR", "migrations")
migrationsDir = getEnv("MIGRATIONS_DIR", "")
}
// 执行迁移
// 执行迁移(黑盒模式:内部自动处理所有细节)
if err := migration.RunMigrationsFromConfigWithCommand(configFile, migrationsDir, command); err != nil {
fmt.Fprintf(os.Stderr, "错误: %v\n", err)
os.Exit(1)
@@ -144,4 +149,3 @@ func printHelp() {
fmt.Println(" 3. 环境变量 DATABASE_URL")
fmt.Println(" 4. 默认值config.json 和 migrations")
}