bbgo_origin/pkg/migrations/sqlite3/migration_api.go
2021-02-17 17:35:11 +08:00

60 lines
1.4 KiB
Go

package sqlite3
import (
"fmt"
"runtime"
"strings"
"github.com/c9s/rockhopper"
)
var registeredGoMigrations map[int64]*rockhopper.Migration
func Migrations() rockhopper.MigrationSlice {
var migrations = rockhopper.MigrationSlice{}
for _, migration := range registeredGoMigrations {
migrations = append(migrations, migration)
}
return migrations.SortAndConnect()
}
// AddMigration adds a migration.
func AddMigration(up, down rockhopper.TransactionHandler) {
pc, filename, _, _ := runtime.Caller(1)
funcName := runtime.FuncForPC(pc).Name()
lastSlash := strings.LastIndexByte(funcName, '/')
if lastSlash < 0 {
lastSlash = 0
}
lastDot := strings.LastIndexByte(funcName[lastSlash:], '.') + lastSlash
packageName := funcName[:lastDot]
AddNamedMigration(packageName, filename, up, down)
}
// AddNamedMigration : Add a named migration.
func AddNamedMigration(packageName, filename string, up, down rockhopper.TransactionHandler) {
if registeredGoMigrations == nil {
registeredGoMigrations = make(map[int64]*rockhopper.Migration)
}
v, _ := rockhopper.FileNumericComponent(filename)
migration := &rockhopper.Migration{
Package: packageName,
Registered: true,
Version: v,
UpFn: up,
DownFn: down,
Source: filename,
UseTx: true,
}
if existing, ok := registeredGoMigrations[v]; ok {
panic(fmt.Sprintf("failed to add migration %q: version conflicts with %q", filename, existing.Source))
}
registeredGoMigrations[v] = migration
}