修复迁移时,数据库未指定的情况下数据库脚本混乱的问题

This commit is contained in:
2025-12-06 22:03:57 +08:00
parent 6547e7bca8
commit b66f345281
7 changed files with 194 additions and 77 deletions

View File

@@ -24,11 +24,11 @@ import (
// ./migrate down # 回滚最后一个迁移
//
// Docker 中使用:
// # 方式1挂载配置文件
// # 方式1挂载配置文件(推荐)
// docker run -v /host/config.json:/app/config.json myapp ./migrate up
//
// # 方式2使用环境变量
// docker run -e DATABASE_URL="mysql://..." myapp ./migrate up
// # 方式2使用环境变量指定配置文件路径
// docker run -e CONFIG_FILE=/etc/app/config.json myapp ./migrate up
//
// # 方式3指定容器内的配置文件路径
// docker run myapp ./migrate up -config /etc/app/config.json
@@ -41,8 +41,7 @@ import (
// 配置优先级(从高到低):
// 1. 命令行参数 -config 和 -dir
// 2. 环境变量 CONFIG_FILE 和 MIGRATIONS_DIR
// 3. 环境变量 DATABASE_URL直接连接无需配置文件
// 4. 默认值config.json 和 migrations
// 3. 默认值config.json 和 migrations
var (
configFile string
@@ -137,15 +136,14 @@ func printHelp() {
fmt.Println(" # 指定配置和迁移目录")
fmt.Println(" migrate up -c config.json -d db/migrations")
fmt.Println()
fmt.Println(" # 使用环境变量")
fmt.Println(" DATABASE_URL='mysql://...' migrate up")
fmt.Println(" # 使用环境变量指定配置文件路径")
fmt.Println(" CONFIG_FILE=/etc/app/config.json migrate up")
fmt.Println()
fmt.Println(" # Docker 中使用")
fmt.Println(" # Docker 中使用(挂载配置文件)")
fmt.Println(" docker run -v /host/config.json:/app/config.json myapp migrate up")
fmt.Println()
fmt.Println("配置优先级(从高到低):")
fmt.Println(" 1. 命令行参数 -config 和 -dir")
fmt.Println(" 2. 环境变量 CONFIG_FILE 和 MIGRATIONS_DIR")
fmt.Println(" 3. 环境变量 DATABASE_URL")
fmt.Println(" 4. 默认值config.json 和 migrations")
fmt.Println(" 3. 默认值config.json 和 migrations")
}