From 085ba1e323b70ea1bcfb88a8a8cdbb86986b10ad Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 26 Apr 2022 18:48:27 +0800 Subject: [PATCH] compile and update migration package --- pkg/migrations/mysql/migration_api.go | 34 ++++++++++++++++++-- pkg/migrations/mysql/migration_api_test.go | 20 ++++++++++++ pkg/migrations/sqlite3/migration_api.go | 34 ++++++++++++++++++-- pkg/migrations/sqlite3/migration_api_test.go | 20 ++++++++++++ 4 files changed, 102 insertions(+), 6 deletions(-) create mode 100644 pkg/migrations/mysql/migration_api_test.go create mode 100644 pkg/migrations/sqlite3/migration_api_test.go diff --git a/pkg/migrations/mysql/migration_api.go b/pkg/migrations/mysql/migration_api.go index 95753ff3f..b68c5f81d 100644 --- a/pkg/migrations/mysql/migration_api.go +++ b/pkg/migrations/mysql/migration_api.go @@ -2,6 +2,7 @@ package mysql import ( "fmt" + "log" "runtime" "strings" @@ -10,6 +11,26 @@ import ( var registeredGoMigrations map[int64]*rockhopper.Migration +func MergeMigrationsMap(ms map[int64]*rockhopper.Migration) { + for k, m := range ms { + if _, ok := registeredGoMigrations[k]; !ok { + registeredGoMigrations[k] = m + } else { + log.Printf("the migration key %d is duplicated: %+v", k, m) + } + } +} + +func GetMigrationsMap() map[int64]*rockhopper.Migration { + 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 func Migrations() rockhopper.MigrationSlice { var migrations = rockhopper.MigrationSlice{} for _, migration := range registeredGoMigrations { @@ -19,21 +40,28 @@ func Migrations() rockhopper.MigrationSlice { return migrations.SortAndConnect() } -// AddMigration adds a migration. +// AddMigration adds a migration with its runtime caller information func AddMigration(up, down rockhopper.TransactionHandler) { pc, filename, _, _ := runtime.Caller(1) funcName := runtime.FuncForPC(pc).Name() + packageName := _parseFuncPackageName(funcName) + AddNamedMigration(packageName, filename, up, down) +} + +// parseFuncPackageName parses the package name from a given runtime caller function name +func _parseFuncPackageName(funcName string) string { lastSlash := strings.LastIndexByte(funcName, '/') if lastSlash < 0 { lastSlash = 0 } + lastDot := strings.LastIndexByte(funcName[lastSlash:], '.') + lastSlash packageName := funcName[:lastDot] - AddNamedMigration(packageName, filename, up, down) + return packageName } -// AddNamedMigration : Add a named migration. +// AddNamedMigration adds a named migration to the registered go migration map func AddNamedMigration(packageName, filename string, up, down rockhopper.TransactionHandler) { if registeredGoMigrations == nil { registeredGoMigrations = make(map[int64]*rockhopper.Migration) diff --git a/pkg/migrations/mysql/migration_api_test.go b/pkg/migrations/mysql/migration_api_test.go new file mode 100644 index 000000000..2684076d3 --- /dev/null +++ b/pkg/migrations/mysql/migration_api_test.go @@ -0,0 +1,20 @@ +package mysql + +import ( + "testing" + + "github.com/c9s/rockhopper" + "github.com/stretchr/testify/assert" +) + +func TestGetMigrationsMap(t *testing.T) { + mm := GetMigrationsMap() + assert.NotEmpty(t, mm) +} + +func TestMergeMigrationsMap(t *testing.T) { + MergeMigrationsMap(map[int64]*rockhopper.Migration{ + 2: &rockhopper.Migration{}, + 3: &rockhopper.Migration{}, + }) +} diff --git a/pkg/migrations/sqlite3/migration_api.go b/pkg/migrations/sqlite3/migration_api.go index 388f397f1..eecc54695 100644 --- a/pkg/migrations/sqlite3/migration_api.go +++ b/pkg/migrations/sqlite3/migration_api.go @@ -2,6 +2,7 @@ package sqlite3 import ( "fmt" + "log" "runtime" "strings" @@ -10,6 +11,26 @@ import ( var registeredGoMigrations map[int64]*rockhopper.Migration +func MergeMigrationsMap(ms map[int64]*rockhopper.Migration) { + for k, m := range ms { + if _, ok := registeredGoMigrations[k]; !ok { + registeredGoMigrations[k] = m + } else { + log.Printf("the migration key %d is duplicated: %+v", k, m) + } + } +} + +func GetMigrationsMap() map[int64]*rockhopper.Migration { + 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 func Migrations() rockhopper.MigrationSlice { var migrations = rockhopper.MigrationSlice{} for _, migration := range registeredGoMigrations { @@ -19,21 +40,28 @@ func Migrations() rockhopper.MigrationSlice { return migrations.SortAndConnect() } -// AddMigration adds a migration. +// AddMigration adds a migration with its runtime caller information func AddMigration(up, down rockhopper.TransactionHandler) { pc, filename, _, _ := runtime.Caller(1) funcName := runtime.FuncForPC(pc).Name() + packageName := _parseFuncPackageName(funcName) + AddNamedMigration(packageName, filename, up, down) +} + +// parseFuncPackageName parses the package name from a given runtime caller function name +func _parseFuncPackageName(funcName string) string { lastSlash := strings.LastIndexByte(funcName, '/') if lastSlash < 0 { lastSlash = 0 } + lastDot := strings.LastIndexByte(funcName[lastSlash:], '.') + lastSlash packageName := funcName[:lastDot] - AddNamedMigration(packageName, filename, up, down) + return packageName } -// AddNamedMigration : Add a named migration. +// AddNamedMigration adds a named migration to the registered go migration map func AddNamedMigration(packageName, filename string, up, down rockhopper.TransactionHandler) { if registeredGoMigrations == nil { registeredGoMigrations = make(map[int64]*rockhopper.Migration) diff --git a/pkg/migrations/sqlite3/migration_api_test.go b/pkg/migrations/sqlite3/migration_api_test.go new file mode 100644 index 000000000..d1f4fe1ab --- /dev/null +++ b/pkg/migrations/sqlite3/migration_api_test.go @@ -0,0 +1,20 @@ +package sqlite3 + +import ( + "testing" + + "github.com/c9s/rockhopper" + "github.com/stretchr/testify/assert" +) + +func TestGetMigrationsMap(t *testing.T) { + mm := GetMigrationsMap() + assert.NotEmpty(t, mm) +} + +func TestMergeMigrationsMap(t *testing.T) { + MergeMigrationsMap(map[int64]*rockhopper.Migration{ + 2: &rockhopper.Migration{}, + 3: &rockhopper.Migration{}, + }) +}