bbgo_origin/pkg/migrations/mysql/migration_api.go

92 lines
2.5 KiB
Go
Raw Normal View History

2021-02-17 09:28:05 +00:00
package mysql
import (
"fmt"
2022-04-30 17:23:04 +00:00
"log"
2021-02-17 09:28:05 +00:00
"runtime"
"strings"
2021-02-17 09:35:11 +00:00
2024-01-18 13:36:04 +00:00
"github.com/c9s/rockhopper/v2"
2021-02-17 09:28:05 +00:00
)
2024-01-24 07:56:04 +00:00
var registeredGoMigrations = map[rockhopper.RegistryKey]*rockhopper.Migration{}
2021-02-17 09:28:05 +00:00
2024-01-24 07:56:04 +00:00
func MergeMigrationsMap(ms map[rockhopper.RegistryKey]*rockhopper.Migration) {
2022-04-30 17:23:04 +00:00
for k, m := range ms {
if _, ok := registeredGoMigrations[k]; !ok {
registeredGoMigrations[k] = m
} else {
2024-01-18 13:36:04 +00:00
log.Printf("the migration key %+v is duplicated: %+v", k, m)
2022-04-30 17:23:04 +00:00
}
}
}
2024-01-24 07:56:04 +00:00
func GetMigrationsMap() map[rockhopper.RegistryKey]*rockhopper.Migration {
2022-04-30 17:23:04 +00:00
return registeredGoMigrations
}
// SortedMigrations builds up the migration objects, sort them by timestamp and return as a slice
func SortedMigrations() rockhopper.MigrationSlice {
return Migrations()
}
// Migrations builds up the migration objects, sort them by timestamp and return as a slice
2021-02-17 09:35:11 +00:00
func Migrations() rockhopper.MigrationSlice {
var migrations = rockhopper.MigrationSlice{}
for _, migration := range registeredGoMigrations {
migrations = append(migrations, migration)
}
return migrations.SortAndConnect()
}
2022-04-30 17:23:04 +00:00
// AddMigration adds a migration with its runtime caller information
2024-01-18 13:36:04 +00:00
func AddMigration(packageName string, up, down rockhopper.TransactionHandler) {
2021-02-17 09:28:05 +00:00
pc, filename, _, _ := runtime.Caller(1)
2024-01-18 13:36:04 +00:00
if packageName == "" {
funcName := runtime.FuncForPC(pc).Name()
packageName = _parseFuncPackageName(funcName)
}
2022-04-30 17:23:04 +00:00
AddNamedMigration(packageName, filename, up, down)
}
// parseFuncPackageName parses the package name from a given runtime caller function name
func _parseFuncPackageName(funcName string) string {
2021-02-17 09:28:05 +00:00
lastSlash := strings.LastIndexByte(funcName, '/')
if lastSlash < 0 {
lastSlash = 0
}
2022-04-30 17:23:04 +00:00
2021-02-17 09:28:05 +00:00
lastDot := strings.LastIndexByte(funcName[lastSlash:], '.') + lastSlash
packageName := funcName[:lastDot]
2022-04-30 17:23:04 +00:00
return packageName
2021-02-17 09:28:05 +00:00
}
2022-04-30 17:23:04 +00:00
// AddNamedMigration adds a named migration to the registered go migration map
2021-02-17 09:28:05 +00:00
func AddNamedMigration(packageName, filename string, up, down rockhopper.TransactionHandler) {
2024-01-18 13:36:04 +00:00
v, err := rockhopper.FileNumericComponent(filename)
if err != nil {
panic(fmt.Errorf("unable to parse numeric component from filename %s: %v", filename, err))
}
2021-02-17 09:28:05 +00:00
migration := &rockhopper.Migration{
Package: packageName,
Registered: true,
Version: v,
UpFn: up,
DownFn: down,
Source: filename,
UseTx: true,
}
2024-01-24 07:56:04 +00:00
key := rockhopper.RegistryKey{Package: packageName, Version: v}
2024-01-18 13:36:04 +00:00
if existing, ok := registeredGoMigrations[key]; ok {
panic(fmt.Sprintf("failed to add migration %q: version conflicts with key %+v: %+v", filename, key, existing))
2021-02-17 09:28:05 +00:00
}
2024-01-18 13:36:04 +00:00
registeredGoMigrations[key] = migration
2021-02-17 09:28:05 +00:00
}