Skip to content
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

v2 #2

Merged
merged 6 commits into from
Jan 23, 2018
Merged

v2 #2

merged 6 commits into from
Jan 23, 2018

Conversation

josephbuchma
Copy link
Member

@josephbuchma josephbuchma commented Dec 26, 2017

Upd for db-journey/migrate#3 and some extra features:

  • Implemented driver.Lockable interface
  • Custom transactions / disable transactions

@josephbuchma josephbuchma changed the title Improvements, refactoring v2 Dec 26, 2017
- Implemented FileTemplate method
- Locking during migrations
- Custom transactions / disable transactions
@josephbuchma josephbuchma force-pushed the v2 branch 2 times, most recently from 9e1a0be to 8738ef5 Compare December 29, 2017 01:29
mysql.go Outdated
func (drv *Driver) Lock() error {
// NOTE: versionTx will be implicitly committed by LOCK TABLES
// the only reason we need it is to get exclusive db connection.
// TODO: go1.9 has DB.Conn() which returns exclusive connection, use it when time comes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the time already?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use 1.9 in production yet, waiting some time before upgrading minor version of Go is quite popular practice. I built custom migrations appliance on top of migrate package. Moving it to DB.Conn() is not a big deal, let's wait a while.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok fine

mysql.go Outdated
versionUpdSQL = "DELETE FROM " + versionsTableName + " WHERE version = ?"
}
if _, err = drv.versionTx.Exec(versionUpdSQL, f.Version); err != nil {
drv.rollbackVersionTx()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the record, if the rollback fails, the error is lost here.

mysql.go Outdated
type migration struct {
// notx determines if default transaction
// should be disabled
notx bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be noTx

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to avoid reading notX

mysql.go Outdated
return fmt.Errorf("Failed to commit lines %d-%d: %s", s.txbegin, s.txend, err)
}

// newStmt is a DRYer for migration.parse
Copy link
Member

@gravis gravis Jan 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong func name

mysql.go Outdated
// It locks schema_migrations table, so concurrent execution is safe.
func (drv *Driver) Migrate(f file.File) error {
if drv.versionTx == nil {
return errors.New("migrate must call Lock before Migrate")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you lock here instead of returning an error?
There could be an unlock added as a defer too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we could lock/unlock here, there would be no reason for exposing Lock/Unlock methods in driver interface. migrate package is responsible for locking

Copy link
Member

@gravis gravis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall very good work!
I just have minor issues to be addressed before merging this.
Thanks!

Place SQL between "-- TXBEGIN" and "-- TXEND" comments for custom transaction:
- you CAN have multiple separate transactions in single migration
- any SQL not wrapped into TXBEGIN - TXEND will be executed without transaction.
Add "-- NOTX" comment above all SQL to disable default transaction. NOTE:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably unify this with the existing -- disable_ddl_transaction in pg driver

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to keep this "directive" comments short to avoid potential misspelling, or we should add some prefix (like ++ in goose) to apply validations and throw error if user misspelled something

@gravis gravis merged commit 1b63424 into db-journey:master Jan 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants