feat: 完善后端脚手架基础能力
This commit is contained in:
+15
-5
@@ -12,7 +12,8 @@ import (
|
||||
|
||||
// MigrationConfig 迁移配置
|
||||
type MigrationConfig struct {
|
||||
Environment string // local, production
|
||||
Environment string // 例如 local_postgres、production_mysql
|
||||
Directory string // 按数据库方言隔离的迁移目录
|
||||
Timeout int // 超时时间(秒)
|
||||
}
|
||||
|
||||
@@ -77,11 +78,15 @@ func (m *MigrationManager) GenerateMigration(name string) error {
|
||||
}
|
||||
|
||||
// ApplyMigrations 应用迁移
|
||||
func (m *MigrationManager) ApplyMigrations() error {
|
||||
func (m *MigrationManager) ApplyMigrations(dryRun bool) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(m.config.Timeout)*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cmd := exec.CommandContext(ctx, "atlas", "migrate", "apply", "--env", m.config.Environment)
|
||||
args := []string{"migrate", "apply", "--env", m.config.Environment}
|
||||
if dryRun {
|
||||
args = append(args, "--dry-run")
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, "atlas", args...)
|
||||
output, err := cmd.CombinedOutput()
|
||||
|
||||
if err != nil {
|
||||
@@ -91,7 +96,9 @@ func (m *MigrationManager) ApplyMigrations() error {
|
||||
return fmt.Errorf("应用迁移失败: %w", err)
|
||||
}
|
||||
|
||||
m.logger.Info("迁移应用成功", zap.String("output", string(output)))
|
||||
m.logger.Info("迁移应用成功",
|
||||
zap.Bool("dry_run", dryRun),
|
||||
zap.String("output", string(output)))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -126,7 +133,10 @@ func EnsureAtlasInstalled() error {
|
||||
|
||||
// InitMigrationDirectory 初始化迁移目录
|
||||
func (m *MigrationManager) InitMigrationDirectory() error {
|
||||
migrationDir := "migrations"
|
||||
migrationDir := m.config.Directory
|
||||
if migrationDir == "" {
|
||||
migrationDir = "migrations"
|
||||
}
|
||||
|
||||
// 检查目录是否存在
|
||||
if _, err := os.Stat(migrationDir); os.IsNotExist(err) {
|
||||
|
||||
Reference in New Issue
Block a user