Support non-PostgreSQL databases
This commit is contained in:
parent
de9e7d9a41
commit
f39c1c640a
3 changed files with 86 additions and 16 deletions
38
migrate.go
38
migrate.go
|
@ -11,7 +11,7 @@ import (
|
|||
// Options contains all settings
|
||||
type Options struct {
|
||||
TableName string // Name used for version info table; defaults to DefaultTableName if not set
|
||||
Schema string // Schema used for version info table; For PostgreSQL, ignored if not set
|
||||
Schema string // Schema used for version info table; In PostgreSQL the current schema is changed to the one specified here
|
||||
AssetPrefix string
|
||||
}
|
||||
|
||||
|
@ -29,23 +29,13 @@ type AssetFunc func(string) ([]byte, error)
|
|||
// Migrate executes migrations to get to the desired version
|
||||
// Downgrading is not supported as it could result in data loss
|
||||
func Migrate(db *sql.DB, version int, o Options, asset AssetFunc) error {
|
||||
dbType := getDbType(db)
|
||||
if o.TableName == `` {
|
||||
o.TableName = DefaultTableName
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
searchPath := `public`
|
||||
_ = db.QueryRow(`SHOW search_path`).Scan(&searchPath)
|
||||
|
||||
if o.Schema != `` {
|
||||
_, _ = db.Exec(`CREATE SCHEMA IF NOT EXISTS ` + o.Schema)
|
||||
|
||||
_, err = db.Exec(`SET search_path TO ` + o.Schema)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
|
@ -53,12 +43,28 @@ func Migrate(db *sql.DB, version int, o Options, asset AssetFunc) error {
|
|||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
_, err = tx.Exec(`CREATE TABLE IF NOT EXISTS ` + o.TableName + ` (Version integer NOT NULL PRIMARY KEY)`)
|
||||
if o.Schema != `` {
|
||||
err = createSchemaIfNotExists(tx, o.Schema)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if dbType == `pq` {
|
||||
_ = tx.QueryRow(`SHOW search_path`).Scan(&searchPath)
|
||||
|
||||
_, err = tx.Exec(`SET search_path TO ` + o.Schema)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
versionTable, err := createTableIfNotExists(tx, o.Schema, o.TableName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
row := tx.QueryRow(`SELECT Version FROM ` + o.TableName + ` ORDER BY Version DESC`)
|
||||
row := tx.QueryRow(`SELECT version FROM ` + versionTable + ` ORDER BY Version DESC`)
|
||||
|
||||
var v int
|
||||
err = row.Scan(&v)
|
||||
|
@ -80,13 +86,13 @@ func Migrate(db *sql.DB, version int, o Options, asset AssetFunc) error {
|
|||
return errorf(err)
|
||||
}
|
||||
|
||||
_, err = tx.Exec(`INSERT INTO ` + o.TableName + ` VALUES (` + strconv.Itoa(i) + `)`)
|
||||
_, err = tx.Exec(`INSERT INTO ` + versionTable + ` (version) VALUES (` + strconv.Itoa(i) + `)`)
|
||||
if err != nil {
|
||||
return errorf(err)
|
||||
}
|
||||
}
|
||||
|
||||
if o.Schema != `` {
|
||||
if dbType == `pq` && o.Schema != `` {
|
||||
_, err = tx.Exec(`SET search_path TO ` + searchPath)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue