-
Notifications
You must be signed in to change notification settings - Fork 523
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
Missing migrations handling. BMP-596 #72
Closed
Stepych
wants to merge
14
commits into
pressly:master
from
mc2soft:bayburtyan_apply_missing_migrations
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8e5ca2d
.gitignore, renaming pressly/goose to mc2soft/goose, README - fork go…
Stepych d956973
New example - go-migrations-package BMP-596
Stepych e6e65d2
Support for missing migrations. New commands: status-missing, up-miss…
Stepych 19e35a6
up-missing is alternative to all other up* commands, no way other! BM…
Stepych bcbb526
New option --missing-only for status command, instead of status-missi…
Stepych 66b3d7a
UpMissing logic replaces old GetDBVersion-based migration applying lo…
Stepych af054a9
Link to the example at the fork's goal description.
Stepych e5f5e42
New option -pretend - run migrations without applying them - only upd…
Stepych ef88f76
[refactoring] Option --missing-only for status command is flag now.
Stepych e922cbc
go fmt
Stepych 155caad
Ignoring .db (SQLite DB files from examples).
Stepych aa2f7cc
fix example migrations file names.
Stepych ef50ae1
Command up-to VERSION returns with missing migrations support.
Stepych 85c3aa1
Review fix: revert adding "-pretend" feature.
Stepych 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 |
---|---|---|
|
@@ -2,3 +2,14 @@ | |
cmd/goose/goose* | ||
*.swp | ||
*.test | ||
|
||
# IDE | ||
.vscode/ | ||
*.iml | ||
.idea | ||
|
||
# Mac | ||
.DS_Store | ||
|
||
*.db | ||
|
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 |
---|---|---|
|
@@ -7,7 +7,7 @@ import ( | |
"log" | ||
"os" | ||
|
||
"github.com/pressly/goose" | ||
"github.com/mc2soft/goose" | ||
|
||
// Init DB drivers. | ||
_ "github.com/go-sql-driver/mysql" | ||
|
@@ -17,8 +17,9 @@ import ( | |
) | ||
|
||
var ( | ||
flags = flag.NewFlagSet("goose", flag.ExitOnError) | ||
dir = flags.String("dir", ".", "directory with migration files") | ||
flags = flag.NewFlagSet("goose", flag.ExitOnError) | ||
dir = flags.String("dir", ".", "directory with migration files") | ||
missingOnly = flags.Bool("missing-only", false, "for status command - find out only migrations, missing from the current DB") | ||
) | ||
|
||
func main() { | ||
|
@@ -28,7 +29,7 @@ func main() { | |
args := flags.Args() | ||
|
||
if len(args) > 1 && args[0] == "create" { | ||
if err := goose.Run("create", nil, *dir, args[1:]...); err != nil { | ||
if err := goose.Run("create", nil, *dir, *missingOnly, args[1:]...); err != nil { | ||
log.Fatalf("goose run: %v", err) | ||
} | ||
return | ||
|
@@ -75,7 +76,7 @@ func main() { | |
arguments = append(arguments, args[3:]...) | ||
} | ||
|
||
if err := goose.Run(command, db, *dir, arguments...); err != nil { | ||
if err := goose.Run(command, db, *dir, *missingOnly, arguments...); err != nil { | ||
log.Fatalf("goose run: %v", err) | ||
} | ||
} | ||
|
@@ -107,17 +108,22 @@ Examples: | |
goose redshift "postgres://user:[email protected]:5439/db" status | ||
|
||
Options: | ||
-dir string | ||
directory with migration files (default ".") | ||
-missing-only | ||
for status command - find out only migrations, missing from the current DB | ||
` | ||
|
||
usageCommands = ` | ||
Commands: | ||
up Migrate the DB to the most recent version available | ||
up-by-one Migrate up by a single version | ||
up-to VERSION Migrate the DB to a specific VERSION | ||
down Roll back the version by 1 | ||
down-to VERSION Roll back to a specific VERSION | ||
redo Re-run the latest migration | ||
reset Roll back all migrations | ||
status Dump the migration status for the current DB | ||
status Dump the migration status for the current DB. Use [-missing-only] option to find out only migrations, missing from the current DB | ||
version Print the current version of the database | ||
create NAME [sql|go] Creates new migration file with next version | ||
` | ||
|
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# SQL + Go migrations | ||
|
||
## This example: Best practice: Split migrations into a standalone package | ||
|
||
Also take a look at "missing" migrations handling. | ||
|
||
```bash | ||
$ go run main.go -dir ./db/migrations sqlite3 ./db/foo.db status | ||
Applied At Migration | ||
======================================= | ||
Pending -- 00001_create_users_table.sql | ||
Pending -- 00002_rename_root.go | ||
Pending -- 00004_rename_admin.go | ||
$ go run main.go -dir ./db/migrations -missing-only sqlite3 ./db/foo.db status | ||
Missing migrations | ||
=========== | ||
00001_create_users_table.sql | ||
00002_rename_root.go | ||
00004_rename_admin.go | ||
$ go run main.go -dir ./db/migrations sqlite3 ./db/foo.db up | ||
OK 00001_create_users_table.sql | ||
OK 00002_rename_root.go | ||
OK 00004_rename_admin.go | ||
$ go run main.go -dir ./db/migrations sqlite3 ./db/foo.db status | ||
Applied At Migration | ||
======================================= | ||
Fri Oct 6 13:03:09 2017 -- 00001_create_users_table.sql | ||
Fri Oct 6 13:03:09 2017 -- 00002_rename_root.go | ||
Fri Oct 6 13:03:09 2017 -- 00004_rename_admin.go | ||
$ go run main.go -dir ./db/migrations -missing-only sqlite3 ./db/foo.db status | ||
goose: no missing migrations | ||
|
||
``` | ||
Get "missing" migrations: remove "_" at _00003 and _00005 migrations, then do: | ||
```bash | ||
$ go run main.go -dir ./db/migrations sqlite3 ./db/foo.db status | ||
Applied At Migration | ||
======================================= | ||
Fri Oct 6 13:03:09 2017 -- 00001_create_users_table.sql | ||
Fri Oct 6 13:03:09 2017 -- 00002_rename_root.go | ||
Pending -- 00003_rename_admin.go | ||
Fri Oct 6 13:03:09 2017 -- 00004_rename_admin.go | ||
Pending -- 00005_rename_admin.go | ||
$ go run main.go -dir ./db/migrations -missing-only sqlite3 ./db/foo.db status | ||
Missing migrations | ||
=========== | ||
00003_rename_admin.go | ||
00005_rename_admin.go | ||
$ go run main.go -dir ./db/migrations sqlite3 ./db/foo.db up-by-one | ||
OK 00003_rename_admin.go | ||
$ go run main.go -dir ./db/migrations sqlite3 ./db/foo.db up-by-one | ||
OK 00005_rename_admin.go | ||
$ go run main.go -dir ./db/migrations sqlite3 ./db/foo.db status | ||
Applied At Migration | ||
======================================= | ||
Fri Oct 6 13:03:09 2017 -- 00001_create_users_table.sql | ||
Fri Oct 6 13:03:09 2017 -- 00002_rename_root.go | ||
Fri Oct 6 13:20:52 2017 -- 00003_rename_admin.go | ||
Fri Oct 6 13:03:09 2017 -- 00004_rename_admin.go | ||
Fri Oct 6 13:20:56 2017 -- 00005_rename_admin.go | ||
$ go run main.go -dir ./db/migrations sqlite3 ./db/foo.db down | ||
OK 00005_rename_admin.go | ||
$ go run main.go -dir ./db/migrations sqlite3 ./db/foo.db down | ||
OK 00003_rename_admin.go | ||
$ go run main.go -dir ./db/migrations sqlite3 ./db/foo.db down | ||
OK 00004_rename_admin.go | ||
$ go run main.go -dir ./db/migrations sqlite3 ./db/foo.db down | ||
OK 00002_rename_root.go | ||
$ go run main.go -dir ./db/migrations sqlite3 ./db/foo.db down | ||
OK 00001_create_users_table.sql | ||
$ go run main.go -dir ./db/migrations sqlite3 ./db/foo.db down | ||
2017/10/06 16:21:27 goose run: no migration 0 | ||
exit status 1 | ||
``` |
15 changes: 15 additions & 0 deletions
15
examples/go-migrations-package/db/migrations/00001_create_users_table.sql
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- +goose NO TRANSACTION | ||
-- +goose Up | ||
CREATE TABLE users ( | ||
id int NOT NULL PRIMARY KEY, | ||
username text, | ||
name text, | ||
surname text | ||
); | ||
|
||
INSERT INTO users VALUES | ||
(0, 'root', '', ''), | ||
(1, 'stepych', 'Stepan', 'Bayburtyan'); | ||
|
||
-- +goose Down | ||
DROP TABLE users; |
27 changes: 27 additions & 0 deletions
27
examples/go-migrations-package/db/migrations/00002_rename_root.go
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package migrations | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/mc2soft/goose" | ||
) | ||
|
||
func init() { | ||
goose.AddMigration(Up00002, Down00002) | ||
} | ||
|
||
func Up00002(tx *sql.Tx) error { | ||
_, err := tx.Exec("UPDATE users SET username='admin' WHERE username='root';") | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func Down00002(tx *sql.Tx) error { | ||
_, err := tx.Exec("UPDATE users SET username='root' WHERE username='admin';") | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
27 changes: 27 additions & 0 deletions
27
examples/go-migrations-package/db/migrations/00004_rename_admin.go
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package migrations | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/mc2soft/goose" | ||
) | ||
|
||
func init() { | ||
goose.AddMigration(Up00004, Down00004) | ||
} | ||
|
||
func Up00004(tx *sql.Tx) error { | ||
_, err := tx.Exec("UPDATE users SET username='admin4' WHERE username='admin';") | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func Down00004(tx *sql.Tx) error { | ||
_, err := tx.Exec("UPDATE users SET username='admin' WHERE username='admin4';") | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
27 changes: 27 additions & 0 deletions
27
examples/go-migrations-package/db/migrations/_00003_rename_admin.go
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package migrations | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/mc2soft/goose" | ||
) | ||
|
||
func init() { | ||
goose.AddMigration(Up00003, Down00003) | ||
} | ||
|
||
func Up00003(tx *sql.Tx) error { | ||
_, err := tx.Exec("UPDATE users SET username='admin3' WHERE username='admin4';") | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func Down00003(tx *sql.Tx) error { | ||
_, err := tx.Exec("UPDATE users SET username='admin4' WHERE username='admin3';") | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
27 changes: 27 additions & 0 deletions
27
examples/go-migrations-package/db/migrations/_00005_rename_admin.go
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package migrations | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/mc2soft/goose" | ||
) | ||
|
||
func init() { | ||
goose.AddMigration(Up00005, Down00005) | ||
} | ||
|
||
func Up00005(tx *sql.Tx) error { | ||
_, err := tx.Exec("UPDATE users SET username='admin5' WHERE username='admin3';") | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func Down00005(tx *sql.Tx) error { | ||
_, err := tx.Exec("UPDATE users SET username='admin3' WHERE username='admin5';") | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
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.
You don't expect us to merge this change
github.com/mc2soft/goose
, do you? :)Can you iron this PR out and provide a high-level overview of what is this PR supposed to solve? Thanks!
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 PR resolves #73. Ok, i will iron it to avoid "mc2soft" mentions)) But pls give me feedback, do you consider such issue?
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.
So in the high-level, this would find all the missing migrations -- ie. all migrations that are available but were not applied in the DB, right?
You can
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.
Yes, with new commands:
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.
Now, do we need 1. at all? Can't
goose status
show these migrations as well by default?Imho, 2. is explicit enough. I like it. It might even help people who insist on using Timestamp based migrations #63.
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.
status
show all migrations (could be a lot of them!), including missing ("pending"). But the newstatus-missing
shows only missing migrations.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.
I'd like someone from #63 to test this feature. I don't really have the same use case. Can you ping these guys, once you have the PR ready?