feat(架构): 完善基础架构设计
This commit is contained in:
@@ -50,6 +50,16 @@ func (a *Application) Run() error {
|
||||
// 打印启动信息
|
||||
a.printBanner()
|
||||
|
||||
// 检查是否需要自动迁移
|
||||
if a.config.Database.AutoMigrate {
|
||||
a.logger.Info("Auto migration is enabled, running database migrations...")
|
||||
if err := a.RunMigrations(); err != nil {
|
||||
a.logger.Error("Auto migration failed", zap.Error(err))
|
||||
return fmt.Errorf("auto migration failed: %w", err)
|
||||
}
|
||||
a.logger.Info("Auto migration completed successfully")
|
||||
}
|
||||
|
||||
// 启动容器
|
||||
a.logger.Info("Starting application container...")
|
||||
if err := a.container.Start(); err != nil {
|
||||
@@ -92,10 +102,10 @@ func (a *Application) RunMigrations() error {
|
||||
func (a *Application) printBanner() {
|
||||
banner := fmt.Sprintf(`
|
||||
╔══════════════════════════════════════════════════════════════╗
|
||||
║ %s ║
|
||||
║ Version: %s ║
|
||||
║ Environment: %s ║
|
||||
║ Port: %s ║
|
||||
║ %s
|
||||
║ Version: %s
|
||||
║ Environment: %s
|
||||
║ Port: %s
|
||||
╚══════════════════════════════════════════════════════════════╝
|
||||
`,
|
||||
a.config.App.Name,
|
||||
@@ -151,9 +161,20 @@ func (a *Application) createDatabaseConnection() (*gorm.DB, error) {
|
||||
|
||||
// autoMigrate 自动迁移
|
||||
func (a *Application) autoMigrate(db *gorm.DB) error {
|
||||
// 如果需要删除某些表,可以在这里手动删除
|
||||
// 注意:这会永久删除数据,请谨慎使用!
|
||||
/*
|
||||
// 删除不再需要的表(示例,请根据实际情况使用)
|
||||
if err := db.Migrator().DropTable(&entities.FavoriteItem{}); err != nil {
|
||||
a.logger.Warn("Failed to drop table", zap.Error(err))
|
||||
// 继续执行,不阻断迁移
|
||||
}
|
||||
*/
|
||||
|
||||
// 迁移用户相关表
|
||||
return db.AutoMigrate(
|
||||
&entities.User{},
|
||||
&entities.SMSCode{},
|
||||
// 后续可以添加其他实体
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user