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

Add type MigrationSet to allow concurrent usage of sql-migrate #156

Merged
merged 3 commits into from
Nov 20, 2019

Conversation

marema31
Copy link
Contributor

Usage of global variable in migrate package make difficult the concurrent usage of sql-migrate on two (or more) databases/schema.

This PR introduce a MigrationSet type that provide a way to instanciate sql-migrate with different parameters for databases.

To ensure backward compatibility the global variables (tableName and schemaName) are replaced by a MigrationSet instance and functions without receiver call their MigrationSet counterpart on this instance).

migrate_test.go Outdated
Migrations: sqliteMigrations[:1],
}

ms := NewMigrationSet("", "")
Copy link
Owner

Choose a reason for hiding this comment

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

Perhaps we should use a custom table name in this test?

// Shouldn't apply migration again
n, err = ms.Exec(s.Db, "sqlite3", migrations, Up)
c.Assert(err, IsNil)
c.Assert(n, Equals, 0)
Copy link
Owner

Choose a reason for hiding this comment

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

And then here do a direct query on the custom table name to verify that it actually wrote the migration record to that table?

This way we're validating that the supplied parameters actually work (and keep working in future releases).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will do the both to verify the default value

migrate.go Outdated
Comment on lines 29 to 45
type MigrationSet struct {
// Name of the table used to store migration info.
tableName string
// Name of a schema that the migration table be referenced.
schemaName string
}

// NewMigrationSet returns a parametrized Migration object
func NewMigrationSet(schemaName string, tableName string) MigrationSet {
if tableName == "" {
tableName = "gorp_migrations"
}
return MigrationSet{
tableName: tableName,
schemaName: schemaName,
}
}
Copy link
Owner

Choose a reason for hiding this comment

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

My only concern here is that an empty MigrationSet is an invalid configuration.

Usually in go the empty value of a struct is a useful one. Perhaps it's better to remove the tableName == "" check here and move it to where the value is actually used.

Once you do that, the members of MigrationSet can become public, which will allow users to use a custom one where they only override the schema name:

MigrationSet{
    SchemaName: "myschema",
}

And once we do that, perhaps we don't need NewMigrationSet() anymore? Just use the object initializer.

Copy link
Owner

Choose a reason for hiding this comment

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

To avoid multiple tableName == "", you could define a method MigrationSet to "retrieve the tableName if set, else default".

Copy link
Owner

Choose a reason for hiding this comment

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

Oh and this avoids an important risk in terms of backwards compatibility: suppose we ever want to add a third parameter to MigrationSet. As is currently, with NewMigrationSet(string, string), we'd need to add a third parameter to that function, which would break any code using it. That's not acceptable. Exposing the struct fields avoids that issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems legit to me, I'll propose a commit in this direction

@rubenv
Copy link
Owner

rubenv commented Nov 20, 2019

This makes total sense and doesn't break anything with regards to backwards compatibilty. Love it, thanks already.

The CI failures aren't caused by you (see #155), so we can ignore them for now.

Added some comments with regards to cleaning it up slightly in terms of usage and improving the test coverage a bit.

@rubenv
Copy link
Owner

rubenv commented Nov 20, 2019

Excellent, thanks a lot!

Merging! 🍾

@rubenv rubenv merged commit 7c4b0a9 into rubenv:master Nov 20, 2019
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