This commit is contained in:
“xHuPo” 2025-05-23 14:15:48 +08:00
parent a6461a9a0e
commit a45ddf13d5
2 changed files with 22 additions and 5 deletions

View file

@ -3,6 +3,7 @@ server:
database:
driver: sqlite
dsn: otpm.sqlite
skip_migration: false
port: 8080
auth:

View file

@ -37,12 +37,28 @@ func InitDB() (*sqlx.DB, error) {
}
func MigrateDB(db *sqlx.DB) error {
if _, err := db.Exec(userTable); err != nil {
return fmt.Errorf("failed to create user migration: %w", err)
// 检查是否需要执行迁移
skipMigration := viper.GetBool("database.skip_migration")
if skipMigration {
log.Println("Skipping database migration as configured")
return nil
}
if _, err := db.Exec(otpTable); err != nil {
return fmt.Errorf("failed to create otp migration: %w", err)
// 执行用户表迁移
if _, err := db.Exec(userTable); err != nil {
log.Printf("Warning: failed to create user migration: %v", err)
// 继续执行,不返回错误
} else {
log.Println("User table migration completed successfully")
}
// 执行OTP表迁移
if _, err := db.Exec(otpTable); err != nil {
log.Printf("Warning: failed to create otp migration: %v", err)
// 继续执行,不返回错误
} else {
log.Println("OTP table migration completed successfully")
}
return nil
}