Skip to content

MySQL - use the database name in the config #1238

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions database/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func (m *Mysql) SetVersion(version int, dirty bool) error {
return &database.Error{OrigErr: err, Err: "transaction start failed"}
}

query := "DELETE FROM `" + m.config.MigrationsTable + "` LIMIT 1"
query := fmt.Sprintf("DELETE FROM `%s`.`%s` LIMIT 1", m.config.DatabaseName, m.config.MigrationsTable)
if _, err := tx.ExecContext(context.Background(), query); err != nil {
if errRollback := tx.Rollback(); errRollback != nil {
err = multierror.Append(err, errRollback)
Expand All @@ -373,7 +373,7 @@ func (m *Mysql) SetVersion(version int, dirty bool) error {
// empty schema version for failed down migration on the first migration
// See: https://github.com/golang-migrate/migrate/issues/330
if version >= 0 || (version == database.NilVersion && dirty) {
query := "INSERT INTO `" + m.config.MigrationsTable + "` (version, dirty) VALUES (?, ?)"
query := fmt.Sprintf("INSERT INTO `%s`.`%s` (version, dirty) VALUES (?, ?)", m.config.DatabaseName, m.config.MigrationsTable)
if _, err := tx.ExecContext(context.Background(), query, version, dirty); err != nil {
if errRollback := tx.Rollback(); errRollback != nil {
err = multierror.Append(err, errRollback)
Expand All @@ -390,7 +390,7 @@ func (m *Mysql) SetVersion(version int, dirty bool) error {
}

func (m *Mysql) Version() (version int, dirty bool, err error) {
query := "SELECT version, dirty FROM `" + m.config.MigrationsTable + "` LIMIT 1"
query := fmt.Sprintf("SELECT version, dirty FROM `%s`.`%s` LIMIT 1", m.config.DatabaseName, m.config.MigrationsTable)
err = m.conn.QueryRowContext(context.Background(), query).Scan(&version, &dirty)
switch {
case err == sql.ErrNoRows:
Expand All @@ -411,7 +411,7 @@ func (m *Mysql) Version() (version int, dirty bool, err error) {

func (m *Mysql) Drop() (err error) {
// select all tables
query := `SHOW TABLES LIKE '%'`
query := fmt.Sprintf("SHOW TABLES FROM `%s`", m.config.DatabaseName)
tables, err := m.conn.QueryContext(context.Background(), query)
if err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
Expand Down Expand Up @@ -451,7 +451,7 @@ func (m *Mysql) Drop() (err error) {

// delete one by one ...
for _, t := range tableNames {
query = "DROP TABLE IF EXISTS `" + t + "`"
query = fmt.Sprintf("DROP TABLE IF EXISTS `%s`.`%s`", m.config.DatabaseName, t)
if _, err := m.conn.ExecContext(context.Background(), query); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
Expand Down Expand Up @@ -481,7 +481,7 @@ func (m *Mysql) ensureVersionTable() (err error) {

// check if migration table exists
var result string
query := `SHOW TABLES LIKE '` + m.config.MigrationsTable + `'`
query := fmt.Sprintf("SHOW TABLES FROM `%s` LIKE '%s'", m.config.DatabaseName, m.config.MigrationsTable)
if err := m.conn.QueryRowContext(context.Background(), query).Scan(&result); err != nil {
if err != sql.ErrNoRows {
return &database.Error{OrigErr: err, Query: []byte(query)}
Expand All @@ -491,7 +491,7 @@ func (m *Mysql) ensureVersionTable() (err error) {
}

// if not, create the empty migration table
query = "CREATE TABLE `" + m.config.MigrationsTable + "` (version bigint not null primary key, dirty boolean not null)"
query = fmt.Sprintf("CREATE TABLE `%s`.`%s` (version bigint not null primary key, dirty boolean not null)", m.config.DatabaseName, m.config.MigrationsTable)
if _, err := m.conn.ExecContext(context.Background(), query); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
Expand Down
Loading