Added ability to change db version table name

pull/104/head
Maxim Krasilnikov 2018-04-29 14:11:02 +03:00
parent b0454a40a9
commit 0639337e0b
4 changed files with 36 additions and 24 deletions

View File

@ -8,7 +8,7 @@ import (
// SQLDialect abstracts the details of specific SQL dialects // SQLDialect abstracts the details of specific SQL dialects
// for goose's few SQL specific statements // for goose's few SQL specific statements
type SQLDialect interface { 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 insertVersionSQL() string // sql string to insert the initial version table row
dbVersionQuery(db *sql.DB) (*sql.Rows, error) dbVersionQuery(db *sql.DB) (*sql.Rows, error)
} }
@ -48,21 +48,21 @@ func SetDialect(d string) error {
type PostgresDialect struct{} type PostgresDialect struct{}
func (pg PostgresDialect) createVersionTableSQL() string { func (pg PostgresDialect) createVersionTableSQL() string {
return `CREATE TABLE goose_db_version ( return fmt.Sprintf(`CREATE TABLE %s (
id serial NOT NULL, id serial NOT NULL,
version_id bigint NOT NULL, version_id bigint NOT NULL,
is_applied boolean NOT NULL, is_applied boolean NOT NULL,
tstamp timestamp NULL default now(), tstamp timestamp NULL default now(),
PRIMARY KEY(id) PRIMARY KEY(id)
);` );`, GetDBVersionTableName())
} }
func (pg PostgresDialect) insertVersionSQL() string { 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);", GetDBVersionTableName())
} }
func (pg PostgresDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { 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", GetDBVersionTableName()))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -78,21 +78,21 @@ func (pg PostgresDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) {
type MySQLDialect struct{} type MySQLDialect struct{}
func (m MySQLDialect) createVersionTableSQL() string { func (m MySQLDialect) createVersionTableSQL() string {
return `CREATE TABLE goose_db_version ( return fmt.Sprintf(`CREATE TABLE %s (
id serial NOT NULL, id serial NOT NULL,
version_id bigint NOT NULL, version_id bigint NOT NULL,
is_applied boolean NOT NULL, is_applied boolean NOT NULL,
tstamp timestamp NULL default now(), tstamp timestamp NULL default now(),
PRIMARY KEY(id) PRIMARY KEY(id)
);` );`, GetDBVersionTableName())
} }
func (m MySQLDialect) insertVersionSQL() string { 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 (?, ?);", GetDBVersionTableName())
} }
func (m MySQLDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { 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", GetDBVersionTableName()))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -108,20 +108,20 @@ func (m MySQLDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) {
type Sqlite3Dialect struct{} type Sqlite3Dialect struct{}
func (m Sqlite3Dialect) createVersionTableSQL() string { func (m Sqlite3Dialect) createVersionTableSQL() string {
return `CREATE TABLE goose_db_version ( return fmt.Sprintf(`CREATE TABLE %s (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
version_id INTEGER NOT NULL, version_id INTEGER NOT NULL,
is_applied INTEGER NOT NULL, is_applied INTEGER NOT NULL,
tstamp TIMESTAMP DEFAULT (datetime('now')) tstamp TIMESTAMP DEFAULT (datetime('now'))
);` );`, GetDBVersionTableName())
} }
func (m Sqlite3Dialect) insertVersionSQL() string { 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 (?, ?);", GetDBVersionTableName())
} }
func (m Sqlite3Dialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { 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", GetDBVersionTableName()))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -137,21 +137,21 @@ func (m Sqlite3Dialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) {
type RedshiftDialect struct{} type RedshiftDialect struct{}
func (rs RedshiftDialect) createVersionTableSQL() string { func (rs RedshiftDialect) createVersionTableSQL() string {
return `CREATE TABLE goose_db_version ( return fmt.Sprintf(`CREATE TABLE %s (
id integer NOT NULL identity(1, 1), id integer NOT NULL identity(1, 1),
version_id bigint NOT NULL, version_id bigint NOT NULL,
is_applied boolean NOT NULL, is_applied boolean NOT NULL,
tstamp timestamp NULL default sysdate, tstamp timestamp NULL default sysdate,
PRIMARY KEY(id) PRIMARY KEY(id)
);` );`, GetDBVersionTableName())
} }
func (rs RedshiftDialect) insertVersionSQL() string { 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);", GetDBVersionTableName())
} }
func (rs RedshiftDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { 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", GetDBVersionTableName()))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -167,24 +167,24 @@ func (rs RedshiftDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) {
type TiDBDialect struct{} type TiDBDialect struct{}
func (m TiDBDialect) createVersionTableSQL() string { 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, id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
version_id bigint NOT NULL, version_id bigint NOT NULL,
is_applied boolean NOT NULL, is_applied boolean NOT NULL,
tstamp timestamp NULL default now(), tstamp timestamp NULL default now(),
PRIMARY KEY(id) PRIMARY KEY(id)
);` );`, GetDBVersionTableName())
} }
func (m TiDBDialect) insertVersionSQL() string { 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 (?, ?);", GetDBVersionTableName())
} }
func (m TiDBDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) { 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", GetDBVersionTableName()))
if err != nil { if err != nil {
return nil, err return nil, err
} }
return rows, err return rows, err
} }

View File

@ -242,7 +242,7 @@ func EnsureDBVersion(db *sql.DB) (int64, error) {
return 0, ErrNoNextVersion return 0, ErrNoNextVersion
} }
// Create the goose_db_version table // Create the db version table
// and insert the initial 0 value into it // and insert the initial 0 value into it
func createVersionTable(db *sql.DB) error { func createVersionTable(db *sql.DB) error {
txn, err := db.Begin() txn, err := db.Begin()

View File

@ -32,7 +32,7 @@ func Status(db *sql.DB, dir string) error {
func printMigrationStatus(db *sql.DB, version int64, script string) { func printMigrationStatus(db *sql.DB, version int64, script string) {
var row MigrationRecord 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", GetDBVersionTableName(), version)
e := db.QueryRow(q).Scan(&row.TStamp, &row.IsApplied) e := db.QueryRow(q).Scan(&row.TStamp, &row.IsApplied)
if e != nil && e != sql.ErrNoRows { if e != nil && e != sql.ErrNoRows {

View File

@ -15,3 +15,15 @@ func Version(db *sql.DB, dir string) error {
log.Printf("goose: version %v\n", current) log.Printf("goose: version %v\n", current)
return nil return nil
} }
var dbVersionTableName = "goose_db_version"
// GetDBVersionTableName returns goose db version table name
func GetDBVersionTableName() string {
return dbVersionTableName
}
// SetDBVersionTableName set goose db version table name
func SetDBVersionTableName(n string) {
dbVersionTableName = n
}