Support non-PostgreSQL databases
This commit is contained in:
parent
de9e7d9a41
commit
f39c1c640a
3 changed files with 86 additions and 16 deletions
35
query.go
Normal file
35
query.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package migrate
|
||||
|
||||
import "database/sql"
|
||||
|
||||
func createSchemaIfNotExists(tx *sql.Tx, schema string) error {
|
||||
row := tx.QueryRow(`SELECT 1 FROM information_schema.schemata WHERE schema_name = '` + schema + `'`)
|
||||
err := row.Scan(new(int))
|
||||
if err == sql.ErrNoRows {
|
||||
_, err = tx.Exec(`CREATE SCHEMA ` + schema)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func createTableIfNotExists(tx *sql.Tx, schema, table string) (string, error) {
|
||||
versionTable := table
|
||||
var schemaCond string
|
||||
|
||||
if schema != `` {
|
||||
versionTable = schema + `.` + table
|
||||
schemaCond = ` AND table_schema = '` + schema + `'`
|
||||
}
|
||||
|
||||
row := tx.QueryRow(`SELECT 1 FROM information_schema.tables WHERE table_name = '` + table + `'` + schemaCond)
|
||||
err := row.Scan(new(int))
|
||||
if err == sql.ErrNoRows {
|
||||
_, err = tx.Exec(`CREATE TABLE ` + versionTable + ` (version integer NOT NULL PRIMARY KEY)`)
|
||||
if err != nil {
|
||||
return ``, err
|
||||
}
|
||||
}
|
||||
return versionTable, err
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue