Skip to content

Commit

Permalink
Merge pull request pressly#104 from chapsuk/table_name
Browse files Browse the repository at this point in the history
Added ability to change db version table name
  • Loading branch information
VojtechVitek authored May 3, 2018
2 parents f0b1c6f + 3676559 commit 95600eb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
44 changes: 22 additions & 22 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
}
2 changes: 1 addition & 1 deletion migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 95600eb

Please sign in to comment.