Fix issues when missing CREATE SCHEMA permissions

This commit is contained in:
Nise Void 2017-04-19 19:16:05 +02:00
parent 65c3956b60
commit 0ebacc4842

View File

@ -36,33 +36,29 @@ func Migrate(db *sql.DB, version int, o Options, asset AssetFunc) error {
o.TableName = DefaultTableName o.TableName = DefaultTableName
} }
var err error
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() tx, err := db.Begin()
if err != nil { if err != nil {
return err return err
} }
defer tx.Rollback() defer tx.Rollback()
table := o.TableName _, err = tx.Exec(`CREATE TABLE IF NOT EXISTS ` + o.TableName + ` (Version integer NOT NULL PRIMARY KEY)`)
if o.Schema != `` {
table = o.Schema + `.` + table
_, err = tx.Exec(`CREATE SCHEMA IF NOT EXISTS ` + o.Schema)
if err != nil { if err != nil {
return err return err
} }
_, err = tx.Exec(`SET search_path TO ` + o.Schema + `,public`) row := tx.QueryRow(`SELECT Version FROM ` + o.TableName + ` ORDER BY Version DESC`)
if err != nil {
return err
}
}
_, err = tx.Exec(`CREATE TABLE IF NOT EXISTS ` + table + ` (Version integer NOT NULL PRIMARY KEY)`)
if err != nil {
return err
}
row := tx.QueryRow(`SELECT Version FROM ` + table + ` ORDER BY Version DESC`)
var v int var v int
err = row.Scan(&v) err = row.Scan(&v)
@ -85,12 +81,17 @@ func Migrate(db *sql.DB, version int, o Options, asset AssetFunc) error {
return err return err
} }
_, err = tx.Exec(`INSERT INTO ` + table + ` VALUES (` + strconv.Itoa(i) + `)`) _, err = tx.Exec(`INSERT INTO ` + o.TableName + ` VALUES (` + strconv.Itoa(i) + `)`)
if err != nil { if err != nil {
return err return err
} }
} }
_, err = tx.Exec(`SET search_path TO ` + o.Schema + `,public`)
if err != nil {
return err
}
err = tx.Commit() err = tx.Commit()
if err != nil { if err != nil {
return err return err