Compare commits
No commits in common. "main" and "v1.2.0" have entirely different histories.
2 changed files with 18 additions and 25 deletions
|
@ -29,8 +29,7 @@ import (
|
|||
|
||||
// An upgrader verifies the schema for one or more databases and upgrades them if possible.
|
||||
type Upgrader struct {
|
||||
schema string
|
||||
databases map[string]Database
|
||||
databases map[string]Database
|
||||
logCallback func(string, string)
|
||||
sqlCallback func(string, int) ([]byte, bool)
|
||||
}
|
||||
|
@ -42,7 +41,7 @@ type Database struct {
|
|||
Username string
|
||||
Password string
|
||||
|
||||
db *sql.DB
|
||||
db *sql.DB
|
||||
upgrader *Upgrader
|
||||
}
|
||||
|
||||
|
|
38
upgrader.go
38
upgrader.go
|
@ -14,14 +14,7 @@ func defaultCallback(topic, msg string) {// {{{
|
|||
}// }}}
|
||||
|
||||
// NewUpgrader creates an upgrader with an empty list of databases.
|
||||
func NewUpgrader(schema ...string) (upgrader Upgrader) {// {{{
|
||||
// Using a variadic function for backward compatibility.
|
||||
if len(schema) > 0 {
|
||||
upgrader.schema = schema[0]
|
||||
} else {
|
||||
upgrader.schema = "_db"
|
||||
}
|
||||
|
||||
func NewUpgrader() (upgrader Upgrader) {// {{{
|
||||
upgrader.logCallback = defaultCallback
|
||||
upgrader.databases = map[string]Database{}
|
||||
return
|
||||
|
@ -43,13 +36,13 @@ func (upgrader *Upgrader) Version(dbName string) (version int, err error) {// {{
|
|||
return
|
||||
}
|
||||
|
||||
version, err = dbase.Version()
|
||||
version, err = dbase.version()
|
||||
return
|
||||
}// }}}
|
||||
|
||||
func (dbase Database) createSchemaTable() (err error) {// {{{
|
||||
dbase.upgrader.logCallback("create", fmt.Sprintf("%s, %s.schema", dbase.DbName, dbase.upgrader.schema))
|
||||
_, err = dbase.db.Exec(`CREATE SCHEMA "`+dbase.upgrader.schema+`"`)
|
||||
dbase.upgrader.logCallback("create", fmt.Sprintf("%s, _db.schema", dbase.DbName))
|
||||
_, err = dbase.db.Exec(`CREATE SCHEMA "_db"`)
|
||||
|
||||
// Error code 42P06 "duplicate_schema" is an OK error,
|
||||
// table can still be missing and created.
|
||||
|
@ -59,7 +52,7 @@ func (dbase Database) createSchemaTable() (err error) {// {{{
|
|||
}
|
||||
|
||||
_, err = dbase.db.Exec(`
|
||||
CREATE TABLE "`+dbase.upgrader.schema+`"."schema" (
|
||||
CREATE TABLE "_db"."schema" (
|
||||
version int4 NOT NULL,
|
||||
updated timestamp NOT NULL DEFAULT NOW(),
|
||||
|
||||
|
@ -69,7 +62,7 @@ func (dbase Database) createSchemaTable() (err error) {// {{{
|
|||
return
|
||||
}// }}}
|
||||
func (dbase Database) appendSchemaVersion(version int) (err error) {// {{{
|
||||
_, err = dbase.db.Exec(`INSERT INTO `+dbase.upgrader.schema+`.schema(version) VALUES($1)`, version)
|
||||
_, err = dbase.db.Exec(`INSERT INTO _db.schema(version) VALUES($1)`, version)
|
||||
return
|
||||
}// }}}
|
||||
|
||||
|
@ -79,7 +72,7 @@ func (dbase Database) verifySchemaTable() (err error) {// {{{
|
|||
`SELECT EXISTS (
|
||||
SELECT FROM pg_catalog.pg_class c
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
||||
WHERE n.nspname = '`+dbase.upgrader.schema+`'
|
||||
WHERE n.nspname = '_db'
|
||||
AND c.relname = 'schema'
|
||||
)`,
|
||||
); err != nil {
|
||||
|
@ -101,7 +94,7 @@ func (dbase Database) verifySchemaTable() (err error) {// {{{
|
|||
func (dbase Database) verifySchemaEntry() (err error) {// {{{
|
||||
var version int
|
||||
var row *sql.Row
|
||||
row = dbase.db.QueryRow(`SELECT version FROM `+dbase.upgrader.schema+`.schema LIMIT 1`)
|
||||
row = dbase.db.QueryRow(`SELECT version FROM _db.schema LIMIT 1`)
|
||||
|
||||
err = row.Scan(&version)
|
||||
if err == sql.ErrNoRows {
|
||||
|
@ -111,10 +104,10 @@ func (dbase Database) verifySchemaEntry() (err error) {// {{{
|
|||
|
||||
return
|
||||
}// }}}
|
||||
func (dbase Database) Version() (version int, err error) {// {{{
|
||||
func (dbase Database) version() (version int, err error) {// {{{
|
||||
var rows *sql.Rows
|
||||
rows, err = dbase.db.Query(
|
||||
`SELECT version FROM `+dbase.upgrader.schema+`.schema ORDER BY version DESC LIMIT 1`,
|
||||
`SELECT version FROM _db.schema ORDER BY version DESC LIMIT 1`,
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -124,13 +117,14 @@ func (dbase Database) Version() (version int, err error) {// {{{
|
|||
if rows.Next() {
|
||||
err = rows.Scan(&version)
|
||||
} else {
|
||||
err = fmt.Errorf(`Database "%s" is missing an entry in `+dbase.upgrader.schema+`.schema`, dbase.DbName)
|
||||
err = fmt.Errorf(`Database "%s" is missing an entry in _db.schema`, dbase.DbName)
|
||||
}
|
||||
return
|
||||
}// }}}
|
||||
|
||||
// AddDatabase sets a database up for the Run() function with verifying/creating the _db.schema table.
|
||||
func (upgrader Upgrader) AddDatabase(host string, port int, dbName, user, pass string) (db Database, err error) {// {{{
|
||||
func (upgrader Upgrader) AddDatabase(host string, port int, dbName, user, pass string) (err error) {// {{{
|
||||
var db Database
|
||||
if db, err = newDatabase(host, port, dbName, user, pass); err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -150,11 +144,11 @@ func (upgrader Upgrader) Run() (err error) {// {{{
|
|||
var version int
|
||||
|
||||
for dbName, dbase := range upgrader.databases {
|
||||
version, err = dbase.Version()
|
||||
version, err = dbase.version()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
upgrader.logCallback("version", fmt.Sprintf("%s.%s: %d", dbName, upgrader.schema, version))
|
||||
upgrader.logCallback("version", fmt.Sprintf("%s: %d", dbName, version))
|
||||
|
||||
for {
|
||||
version++
|
||||
|
@ -163,7 +157,7 @@ func (upgrader Upgrader) Run() (err error) {// {{{
|
|||
break
|
||||
}
|
||||
|
||||
upgrader.logCallback("exec", fmt.Sprintf("%s.%s: %d", dbName, upgrader.schema, version))
|
||||
upgrader.logCallback("exec", fmt.Sprintf("%s: %d", dbName, version))
|
||||
if _, err = dbase.db.Exec(string(sql)); err != nil {
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue