Support non-PostgreSQL databases

This commit is contained in:
Nise Void 2019-11-12 13:01:36 +01:00
parent de9e7d9a41
commit f39c1c640a
Signed by: NiseVoid
GPG key ID: FBA14AC83EA602F3
3 changed files with 86 additions and 16 deletions

35
query.go Normal file
View 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
}