fix(admin): standardize resource initialization and lists
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/config"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/initdb"
|
||||
deliverylogic "git.apinb.com/heqiapp/platforms/backend/api/internal/logic/delivery"
|
||||
@@ -165,7 +167,7 @@ func writeMockData() error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("connect database: %w", err)
|
||||
}
|
||||
if err := initdb.New(databaseService); err != nil {
|
||||
if err := initdb.New(); err != nil {
|
||||
return fmt.Errorf("initialize platform data: %w", err)
|
||||
}
|
||||
if err := seed.MockData(databaseService); err != nil {
|
||||
@@ -175,45 +177,24 @@ func writeMockData() error {
|
||||
}
|
||||
|
||||
func migrateDatabase() error {
|
||||
config.New("heqi")
|
||||
if config.Spec.Databases == nil {
|
||||
return fmt.Errorf("database configuration is required")
|
||||
config.New(serviceKey)
|
||||
options := &types.SqlOptions{
|
||||
MaxIdleConns: vars.SqlOptionMaxIdleConns,
|
||||
MaxOpenConns: vars.SqlOptionMaxOpenConns,
|
||||
ConnMaxLifetime: vars.SqlOptionConnMaxLifetime,
|
||||
IsAutoMigrate: true,
|
||||
LogStdout: false,
|
||||
Debug: true,
|
||||
}
|
||||
var migrationDatabase *gorm.DB
|
||||
var err error
|
||||
driver := strings.ToLower(config.Spec.Databases.Driver)
|
||||
switch driver {
|
||||
case "postgres":
|
||||
migrationDatabase, err = database.NewPostgres(config.Spec.Databases.Source, nil)
|
||||
case "mysql":
|
||||
migrationDatabase, err = database.NewMysql(config.Spec.Databases.Source, nil)
|
||||
default:
|
||||
return fmt.Errorf("unsupported database driver: %s", config.Spec.Databases.Driver)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("connect database before migration: %w", err)
|
||||
}
|
||||
if err := prepareAdditiveMigrations(migrationDatabase, driver); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := rejectLegacyPaymentSchema(migrationDatabase); err != nil {
|
||||
return err
|
||||
}
|
||||
const legacyPhoneIndex = "idx_platform_account_phone"
|
||||
if migrationDatabase.Migrator().HasIndex(&models.PlatformAccount{}, legacyPhoneIndex) {
|
||||
if err := migrationDatabase.Migrator().DropIndex(&models.PlatformAccount{}, legacyPhoneIndex); err != nil {
|
||||
return fmt.Errorf("drop legacy platform account phone index: %w", err)
|
||||
}
|
||||
}
|
||||
databaseService, err := database.NewDatabase(
|
||||
_, err := database.NewDatabase(
|
||||
config.Spec.Databases.Driver,
|
||||
config.Spec.Databases.Source,
|
||||
nil,
|
||||
options,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("migrate database: %w", err)
|
||||
}
|
||||
return initdb.New(databaseService)
|
||||
return initdb.New()
|
||||
}
|
||||
|
||||
// rejectLegacyPaymentSchema 阻止通用迁移物理删除历史资金表。
|
||||
|
||||
Reference in New Issue
Block a user