refactor(database): 调整数据库初始化函数参数传递方式

将 Init 函数参数从全局变量改为通过 NewDatabase 函数参数传入,
使初始化逻辑更清晰、可控。同时优化代码格式,去除多余空行,
提升代码可读性。
```
This commit is contained in:
yanweidong 2025-09-23 11:02:25 +08:00
parent f2d8ae26f6
commit cf0ee224f7
2 changed files with 7 additions and 9 deletions

View File

@ -15,13 +15,11 @@ import (
var ( var (
// MigrateTables holds the tables that need to be auto-migrated on database initialization // MigrateTables holds the tables that need to be auto-migrated on database initialization
MigrateTables []any MigrateTables []any
// Init is an optional initialization function that can be executed after database connection is established
Init *func() = nil
) )
// NewDatabase creates a new database connection based on the provided driver type // NewDatabase creates a new database connection based on the provided driver type
// It supports both MySQL and PostgreSQL databases // It supports both MySQL and PostgreSQL databases
func NewDatabase(driver string, dsn []string, options *types.SqlOptions) (db *gorm.DB, err error) { func NewDatabase(driver string, dsn []string, options *types.SqlOptions, Init *func()) (db *gorm.DB, err error) {
driver = strings.ToLower(driver) driver = strings.ToLower(driver)
switch driver { switch driver {
case "mysql": case "mysql":