feat: automate business upgrades and sync reviewed frontend
This commit is contained in:
@@ -4,17 +4,20 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
drivermysql "github.com/go-sql-driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"kra/internal/config"
|
||||
"kra/internal/data/swmigration"
|
||||
"kra/pkg/database/migration"
|
||||
"kra/pkg/module"
|
||||
)
|
||||
|
||||
// MigrateDatabase opens only the configured primary database. It does not
|
||||
// create a database, start a scheduler, open SDK clients, or initialize users.
|
||||
func MigrateDatabase(ctx context.Context, c *config.Database, catalog module.Catalog, logger *slog.Logger, checkOnly bool) error {
|
||||
func MigrateDatabase(ctx context.Context, c *config.Database, catalog module.Catalog, logger *slog.Logger, checkOnly bool, configPath ...string) error {
|
||||
if !databaseConnectionConfigured(c) {
|
||||
return fmt.Errorf("migration requires an existing configured database")
|
||||
}
|
||||
@@ -23,7 +26,23 @@ func MigrateDatabase(ctx context.Context, c *config.Database, catalog module.Cat
|
||||
if !checkOnly && migrationConfig.MaxOpenConns > 0 && migrationConfig.MaxOpenConns < 2 {
|
||||
migrationConfig.MaxOpenConns = 2
|
||||
}
|
||||
db, err := openDatabase(&migrationConfig, false, "", logger)
|
||||
var db *gorm.DB
|
||||
var err error
|
||||
if normalizedDriver(c.Driver) == "mysql" && !checkOnly {
|
||||
var dsn string
|
||||
dsn, err = databaseDSN(&migrationConfig, "")
|
||||
if err == nil {
|
||||
var options *drivermysql.Config
|
||||
options, err = drivermysql.ParseDSN(dsn)
|
||||
if err == nil {
|
||||
options.ReadTimeout = 0
|
||||
options.WriteTimeout = 0
|
||||
db, err = openWithDriverConfig("mysql", options.FormatDSN(), &migrationConfig, logger)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
db, err = openDatabase(&migrationConfig, false, "", logger)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("open migration database: %w", err)
|
||||
}
|
||||
@@ -32,6 +51,17 @@ func MigrateDatabase(ctx context.Context, c *config.Database, catalog module.Cat
|
||||
if checkOnly {
|
||||
return checkMigratedSchema(db, catalog)
|
||||
}
|
||||
automatic, err := swmigration.Recognized(db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if automatic {
|
||||
root := ""
|
||||
if len(configPath) > 0 && configPath[0] != "" {
|
||||
root = filepath.Join(filepath.Dir(configPath[0]), "migration-backups")
|
||||
}
|
||||
return swmigration.Run(ctx, db, catalog, root, logger)
|
||||
}
|
||||
// A dedicated connection owns the lock even when a migration uses a
|
||||
// transaction or temporarily changes connection-local SQL compatibility.
|
||||
run := func() error {
|
||||
@@ -67,6 +97,22 @@ func MigrateDatabase(ctx context.Context, c *config.Database, catalog module.Cat
|
||||
})
|
||||
}
|
||||
|
||||
// SW upgrades are automatic even for installations that previously disabled
|
||||
// generic table auto-migration. Other template databases keep that setting.
|
||||
func prepareConfiguredDatabase(ctx context.Context, db *gorm.DB, c *config.Database, catalog module.Catalog, logger *slog.Logger, configPath string, disable bool) error {
|
||||
automatic, err := swmigration.Recognized(db.Session(&gorm.Session{NewDB: true}).WithContext(ctx))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if automatic {
|
||||
return MigrateDatabase(ctx, c, catalog, logger, false, configPath)
|
||||
}
|
||||
if disable {
|
||||
return checkMigratedSchema(db, catalog)
|
||||
}
|
||||
return migrateAll(db, catalog)
|
||||
}
|
||||
|
||||
func checkMigratedSchema(db *gorm.DB, catalog module.Catalog) error {
|
||||
db = db.Session(&gorm.Session{NewDB: true})
|
||||
if err := migration.CheckApplied(db, catalog.MigrationSteps()); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user