diff --git a/dialect.go b/dialect.go index 81831a979..488f5e040 100644 --- a/dialect.go +++ b/dialect.go @@ -8,7 +8,7 @@ import ( // SQLDialect abstracts the details of specific SQL dialects // for goose's few SQL specific statements type SQLDialect interface { - createVersionTableSQL() string // sql string to create the goose_db_version table + createVersionTableSQL() string // sql string to create the db version table insertVersionSQL() string // sql string to insert the initial version table row dbVersionQuery(db *sql.DB) (*sql.Rows, error) } @@ -48,21 +48,21 @@ func SetDialect(d string) error { type PostgresDialect struct{} func (pg PostgresDialect) createVersionTableSQL() string { - return `CREATE TABLE goose_db_version ( + return fmt.Sprintf(`CREATE TABLE %s ( id serial NOT NULL, version_id bigint NOT NULL, is_applied boolean NOT NULL, tstamp timestamp NULL default now(), PRIMARY KEY(id) - );` + );`, TableName()) } func (pg PostgresDialect) insertVersionSQL() string { - return "INSERT INTO goose_db_version (version_id, is_applied) VALUES ($1, $2);" + return fmt.Sprintf("INSERT INTO %s (version_id, is_applied) VALUES ($1, $2);", TableName()) } func (pg PostgresDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { - rows, err := db.Query("SELECT version_id, is_applied from goose_db_version ORDER BY id DESC") + rows, err := db.Query(fmt.Sprintf("SELECT version_id, is_applied from %s ORDER BY id DESC", TableName())) if err != nil { return nil, err } @@ -78,21 +78,21 @@ func (pg PostgresDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { type MySQLDialect struct{} func (m MySQLDialect) createVersionTableSQL() string { - return `CREATE TABLE goose_db_version ( + return fmt.Sprintf(`CREATE TABLE %s ( id serial NOT NULL, version_id bigint NOT NULL, is_applied boolean NOT NULL, tstamp timestamp NULL default now(), PRIMARY KEY(id) - );` + );`, TableName()) } func (m MySQLDialect) insertVersionSQL() string { - return "INSERT INTO goose_db_version (version_id, is_applied) VALUES (?, ?);" + return fmt.Sprintf("INSERT INTO %s (version_id, is_applied) VALUES (?, ?);", TableName()) } func (m MySQLDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { - rows, err := db.Query("SELECT version_id, is_applied from goose_db_version ORDER BY id DESC") + rows, err := db.Query(fmt.Sprintf("SELECT version_id, is_applied from %s ORDER BY id DESC", TableName())) if err != nil { return nil, err } @@ -108,20 +108,20 @@ func (m MySQLDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { type Sqlite3Dialect struct{} func (m Sqlite3Dialect) createVersionTableSQL() string { - return `CREATE TABLE goose_db_version ( + return fmt.Sprintf(`CREATE TABLE %s ( id INTEGER PRIMARY KEY AUTOINCREMENT, version_id INTEGER NOT NULL, is_applied INTEGER NOT NULL, tstamp TIMESTAMP DEFAULT (datetime('now')) - );` + );`, TableName()) } func (m Sqlite3Dialect) insertVersionSQL() string { - return "INSERT INTO goose_db_version (version_id, is_applied) VALUES (?, ?);" + return fmt.Sprintf("INSERT INTO %s (version_id, is_applied) VALUES (?, ?);", TableName()) } func (m Sqlite3Dialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { - rows, err := db.Query("SELECT version_id, is_applied from goose_db_version ORDER BY id DESC") + rows, err := db.Query(fmt.Sprintf("SELECT version_id, is_applied from %s ORDER BY id DESC", TableName())) if err != nil { return nil, err } @@ -137,21 +137,21 @@ func (m Sqlite3Dialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { type RedshiftDialect struct{} func (rs RedshiftDialect) createVersionTableSQL() string { - return `CREATE TABLE goose_db_version ( + return fmt.Sprintf(`CREATE TABLE %s ( id integer NOT NULL identity(1, 1), version_id bigint NOT NULL, is_applied boolean NOT NULL, tstamp timestamp NULL default sysdate, PRIMARY KEY(id) - );` + );`, TableName()) } func (rs RedshiftDialect) insertVersionSQL() string { - return "INSERT INTO goose_db_version (version_id, is_applied) VALUES ($1, $2);" + return fmt.Sprintf("INSERT INTO %s (version_id, is_applied) VALUES ($1, $2);", TableName()) } func (rs RedshiftDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { - rows, err := db.Query("SELECT version_id, is_applied from goose_db_version ORDER BY id DESC") + rows, err := db.Query(fmt.Sprintf("SELECT version_id, is_applied from %s ORDER BY id DESC", TableName())) if err != nil { return nil, err } @@ -167,24 +167,24 @@ func (rs RedshiftDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { type TiDBDialect struct{} func (m TiDBDialect) createVersionTableSQL() string { - return `CREATE TABLE goose_db_version ( + return fmt.Sprintf(`CREATE TABLE %s ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE, version_id bigint NOT NULL, is_applied boolean NOT NULL, tstamp timestamp NULL default now(), PRIMARY KEY(id) - );` + );`, TableName()) } func (m TiDBDialect) insertVersionSQL() string { - return "INSERT INTO goose_db_version (version_id, is_applied) VALUES (?, ?);" + return fmt.Sprintf("INSERT INTO %s (version_id, is_applied) VALUES (?, ?);", TableName()) } func (m TiDBDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { - rows, err := db.Query("SELECT version_id, is_applied from goose_db_version ORDER BY id DESC") + rows, err := db.Query(fmt.Sprintf("SELECT version_id, is_applied from %s ORDER BY id DESC", TableName())) if err != nil { return nil, err } return rows, err -} \ No newline at end of file +} diff --git a/migrate.go b/migrate.go index cb6be7c55..330c31331 100644 --- a/migrate.go +++ b/migrate.go @@ -241,7 +241,7 @@ func EnsureDBVersion(db *sql.DB) (int64, error) { return 0, ErrNoNextVersion } -// Create the goose_db_version table +// Create the db version table // and insert the initial 0 value into it func createVersionTable(db *sql.DB) error { txn, err := db.Begin() diff --git a/status.go b/status.go index 3cc5ed6b4..cf441258c 100644 --- a/status.go +++ b/status.go @@ -31,7 +31,7 @@ func Status(db *sql.DB, dir string) error { func printMigrationStatus(db *sql.DB, version int64, script string) { var row MigrationRecord - q := fmt.Sprintf("SELECT tstamp, is_applied FROM goose_db_version WHERE version_id=%d ORDER BY tstamp DESC LIMIT 1", version) + q := fmt.Sprintf("SELECT tstamp, is_applied FROM %s WHERE version_id=%d ORDER BY tstamp DESC LIMIT 1", TableName(), version) e := db.QueryRow(q).Scan(&row.TStamp, &row.IsApplied) if e != nil && e != sql.ErrNoRows { diff --git a/version.go b/version.go index 640a4a075..800540031 100644 --- a/version.go +++ b/version.go @@ -14,3 +14,15 @@ func Version(db *sql.DB, dir string) error { log.Printf("goose: version %v\n", current) return nil } + +var tableName = "goose_db_version" + +// TableName returns goose db version table name +func TableName() string { + return tableName +} + +// SetTableName set goose db version table name +func SetTableName(n string) { + tableName = n +}