compile and update migration package

This commit is contained in:
c9s 2022-05-01 01:16:10 +08:00
parent 486cf50a9c
commit ce54e917a2
2 changed files with 6 additions and 62 deletions

View File

@ -2,7 +2,6 @@ package mysql
import ( import (
"fmt" "fmt"
"log"
"runtime" "runtime"
"strings" "strings"
@ -11,26 +10,6 @@ import (
var registeredGoMigrations map[int64]*rockhopper.Migration 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 { func Migrations() rockhopper.MigrationSlice {
var migrations = rockhopper.MigrationSlice{} var migrations = rockhopper.MigrationSlice{}
for _, migration := range registeredGoMigrations { for _, migration := range registeredGoMigrations {
@ -40,28 +19,21 @@ func Migrations() rockhopper.MigrationSlice {
return migrations.SortAndConnect() return migrations.SortAndConnect()
} }
// AddMigration adds a migration with its runtime caller information // AddMigration adds a migration.
func AddMigration(up, down rockhopper.TransactionHandler) { func AddMigration(up, down rockhopper.TransactionHandler) {
pc, filename, _, _ := runtime.Caller(1) pc, filename, _, _ := runtime.Caller(1)
funcName := runtime.FuncForPC(pc).Name() 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, '/') lastSlash := strings.LastIndexByte(funcName, '/')
if lastSlash < 0 { if lastSlash < 0 {
lastSlash = 0 lastSlash = 0
} }
lastDot := strings.LastIndexByte(funcName[lastSlash:], '.') + lastSlash lastDot := strings.LastIndexByte(funcName[lastSlash:], '.') + lastSlash
packageName := funcName[:lastDot] packageName := funcName[:lastDot]
return packageName AddNamedMigration(packageName, filename, up, down)
} }
// AddNamedMigration adds a named migration to the registered go migration map // AddNamedMigration : Add a named migration.
func AddNamedMigration(packageName, filename string, up, down rockhopper.TransactionHandler) { func AddNamedMigration(packageName, filename string, up, down rockhopper.TransactionHandler) {
if registeredGoMigrations == nil { if registeredGoMigrations == nil {
registeredGoMigrations = make(map[int64]*rockhopper.Migration) registeredGoMigrations = make(map[int64]*rockhopper.Migration)

View File

@ -2,7 +2,6 @@ package sqlite3
import ( import (
"fmt" "fmt"
"log"
"runtime" "runtime"
"strings" "strings"
@ -11,26 +10,6 @@ import (
var registeredGoMigrations map[int64]*rockhopper.Migration 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 { func Migrations() rockhopper.MigrationSlice {
var migrations = rockhopper.MigrationSlice{} var migrations = rockhopper.MigrationSlice{}
for _, migration := range registeredGoMigrations { for _, migration := range registeredGoMigrations {
@ -40,28 +19,21 @@ func Migrations() rockhopper.MigrationSlice {
return migrations.SortAndConnect() return migrations.SortAndConnect()
} }
// AddMigration adds a migration with its runtime caller information // AddMigration adds a migration.
func AddMigration(up, down rockhopper.TransactionHandler) { func AddMigration(up, down rockhopper.TransactionHandler) {
pc, filename, _, _ := runtime.Caller(1) pc, filename, _, _ := runtime.Caller(1)
funcName := runtime.FuncForPC(pc).Name() 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, '/') lastSlash := strings.LastIndexByte(funcName, '/')
if lastSlash < 0 { if lastSlash < 0 {
lastSlash = 0 lastSlash = 0
} }
lastDot := strings.LastIndexByte(funcName[lastSlash:], '.') + lastSlash lastDot := strings.LastIndexByte(funcName[lastSlash:], '.') + lastSlash
packageName := funcName[:lastDot] packageName := funcName[:lastDot]
return packageName AddNamedMigration(packageName, filename, up, down)
} }
// AddNamedMigration adds a named migration to the registered go migration map // AddNamedMigration : Add a named migration.
func AddNamedMigration(packageName, filename string, up, down rockhopper.TransactionHandler) { func AddNamedMigration(packageName, filename string, up, down rockhopper.TransactionHandler) {
if registeredGoMigrations == nil { if registeredGoMigrations == nil {
registeredGoMigrations = make(map[int64]*rockhopper.Migration) registeredGoMigrations = make(map[int64]*rockhopper.Migration)