A golang library for updating your database schema
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
933 B

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
}