Skip to content

Commit

Permalink
Merge pull request #4 from c9s/c9s/simplify-api
Browse files Browse the repository at this point in the history
feature: simplify upgrade api
  • Loading branch information
c9s committed Jan 24, 2024
2 parents 8f7715a + 5890567 commit 2ba1ca8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 31 deletions.
31 changes: 7 additions & 24 deletions dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func TestGetMigrationsMap(t *testing.T) {
}
func TestMergeMigrationsMap(t *testing.T) {
MergeMigrationsMap(map[registryKey]*rockhopper.Migration{
registryKey{ Version: 2 }: &rockhopper.Migration{},
registryKey{ Version: 2 }: &rockhopper.Migration{},
MergeMigrationsMap(map[rockhopper.RegistryKey]*rockhopper.Migration{
rockhopper.RegistryKey{ Version: 2 }: &rockhopper.Migration{},
rockhopper.RegistryKey{ Version: 2 }: &rockhopper.Migration{},
})
}
Expand All @@ -59,14 +59,9 @@ import (
"github.com/c9s/rockhopper/v2"
)
type registryKey struct {
Package string
Version int64
}
var registeredGoMigrations = map[registryKey]*rockhopper.Migration{}
var registeredGoMigrations = map[rockhopper.RegistryKey]*rockhopper.Migration{}
func MergeMigrationsMap(ms map[registryKey]*rockhopper.Migration) {
func MergeMigrationsMap(ms map[rockhopper.RegistryKey]*rockhopper.Migration) {
for k, m := range ms {
if _, ok := registeredGoMigrations[k] ; !ok {
registeredGoMigrations[k] = m
Expand All @@ -76,7 +71,7 @@ func MergeMigrationsMap(ms map[registryKey]*rockhopper.Migration) {
}
}
func GetMigrationsMap() map[registryKey]*rockhopper.Migration {
func GetMigrationsMap() map[rockhopper.RegistryKey]*rockhopper.Migration {
return registeredGoMigrations
}
Expand Down Expand Up @@ -122,10 +117,6 @@ func _parseFuncPackageName(funcName string) string {
// 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[registryKey]*rockhopper.Migration)
}
v, err := rockhopper.FileNumericComponent(filename)
if err != nil {
panic(fmt.Errorf("unable to parse numeric component from filename %s: %v", filename, err))
Expand All @@ -142,7 +133,7 @@ func AddNamedMigration(packageName, filename string, up, down rockhopper.Transac
UseTx: true,
}
key := registryKey{ Package: packageName, Version: v}
key := rockhopper.RegistryKey{ Package: packageName, Version: v}
if existing, ok := registeredGoMigrations[key]; ok {
panic(fmt.Sprintf("failed to add migration %q: version conflicts with key %+v: %+v", filename, key, existing))
}
Expand All @@ -160,10 +151,6 @@ import (
func init() {
AddMigration({{ .Migration.Package | quote }}, up{{ .FuncNameBody }}, down{{ .FuncNameBody }})
{{ if .Global }}
rockhopper.AddMigration(up{{.FuncNameBody}}, down{{.FuncNameBody}})
{{ end }}
}
func up{{ .FuncNameBody }}(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
Expand Down Expand Up @@ -226,10 +213,6 @@ type migrationTemplateArgs struct {

// PackageName is the package name that will be used to render the go file.
PackageName string

// Global renders the migration template with the global migration registration calls.
// This parameter avoids migration version conflict for different sql dialect (or driver)
Global bool
}

var specialCharsRegExp = regexp.MustCompile("\\W")
Expand Down
15 changes: 8 additions & 7 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"fmt"
"runtime"
"strings"

log "github.com/sirupsen/logrus"
)

type registryKey struct {
type RegistryKey struct {
Package string
Version int64
}

// registeredGoMigrations stores the global registered migrations
// applications may register their compiledd migration scrips into this map
var registeredGoMigrations = map[registryKey]*Migration{}
var registeredGoMigrations = map[RegistryKey]*Migration{}

// AddMigration registers a migration to the global map
func AddMigration(up, down TransactionHandler) {
Expand All @@ -32,12 +34,11 @@ func AddMigration(up, down TransactionHandler) {

// AddNamedMigration registers a migration to the global map with a given name
func AddNamedMigration(packageName, filename string, up, down TransactionHandler) {
if registeredGoMigrations == nil {
registeredGoMigrations = make(map[registryKey]*Migration)
v, err := FileNumericComponent(filename)
if err != nil {
log.Panic(err)
}

v, _ := FileNumericComponent(filename)

migration := &Migration{
Package: packageName,
Registered: true,
Expand All @@ -49,7 +50,7 @@ func AddNamedMigration(packageName, filename string, up, down TransactionHandler
UseTx: true,
}

key := registryKey{Package: packageName, Version: v}
key := RegistryKey{Package: packageName, Version: v}
if existing, ok := registeredGoMigrations[key]; ok {
panic(fmt.Sprintf("failed to add migration %q: version conflicts with %q", filename, existing.Source))
}
Expand Down
38 changes: 38 additions & 0 deletions up.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,44 @@ func UpBySteps(ctx context.Context, db *DB, m *Migration, steps int, callbacks .
return nil
}

func Upgrade(ctx context.Context, db *DB, migrations MigrationSlice) error {
migrationMap := migrations.MapByPackage()
for _, pkgMigrations := range migrationMap {
_, lastAppliedMigration, err := db.FindLastAppliedMigration(ctx, pkgMigrations)
if err != nil {
return err
}

startMigration := pkgMigrations.Head()
if lastAppliedMigration != nil {
startMigration = lastAppliedMigration.Next
}

err = Up(ctx, db, startMigration, 0, func(m *Migration) {
// log.Infof("migration %d is applied", m.Version)
})

if err != nil {
return err
}
}

return nil
}

// UpgradeFromGo runs the migration upgrades from the registered go-code migration
// parameter packageNames is the package list you want to filter
func UpgradeFromGo(ctx context.Context, db *DB, packageNames ...string) error {
var migrations MigrationSlice
for _, migration := range registeredGoMigrations {
migrations = append(migrations, migration)
}

return Upgrade(ctx, db, migrations.FilterPackage(packageNames))
}

// Up executes the Up methods from the given migration object
// and continues the upgrades to the latest migration.
func Up(ctx context.Context, db *DB, m *Migration, to int64, callbacks ...func(m *Migration)) error {
for ; m != nil; m = m.Next {
if to > 0 && m.Version > to {
Expand Down

0 comments on commit 2ba1ca8

Please sign in to comment.