mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-03 23:06:36 +08:00
[优化] go 引用调整
This commit is contained in:
@@ -0,0 +1,244 @@
|
||||
package goose
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type BridgeContext interface {
|
||||
Context
|
||||
AutoMigrateLegacySchemaMetadata(db *gorm.DB) error
|
||||
InitializeFreshDatabaseSchema(db *gorm.DB, backend string) error
|
||||
IsDatabaseEmpty(db *gorm.DB) (bool, error)
|
||||
RepairCurrentSchemaState(db *gorm.DB, backend string) error
|
||||
SaveLegacyDatabaseSchemaVersion(db *gorm.DB, version int) error
|
||||
UpgradeLegacyDatabaseSchema(db *gorm.DB, backend string, version int) error
|
||||
ValidateCurrentDatabaseSchema(db *gorm.DB, backend string) error
|
||||
}
|
||||
|
||||
type schemaMigrationState int
|
||||
|
||||
const (
|
||||
schemaMigrationStateFresh schemaMigrationState = iota
|
||||
schemaMigrationStateLegacyOnly
|
||||
schemaMigrationStateGooseOnly
|
||||
schemaMigrationStateLegacyBootstrap
|
||||
schemaMigrationStateMixed
|
||||
)
|
||||
|
||||
func detectSchemaState(db *gorm.DB, ctx BridgeContext) (schemaMigrationState, error) {
|
||||
hasLegacyTable := db.Migrator().HasTable("database_schema_versions")
|
||||
hasGooseTable := db.Migrator().HasTable("goose_db_version")
|
||||
|
||||
switch {
|
||||
case hasLegacyTable && hasGooseTable:
|
||||
return schemaMigrationStateMixed, nil
|
||||
case hasLegacyTable:
|
||||
return schemaMigrationStateLegacyOnly, nil
|
||||
case hasGooseTable:
|
||||
return schemaMigrationStateGooseOnly, nil
|
||||
}
|
||||
|
||||
empty, err := ctx.IsDatabaseEmpty(db)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if empty {
|
||||
return schemaMigrationStateFresh, nil
|
||||
}
|
||||
return schemaMigrationStateLegacyBootstrap, nil
|
||||
}
|
||||
|
||||
func LoadDatabaseVersion(db *gorm.DB) (int, bool, error) {
|
||||
if db == nil || !db.Migrator().HasTable("goose_db_version") {
|
||||
return 0, false, nil
|
||||
}
|
||||
|
||||
var version int64
|
||||
err := db.Table("goose_db_version").
|
||||
Where("is_applied = ?", true).
|
||||
Order("version_id DESC").
|
||||
Select("version_id").
|
||||
Limit(1).
|
||||
Row().
|
||||
Scan(&version)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return 0, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
return int(version), true, nil
|
||||
}
|
||||
|
||||
func loadLegacyDatabaseSchemaVersion(db *gorm.DB) (int, bool, error) {
|
||||
if db == nil || !db.Migrator().HasTable("database_schema_versions") {
|
||||
return 0, false, nil
|
||||
}
|
||||
|
||||
var version int
|
||||
err := db.Table("database_schema_versions").
|
||||
Where("id = ?", 1).
|
||||
Select("version").
|
||||
Limit(1).
|
||||
Row().
|
||||
Scan(&version)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return 0, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
return version, true, nil
|
||||
}
|
||||
|
||||
func bootstrapLegacySchemaVersion(db *gorm.DB, ctx BridgeContext) error {
|
||||
if err := ctx.AutoMigrateLegacySchemaMetadata(db); err != nil {
|
||||
return err
|
||||
}
|
||||
version, exists, err := loadLegacyDatabaseSchemaVersion(db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
if int64(version) > LegacyBridgeVersion {
|
||||
return fmt.Errorf("legacy schema version %d is newer than supported terminal version %d", version, LegacyBridgeVersion)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return ctx.SaveLegacyDatabaseSchemaVersion(db, 7)
|
||||
}
|
||||
|
||||
func upgradeLegacyToTerminal(db *gorm.DB, backend string, ctx BridgeContext) error {
|
||||
if err := bootstrapLegacySchemaVersion(db, ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
version, exists, err := loadLegacyDatabaseSchemaVersion(db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("legacy schema version record is missing after bootstrap")
|
||||
}
|
||||
return ctx.UpgradeLegacyDatabaseSchema(db, backend, version)
|
||||
}
|
||||
|
||||
func validateGooseBridgeState(db *gorm.DB) error {
|
||||
version, exists, err := LoadDatabaseVersion(db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
if int64(version) < LegacyBridgeVersion {
|
||||
return fmt.Errorf("goose schema version %d is below legacy bridge baseline %d", version, LegacyBridgeVersion)
|
||||
}
|
||||
if int64(version) > CurrentTargetVersion() {
|
||||
return fmt.Errorf("goose schema version %d is newer than application target version %d", version, CurrentTargetVersion())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func finalizeLegacyToGooseBridge(db *gorm.DB) error {
|
||||
gooseVersion, exists, err := LoadDatabaseVersion(db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists || int64(gooseVersion) < LegacyBridgeVersion {
|
||||
return nil
|
||||
}
|
||||
if !db.Migrator().HasTable("database_schema_versions") {
|
||||
return nil
|
||||
}
|
||||
if err := db.Exec("DROP TABLE IF EXISTS database_schema_versions").Error; err != nil {
|
||||
return fmt.Errorf("drop legacy schema versions table failed: %w", err)
|
||||
}
|
||||
slog.Info("completed legacy-to-goose migration bridge", "goose_version", gooseVersion)
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateRegisteredSchema(db *gorm.DB) error {
|
||||
if err := validateNodeCapabilitiesJSON(db); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func EnsureDatabaseSchemaUpToDate(db *gorm.DB, backend string, ctx BridgeContext) error {
|
||||
var startDesc string
|
||||
legacyVer, hasLegacy, _ := loadLegacyDatabaseSchemaVersion(db)
|
||||
gooseVer, hasGoose, _ := LoadDatabaseVersion(db)
|
||||
if hasGoose {
|
||||
startDesc = fmt.Sprintf("goose version %d", gooseVer)
|
||||
} else if hasLegacy {
|
||||
startDesc = fmt.Sprintf("legacy version %d", legacyVer)
|
||||
} else {
|
||||
startDesc = "none (fresh database)"
|
||||
}
|
||||
|
||||
state, err := detectSchemaState(db, ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch state {
|
||||
case schemaMigrationStateFresh:
|
||||
if err := ctx.InitializeFreshDatabaseSchema(db, backend); err != nil {
|
||||
return err
|
||||
}
|
||||
case schemaMigrationStateLegacyOnly:
|
||||
if err := upgradeLegacyToTerminal(db, backend, ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
case schemaMigrationStateGooseOnly:
|
||||
if err := validateGooseBridgeState(db); err != nil {
|
||||
return err
|
||||
}
|
||||
case schemaMigrationStateLegacyBootstrap:
|
||||
if err := upgradeLegacyToTerminal(db, backend, ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
case schemaMigrationStateMixed:
|
||||
legacyVersion, exists, err := loadLegacyDatabaseSchemaVersion(db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists && int64(legacyVersion) != LegacyBridgeVersion {
|
||||
return fmt.Errorf("incomplete mixed migration state: legacy schema version %d does not match bridge terminal version %d", legacyVersion, LegacyBridgeVersion)
|
||||
}
|
||||
if err := validateGooseBridgeState(db); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("unknown schema migration state: %d", state)
|
||||
}
|
||||
|
||||
if err := runMigrations(db, backend, ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := finalizeLegacyToGooseBridge(db); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ctx.RepairCurrentSchemaState(db, backend); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ctx.ValidateCurrentDatabaseSchema(db, backend); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ValidateRegisteredSchema(db); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
endVer, _, _ := LoadDatabaseVersion(db)
|
||||
if hasGoose && int64(gooseVer) == int64(endVer) {
|
||||
slog.Info("database schema is already up to date", "version", endVer)
|
||||
} else {
|
||||
slog.Info("database migration completed successfully", "from", startDesc, "to", fmt.Sprintf("goose version %d", endVer))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package goose
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
presslygoose "github.com/pressly/goose/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const versionNodeCapabilitiesJSON int64 = 202606020001
|
||||
|
||||
// migration202606020001 adds a future-proof JSON field for node capability
|
||||
// summaries after the legacy v17 migration bridge.
|
||||
func migration202606020001(backend string, ctx Context) *presslygoose.Migration {
|
||||
return newGORMMigration(
|
||||
versionNodeCapabilitiesJSON,
|
||||
"202606020001_add_node_capabilities_json.go",
|
||||
backend,
|
||||
ctx,
|
||||
migrateNodeCapabilitiesJSON,
|
||||
)
|
||||
}
|
||||
|
||||
func migrateNodeCapabilitiesJSON(ctx Context, db *gorm.DB, backend string) error {
|
||||
if err := ctx.ApplyCurrentSchema(db, backend); err != nil {
|
||||
return err
|
||||
}
|
||||
emptyJSON, err := json.Marshal([]string{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal default node capabilities: %w", err)
|
||||
}
|
||||
if err := db.Exec(
|
||||
`UPDATE nodes SET capabilities_json = ? WHERE capabilities_json IS NULL OR TRIM(capabilities_json) = ''`,
|
||||
string(emptyJSON),
|
||||
).Error; err != nil {
|
||||
return fmt.Errorf("backfill nodes.capabilities_json: %w", err)
|
||||
}
|
||||
return validateNodeCapabilitiesJSON(db)
|
||||
}
|
||||
|
||||
func validateNodeCapabilitiesJSON(db *gorm.DB) error {
|
||||
if db == nil {
|
||||
return fmt.Errorf("database handle is nil")
|
||||
}
|
||||
if !db.Migrator().HasColumn("nodes", "capabilities_json") {
|
||||
return fmt.Errorf("column nodes.capabilities_json is missing")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package goose
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
presslygoose "github.com/pressly/goose/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const versionPagesStaticHosting int64 = 202606030001
|
||||
|
||||
// migration202606030001 adds OpenFlare Pages static hosting tables and the
|
||||
// proxy_routes.pages_project_id binding used by the global release snapshot.
|
||||
func migration202606030001(backend string, ctx Context) *presslygoose.Migration {
|
||||
return newGORMMigration(
|
||||
versionPagesStaticHosting,
|
||||
"202606030001_add_pages_static_hosting.go",
|
||||
backend,
|
||||
ctx,
|
||||
migratePagesStaticHosting,
|
||||
)
|
||||
}
|
||||
|
||||
func migratePagesStaticHosting(ctx Context, db *gorm.DB, backend string) error {
|
||||
if err := ctx.ApplyCurrentSchema(db, backend); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := db.Exec(
|
||||
`UPDATE proxy_routes SET upstream_type = 'direct' WHERE upstream_type IS NULL OR TRIM(upstream_type) = ''`,
|
||||
).Error; err != nil {
|
||||
return fmt.Errorf("backfill proxy_routes.upstream_type: %w", err)
|
||||
}
|
||||
return validatePagesStaticHosting(db)
|
||||
}
|
||||
|
||||
func validatePagesStaticHosting(db *gorm.DB) error {
|
||||
if db == nil {
|
||||
return fmt.Errorf("database handle is nil")
|
||||
}
|
||||
for _, table := range []string{"pages_projects", "pages_deployments", "pages_deployment_files"} {
|
||||
if !db.Migrator().HasTable(table) {
|
||||
return fmt.Errorf("table %s is missing", table)
|
||||
}
|
||||
}
|
||||
for _, column := range []string{"upstream_type", "pages_project_id"} {
|
||||
if !db.Migrator().HasColumn("proxy_routes", column) {
|
||||
return fmt.Errorf("column proxy_routes.%s is missing", column)
|
||||
}
|
||||
}
|
||||
for _, column := range []string{"slug", "active_deployment_id", "spa_fallback_enabled", "spa_fallback_path"} {
|
||||
if !db.Migrator().HasColumn("pages_projects", column) {
|
||||
return fmt.Errorf("column pages_projects.%s is missing", column)
|
||||
}
|
||||
}
|
||||
for _, column := range []string{"project_id", "checksum", "artifact_path"} {
|
||||
if !db.Migrator().HasColumn("pages_deployments", column) {
|
||||
return fmt.Errorf("column pages_deployments.%s is missing", column)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package goose
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
presslygoose "github.com/pressly/goose/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const versionPagesSPAFallbackPath int64 = 202606030002
|
||||
|
||||
// migration202606030002 adds a configurable SPA fallback path for Pages
|
||||
// projects. Existing projects keep the previous /index.html behavior.
|
||||
func migration202606030002(backend string, ctx Context) *presslygoose.Migration {
|
||||
return newGORMMigration(
|
||||
versionPagesSPAFallbackPath,
|
||||
"202606030002_add_pages_spa_fallback_path.go",
|
||||
backend,
|
||||
ctx,
|
||||
migratePagesSPAFallbackPath,
|
||||
)
|
||||
}
|
||||
|
||||
func migratePagesSPAFallbackPath(ctx Context, db *gorm.DB, backend string) error {
|
||||
if err := ctx.ApplyCurrentSchema(db, backend); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := db.Exec(
|
||||
`UPDATE pages_projects SET spa_fallback_path = '/index.html' WHERE spa_fallback_path IS NULL OR TRIM(spa_fallback_path) = ''`,
|
||||
).Error; err != nil {
|
||||
return fmt.Errorf("backfill pages_projects.spa_fallback_path: %w", err)
|
||||
}
|
||||
if !db.Migrator().HasColumn("pages_projects", "spa_fallback_path") {
|
||||
return fmt.Errorf("column pages_projects.spa_fallback_path is missing")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package goose
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
presslygoose "github.com/pressly/goose/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const versionDropProxyRouteLegacyPoW int64 = 202606030003
|
||||
|
||||
// migration202606030003 drops the legacy pow_enabled and pow_config columns
|
||||
// from proxy_routes table, since PoW is now entirely managed under WAF rule groups.
|
||||
func migration202606030003(backend string, ctx Context) *presslygoose.Migration {
|
||||
return newGORMMigration(
|
||||
versionDropProxyRouteLegacyPoW,
|
||||
"202606030003_drop_proxy_route_legacy_pow.go",
|
||||
backend,
|
||||
ctx,
|
||||
migrateDropProxyRouteLegacyPoW,
|
||||
)
|
||||
}
|
||||
|
||||
func migrateDropProxyRouteLegacyPoW(ctx Context, db *gorm.DB, backend string) error {
|
||||
if err := ctx.ApplyCurrentSchema(db, backend); err != nil {
|
||||
return err
|
||||
}
|
||||
// Drop pow_enabled column if exists
|
||||
if db.Migrator().HasColumn("proxy_routes", "pow_enabled") {
|
||||
if err := db.Exec("ALTER TABLE proxy_routes DROP COLUMN pow_enabled").Error; err != nil {
|
||||
return fmt.Errorf("drop proxy_routes.pow_enabled: %w", err)
|
||||
}
|
||||
}
|
||||
// Drop pow_config column if exists
|
||||
if db.Migrator().HasColumn("proxy_routes", "pow_config") {
|
||||
if err := db.Exec("ALTER TABLE proxy_routes DROP COLUMN pow_config").Error; err != nil {
|
||||
return fmt.Errorf("drop proxy_routes.pow_config: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package goose
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
presslygoose "github.com/pressly/goose/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const versionPagesFeaturesAndCleanup int64 = 202606040004
|
||||
|
||||
// migration202606040004 merges migrations 202606030004, 202606040001, 202606040002, and 202606040003.
|
||||
// It adds Pages API proxying fields and RootDir/EntryFile to Pages projects,
|
||||
// backfills default entry_file to 'index.html', and ensures unused fields (root_dir, entry_file)
|
||||
// are dropped from Pages deployments.
|
||||
func migration202606040004(backend string, ctx Context) *presslygoose.Migration {
|
||||
return newGORMMigration(
|
||||
versionPagesFeaturesAndCleanup,
|
||||
"202606040004_add_pages_features_and_cleanup.go",
|
||||
backend,
|
||||
ctx,
|
||||
migratePagesFeaturesAndCleanup,
|
||||
)
|
||||
}
|
||||
|
||||
func migratePagesFeaturesAndCleanup(ctx Context, db *gorm.DB, backend string) error {
|
||||
if err := ctx.ApplyCurrentSchema(db, backend); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 1. Verify Pages projects columns
|
||||
cols := []string{
|
||||
"api_proxy_enabled", "api_proxy_path", "api_proxy_pass", "api_proxy_rewrite",
|
||||
"root_dir", "entry_file",
|
||||
}
|
||||
for _, col := range cols {
|
||||
if !db.Migrator().HasColumn("pages_projects", col) {
|
||||
return fmt.Errorf("column pages_projects.%s is missing", col)
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Backfill pages_projects.entry_file to 'index.html' if empty
|
||||
type PagesProject struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
EntryFile string `gorm:"size:512;not null;default:'index.html'"`
|
||||
}
|
||||
if err := db.Model(&PagesProject{}).Where("entry_file = '' OR entry_file IS NULL").Update("entry_file", "index.html").Error; err != nil {
|
||||
return fmt.Errorf("failed to backfill pages_projects.entry_file: %w", err)
|
||||
}
|
||||
|
||||
// 3. Drop unused fields root_dir and entry_file from pages_deployments if they exist
|
||||
if db.Migrator().HasColumn("pages_deployments", "root_dir") {
|
||||
if err := db.Exec("ALTER TABLE pages_deployments DROP COLUMN root_dir").Error; err != nil {
|
||||
return fmt.Errorf("failed to drop pages_deployments.root_dir: %w", err)
|
||||
}
|
||||
}
|
||||
if db.Migrator().HasColumn("pages_deployments", "entry_file") {
|
||||
if err := db.Exec("ALTER TABLE pages_deployments DROP COLUMN entry_file").Error; err != nil {
|
||||
return fmt.Errorf("failed to drop pages_deployments.entry_file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package goose
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
presslygoose "github.com/pressly/goose/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const LegacyBridgeVersion int64 = 17
|
||||
|
||||
type migrationFunc func(ctx Context, db *gorm.DB, backend string) error
|
||||
|
||||
func newBaselineMigration() *presslygoose.Migration {
|
||||
migration := presslygoose.NewGoMigration(LegacyBridgeVersion, nil, nil)
|
||||
migration.Source = fmt.Sprintf("%05d_legacy_terminal_baseline.go", LegacyBridgeVersion)
|
||||
return migration
|
||||
}
|
||||
|
||||
func newGORMMigration(version int64, source string, backend string, ctx Context, up migrationFunc) *presslygoose.Migration {
|
||||
migration := presslygoose.NewGoMigration(version, &presslygoose.GoFunc{
|
||||
RunDB: func(_ context.Context, sqlDB *sql.DB) error {
|
||||
gormDB, err := openGORMDB(ctx, sqlDB, backend)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if backend == "postgres" {
|
||||
return gormDB.Transaction(func(tx *gorm.DB) error {
|
||||
return up(ctx, tx, backend)
|
||||
})
|
||||
}
|
||||
return up(ctx, gormDB, backend)
|
||||
},
|
||||
}, nil)
|
||||
migration.Source = source
|
||||
return migration
|
||||
}
|
||||
|
||||
func registeredMigrations(backend string, ctx Context) []*presslygoose.Migration {
|
||||
return []*presslygoose.Migration{
|
||||
migration202606020001(backend, ctx),
|
||||
migration202606030001(backend, ctx),
|
||||
migration202606030002(backend, ctx),
|
||||
migration202606030003(backend, ctx),
|
||||
migration202606040004(backend, ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func buildMigrations(backend string, ctx Context) []*presslygoose.Migration {
|
||||
migrations := []*presslygoose.Migration{newBaselineMigration()}
|
||||
migrations = append(migrations, registeredMigrations(backend, ctx)...)
|
||||
return migrations
|
||||
}
|
||||
|
||||
func CurrentTargetVersion() int64 {
|
||||
var maxVersion int64 = LegacyBridgeVersion
|
||||
for _, migration := range buildMigrations("sqlite", noopContext{}) {
|
||||
if migration.Version > maxVersion {
|
||||
maxVersion = migration.Version
|
||||
}
|
||||
}
|
||||
return maxVersion
|
||||
}
|
||||
|
||||
type noopContext struct{}
|
||||
|
||||
func (noopContext) ApplyCurrentSchema(db *gorm.DB, backend string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (noopContext) RegisterSharding(db *gorm.DB, backend string) error {
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package goose
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
presslygoose "github.com/pressly/goose/v3"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
type Context interface {
|
||||
ApplyCurrentSchema(db *gorm.DB, backend string) error
|
||||
RegisterSharding(db *gorm.DB, backend string) error
|
||||
}
|
||||
|
||||
func dialectForBackend(backend string) (presslygoose.Dialect, error) {
|
||||
switch backend {
|
||||
case "postgres":
|
||||
return presslygoose.DialectPostgres, nil
|
||||
case "sqlite":
|
||||
return presslygoose.DialectSQLite3, nil
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported database backend: %s", backend)
|
||||
}
|
||||
}
|
||||
|
||||
func openGORMDB(ctx Context, db *sql.DB, backend string) (*gorm.DB, error) {
|
||||
var dialector gorm.Dialector
|
||||
switch backend {
|
||||
case "postgres":
|
||||
dialector = postgres.New(postgres.Config{Conn: db})
|
||||
case "sqlite":
|
||||
dialector = &sqlite.Dialector{Conn: db}
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported database backend: %s", backend)
|
||||
}
|
||||
|
||||
gormDB, err := gorm.Open(dialector, &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ctx.RegisterSharding(gormDB, backend); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return gormDB, nil
|
||||
}
|
||||
|
||||
func buildProvider(db *gorm.DB, backend string, ctx Context) (*presslygoose.Provider, error) {
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dialect, err := dialectForBackend(backend)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return presslygoose.NewProvider(
|
||||
dialect,
|
||||
sqlDB,
|
||||
nil,
|
||||
presslygoose.WithDisableGlobalRegistry(true),
|
||||
presslygoose.WithGoMigrations(buildMigrations(backend, ctx)...),
|
||||
)
|
||||
}
|
||||
|
||||
func runMigrations(db *gorm.DB, backend string, ctx Context) error {
|
||||
provider, err := buildProvider(db, backend, ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("build goose provider: %w", err)
|
||||
}
|
||||
if _, err := provider.Up(context.Background()); err != nil {
|
||||
return fmt.Errorf("goose up failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user