-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow running Golang based post migration steps #1253
Open
guggero
wants to merge
2
commits into
golang-migrate:master
Choose a base branch
from
guggero:post-migrate-exec
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,12 +55,71 @@ func (e ErrDirty) Error() string { | |
return fmt.Sprintf("Dirty database version %v. Fix and force version.", e.Version) | ||
} | ||
|
||
// PostStepCallback is a callback function type that can be used to execute a | ||
// Golang based migration step after a SQL based migration step has been | ||
// executed. The callback function receives the migration and the database | ||
// driver as arguments. | ||
type PostStepCallback func(migr *Migration, driver database.Driver) error | ||
|
||
// options is a set of optional options that can be set when a Migrate instance | ||
// is created. | ||
type options struct { | ||
// postStepCallbacks is a map of PostStepCallback functions that can be | ||
// used to execute a Golang based migration step after a SQL based | ||
// migration step has been executed. The key is the migration version | ||
// and the value is the callback function that should be run _after_ the | ||
// step was executed (but within the same database transaction). | ||
postStepCallbacks map[uint]PostStepCallback | ||
} | ||
|
||
// defaultOptions returns a new options struct with default values. | ||
func defaultOptions() options { | ||
return options{ | ||
postStepCallbacks: make(map[uint]PostStepCallback), | ||
} | ||
} | ||
|
||
// Option is a function that can be used to set options on a Migrate instance. | ||
type Option func(*options) | ||
|
||
// WithPostStepCallbacks is an option that can be used to set a map of | ||
// PostStepCallback functions that can be used to execute a Golang based | ||
// migration step after a SQL based migration step has been executed. The key is | ||
// the migration version and the value is the callback function that should be | ||
// run _after_ the step was executed (but before the version is marked as | ||
// cleanly executed). An error returned from the callback will cause the | ||
// migration to fail and the step to be marked as dirty. | ||
func WithPostStepCallbacks( | ||
postStepCallbacks map[uint]PostStepCallback) Option { | ||
|
||
return func(o *options) { | ||
o.postStepCallbacks = postStepCallbacks | ||
} | ||
} | ||
|
||
// WithPostStepCallback is an option that can be used to set a PostStepCallback | ||
// function that can be used to execute a Golang based migration step after the | ||
// SQL based migration step with the given version number has been executed. The | ||
// callback is the function that should be run _after_ the step was executed | ||
// (but before the version is marked as cleanly executed). An error returned | ||
// from the callback will cause the migration to fail and the step to be marked | ||
// as dirty. | ||
func WithPostStepCallback(version uint, callback PostStepCallback) Option { | ||
return func(o *options) { | ||
o.postStepCallbacks[version] = callback | ||
} | ||
} | ||
|
||
type Migrate struct { | ||
sourceName string | ||
sourceDrv source.Driver | ||
databaseName string | ||
databaseDrv database.Driver | ||
|
||
// opts is a set of options that can be used to modify the behavior | ||
// of the Migrate instance. | ||
opts options | ||
|
||
// Log accepts a Logger interface | ||
Log Logger | ||
|
||
|
@@ -84,8 +143,8 @@ type Migrate struct { | |
|
||
// New returns a new Migrate instance from a source URL and a database URL. | ||
// The URL scheme is defined by each driver. | ||
func New(sourceURL, databaseURL string) (*Migrate, error) { | ||
m := newCommon() | ||
func New(sourceURL, databaseURL string, opts ...Option) (*Migrate, error) { | ||
m := newMigrateWithOptions(opts) | ||
|
||
sourceName, err := iurl.SchemeFromURL(sourceURL) | ||
if err != nil { | ||
|
@@ -118,8 +177,10 @@ func New(sourceURL, databaseURL string) (*Migrate, error) { | |
// and an existing database instance. The source URL scheme is defined by each driver. | ||
// Use any string that can serve as an identifier during logging as databaseName. | ||
// You are responsible for closing the underlying database client if necessary. | ||
func NewWithDatabaseInstance(sourceURL string, databaseName string, databaseInstance database.Driver) (*Migrate, error) { | ||
m := newCommon() | ||
func NewWithDatabaseInstance(sourceURL string, databaseName string, | ||
databaseInstance database.Driver, opts ...Option) (*Migrate, error) { | ||
|
||
m := newMigrateWithOptions(opts) | ||
|
||
sourceName, err := iurl.SchemeFromURL(sourceURL) | ||
if err != nil { | ||
|
@@ -144,8 +205,10 @@ func NewWithDatabaseInstance(sourceURL string, databaseName string, databaseInst | |
// and a database URL. The database URL scheme is defined by each driver. | ||
// Use any string that can serve as an identifier during logging as sourceName. | ||
// You are responsible for closing the underlying source client if necessary. | ||
func NewWithSourceInstance(sourceName string, sourceInstance source.Driver, databaseURL string) (*Migrate, error) { | ||
m := newCommon() | ||
func NewWithSourceInstance(sourceName string, sourceInstance source.Driver, | ||
databaseURL string, opts ...Option) (*Migrate, error) { | ||
|
||
m := newMigrateWithOptions(opts) | ||
|
||
databaseName, err := iurl.SchemeFromURL(databaseURL) | ||
if err != nil { | ||
|
@@ -170,8 +233,11 @@ func NewWithSourceInstance(sourceName string, sourceInstance source.Driver, data | |
// database instance. Use any string that can serve as an identifier during logging | ||
// as sourceName and databaseName. You are responsible for closing down | ||
// the underlying source and database client if necessary. | ||
func NewWithInstance(sourceName string, sourceInstance source.Driver, databaseName string, databaseInstance database.Driver) (*Migrate, error) { | ||
m := newCommon() | ||
func NewWithInstance(sourceName string, sourceInstance source.Driver, | ||
databaseName string, databaseInstance database.Driver, | ||
opts ...Option) (*Migrate, error) { | ||
|
||
m := newMigrateWithOptions(opts) | ||
|
||
m.sourceName = sourceName | ||
m.databaseName = databaseName | ||
|
@@ -182,8 +248,13 @@ func NewWithInstance(sourceName string, sourceInstance source.Driver, databaseNa | |
return m, nil | ||
} | ||
|
||
func newCommon() *Migrate { | ||
func newMigrateWithOptions(optFunctions []Option) *Migrate { | ||
opts := defaultOptions() | ||
for _, opt := range optFunctions { | ||
opt(&opts) | ||
} | ||
return &Migrate{ | ||
opts: opts, | ||
GracefulStop: make(chan bool, 1), | ||
PrefetchMigrations: DefaultPrefetchMigrations, | ||
LockTimeout: DefaultLockTimeout, | ||
|
@@ -746,6 +817,22 @@ func (m *Migrate) runMigrations(ret <-chan interface{}) error { | |
if err := m.databaseDrv.Run(migr.BufferedBody); err != nil { | ||
return err | ||
} | ||
|
||
// If there is a post execution function for | ||
// this migration, run it now. | ||
cb, ok := m.opts.postStepCallbacks[migr.Version] | ||
if ok { | ||
m.logVerbosePrintf("Running post step "+ | ||
"callback for %v\n", migr.LogString()) | ||
|
||
err := cb(migr, m.databaseDrv) | ||
if err != nil { | ||
return err | ||
} | ||
Comment on lines
+828
to
+831
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should wrap
|
||
|
||
m.logVerbosePrintf("Post step callback "+ | ||
"finished for %v\n", migr.LogString()) | ||
} | ||
} | ||
|
||
// set clean state | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC if the post step callback fails, then the version remains dirty. This could end up being a problem requiring down migration from the user. I wonder if it'd be better to just run the migration, the callback and a final
SetVersion
in a transaction?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I agree. But the thing is: The version is also dirty if the SQL based migration fails, as we also error out then.
And there doesn't seem to be the concept of DB transactions in the migration tool, my guess is because not every supported database backend can do transactions...
So not really sure what to do differently here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This appears to be aa quirk related to the way the library works:
migrate/FAQ.md
Lines 62 to 65 in 604248c
In the context of our desired usage we typically catch situations like this via unit tests of the migration itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After reading a bit more of the codebase, I think it's possible to run the callback function in the same db transaction as the migration:
migrate/database/sqlite/sqlite.go
Lines 201 to 204 in 604248c
With the way the interfaces work, if we modify those (using something other than a func opt), then we'd need to update every single driver in the codebase.
Perhaps the slimmest change would be to add the functional opt to the
Run
method in the mainDriver
interface?