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
fix: sorting of down migrations #657
Merged
aeneasr
merged 2 commits into
gobuffalo:development
from
zepatrik:fix-down-migration-specificity
Aug 10, 2021
Merged
Changes from 1 commit
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.
Jump to
Jump to file
Failed to load files.
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
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
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
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
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
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 |
---|---|---|
|
@@ -23,10 +23,6 @@ var mrx = regexp.MustCompile(`^(\d+)_([^.]+)(\.[a-z0-9]+)?\.(up|down)\.(sql|fizz | |
func NewMigrator(c *Connection) Migrator { | ||
return Migrator{ | ||
Connection: c, | ||
Migrations: map[string]Migrations{ | ||
"up": {}, | ||
"down": {}, | ||
}, | ||
} | ||
} | ||
|
||
|
@@ -35,9 +31,10 @@ func NewMigrator(c *Connection) Migrator { | |
// When building a new migration system, you should embed this | ||
// type into your migrator. | ||
type Migrator struct { | ||
Connection *Connection | ||
SchemaPath string | ||
Migrations map[string]Migrations | ||
Connection *Connection | ||
SchemaPath string | ||
UpMigrations UpMigrations | ||
DownMigrations DownMigrations | ||
} | ||
|
||
func (m Migrator) migrationIsCompatible(d dialect, mi Migration) bool { | ||
|
@@ -53,10 +50,10 @@ func (m Migrator) UpLogOnly() error { | |
c := m.Connection | ||
return m.exec(func() error { | ||
mtn := c.MigrationTableName() | ||
mfs := m.Migrations["up"] | ||
mfs := m.UpMigrations | ||
sort.Sort(mfs) | ||
return c.Transaction(func(tx *Connection) error { | ||
for _, mi := range mfs { | ||
for _, mi := range mfs.Migrations { | ||
if !m.migrationIsCompatible(c.Dialect, mi) { | ||
continue | ||
} | ||
|
@@ -89,12 +86,12 @@ func (m Migrator) UpTo(step int) (applied int, err error) { | |
c := m.Connection | ||
err = m.exec(func() error { | ||
mtn := c.MigrationTableName() | ||
mfs := m.Migrations["up"] | ||
mfs := m.UpMigrations | ||
mfs.Filter(func(mf Migration) bool { | ||
return m.migrationIsCompatible(c.Dialect, mf) | ||
}) | ||
sort.Sort(mfs) | ||
for _, mi := range mfs { | ||
for _, mi := range mfs.Migrations { | ||
exists, err := c.Where("version = ?", mi.Version).Exists(mtn) | ||
if err != nil { | ||
return errors.Wrapf(err, "problem checking for migration version %s", mi.Version) | ||
|
@@ -139,20 +136,20 @@ func (m Migrator) Down(step int) error { | |
if err != nil { | ||
return errors.Wrap(err, "migration down: unable count existing migration") | ||
} | ||
mfs := m.Migrations["down"] | ||
mfs := m.DownMigrations | ||
mfs.Filter(func(mf Migration) bool { | ||
return m.migrationIsCompatible(c.Dialect, mf) | ||
}) | ||
sort.Sort(sort.Reverse(mfs)) | ||
sort.Sort(mfs) | ||
Comment on lines
-146
to
+143
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. The important change is here. |
||
// skip all ran migration | ||
if len(mfs) > count { | ||
mfs = mfs[len(mfs)-count:] | ||
if len(mfs.Migrations) > count { | ||
mfs.Migrations = mfs.Migrations[len(mfs.Migrations)-count:] | ||
} | ||
// run only required steps | ||
if step > 0 && len(mfs) >= step { | ||
mfs = mfs[:step] | ||
if step > 0 && len(mfs.Migrations) >= step { | ||
mfs.Migrations = mfs.Migrations[:step] | ||
} | ||
for _, mi := range mfs { | ||
for _, mi := range mfs.Migrations { | ||
exists, err := c.Where("version = ?", mi.Version).Exists(mtn) | ||
if err != nil { | ||
return errors.Wrapf(err, "problem checking for migration version %s", mi.Version) | ||
|
@@ -228,7 +225,7 @@ func (m Migrator) Status(out io.Writer) error { | |
} | ||
w := tabwriter.NewWriter(out, 0, 0, 3, ' ', tabwriter.TabIndent) | ||
_, _ = fmt.Fprintln(w, "Version\tName\tStatus\t") | ||
for _, mf := range m.Migrations["up"] { | ||
for _, mf := range m.UpMigrations.Migrations { | ||
exists, err := m.Connection.Where("version = ?", mf.Version).Exists(m.Connection.MigrationTableName()) | ||
if err != nil { | ||
return errors.Wrapf(err, "problem with migration") | ||
|
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.
and also that these both sort
all
migrations to the back, but are reversed in regards to the version.