Removed schema database, storing metadata in each database instead
This commit is contained in:
parent
f80de360ba
commit
a38bd4304f
45
schema.go
45
schema.go
@ -9,7 +9,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Upgrader struct {
|
type Upgrader struct {
|
||||||
schemaDb Database
|
|
||||||
databases map[string]Database
|
databases map[string]Database
|
||||||
logCallback func(string, string)
|
logCallback func(string, string)
|
||||||
sqlCallback func(string, int) ([]byte, bool)
|
sqlCallback func(string, int) ([]byte, bool)
|
||||||
@ -23,49 +22,7 @@ type Database struct {
|
|||||||
Password string
|
Password string
|
||||||
|
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
schemaDb *Database
|
upgrader *Upgrader
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
func dbUpdate() (err error) {// {{{
|
|
||||||
var rows *sqlx.Rows
|
|
||||||
var schemaStr string
|
|
||||||
var schema int
|
|
||||||
rows, err = db.Queryx(`SELECT value FROM _internal.db WHERE "key"='schema'`)
|
|
||||||
if err != nil { return }
|
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
if !rows.Next() {
|
|
||||||
return errors.New("Table _interval.db missing schema row")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = rows.Scan(&schemaStr); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run updates
|
|
||||||
schema, err = strconv.Atoi(schemaStr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for i := (schema+1); i <= DB_SCHEMA; i++ {
|
|
||||||
log.Printf("\x1b[32mNotes\x1b[0m Upgrading SQL schema to revision %d...", i)
|
|
||||||
sql, _ := embedded.ReadFile(
|
|
||||||
fmt.Sprintf("sql/%04d.sql", i),
|
|
||||||
)
|
|
||||||
_, err = db.Exec(string(sql))
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_, err = db.Exec(`UPDATE _internal.db SET "value"=$1 WHERE "key"='schema'`, i)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Printf("\x1b[32mNotes\x1b[0m OK: %d", i)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}// }}}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// vim: foldmethod=marker
|
// vim: foldmethod=marker
|
||||||
|
66
upgrader.go
66
upgrader.go
@ -12,14 +12,6 @@ func defaultCallback(topic, msg string) {// {{{
|
|||||||
func NewUpgrader(host string, port int, dbName, user, pass string) (upgrader Upgrader, err error) {// {{{
|
func NewUpgrader(host string, port int, dbName, user, pass string) (upgrader Upgrader, err error) {// {{{
|
||||||
upgrader.logCallback = defaultCallback
|
upgrader.logCallback = defaultCallback
|
||||||
upgrader.databases = map[string]Database{}
|
upgrader.databases = map[string]Database{}
|
||||||
upgrader.schemaDb, err = newDatabase(
|
|
||||||
host,
|
|
||||||
port,
|
|
||||||
dbName,
|
|
||||||
user,
|
|
||||||
pass,
|
|
||||||
)
|
|
||||||
err = upgrader.verifySchemaTable()
|
|
||||||
return
|
return
|
||||||
}// }}}
|
}// }}}
|
||||||
|
|
||||||
@ -29,9 +21,10 @@ func (upgrader *Upgrader) SetLogCallback(callback func(string, string)) {// {{{
|
|||||||
func (upgrader *Upgrader) SetSqlCallback(callback func(string, int) ([]byte, bool)) {// {{{
|
func (upgrader *Upgrader) SetSqlCallback(callback func(string, int) ([]byte, bool)) {// {{{
|
||||||
upgrader.sqlCallback = callback
|
upgrader.sqlCallback = callback
|
||||||
}// }}}
|
}// }}}
|
||||||
func (upgrader Upgrader) verifySchemaTable() (err error) {// {{{
|
|
||||||
|
func (dbase Database) verifySchemaTable() (err error) {// {{{
|
||||||
var rows *sql.Rows
|
var rows *sql.Rows
|
||||||
if rows, err = upgrader.schemaDb.db.Query(
|
if rows, err = dbase.db.Query(
|
||||||
`SELECT EXISTS (
|
`SELECT EXISTS (
|
||||||
SELECT FROM pg_catalog.pg_class c
|
SELECT FROM pg_catalog.pg_class c
|
||||||
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
||||||
@ -49,35 +42,33 @@ func (upgrader Upgrader) verifySchemaTable() (err error) {// {{{
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
upgrader.logCallback("create", "_db.schema")
|
dbase.upgrader.logCallback("create", fmt.Sprintf("%s, _db.schema", dbase.DbName))
|
||||||
upgrader.schemaDb.db.Exec(`CREATE SCHEMA "_db"`)
|
dbase.db.Exec(`CREATE SCHEMA "_db"`)
|
||||||
|
|
||||||
if _, err = upgrader.schemaDb.db.Exec(`
|
if _, err = dbase.db.Exec(`
|
||||||
CREATE TABLE "_db"."schema" (
|
CREATE TABLE "_db"."schema" (
|
||||||
database varchar NOT NULL,
|
|
||||||
version int4 NOT NULL,
|
version int4 NOT NULL,
|
||||||
updated timestamp NOT NULL DEFAULT NOW(),
|
updated timestamp NOT NULL DEFAULT NOW(),
|
||||||
|
|
||||||
CONSTRAINT schema_pk PRIMARY KEY (database)
|
CONSTRAINT schema_pk PRIMARY KEY (version)
|
||||||
);
|
)`,
|
||||||
`,
|
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}// }}}
|
}// }}}
|
||||||
func (upgrader Upgrader) verifySchemaEntry(dbase Database) (err error) {// {{{
|
func (dbase Database) verifySchemaEntry() (err error) {// {{{
|
||||||
var rows *sql.Rows
|
var rows *sql.Rows
|
||||||
rows, err = upgrader.schemaDb.db.Query(`SELECT version FROM _db.schema WHERE database=$1`, dbase.DbName)
|
rows, err = dbase.db.Query(`SELECT version FROM _db.schema LIMIT 1`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
if !rows.Next() {
|
if !rows.Next() {
|
||||||
upgrader.logCallback("initiate version", dbase.DbName)
|
dbase.upgrader.logCallback("initiate version", dbase.DbName)
|
||||||
_, err = upgrader.schemaDb.db.Exec(`INSERT INTO _db.schema(database, version) VALUES($1, 0)`, dbase.DbName)
|
_, err = dbase.db.Exec(`INSERT INTO _db.schema(version) VALUES(0)`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -85,11 +76,10 @@ func (upgrader Upgrader) verifySchemaEntry(dbase Database) (err error) {// {{{
|
|||||||
|
|
||||||
return
|
return
|
||||||
}// }}}
|
}// }}}
|
||||||
func (upgrader Upgrader) version(dbName string) (version int, err error) {// {{{
|
func (dbase Database) version() (version int, err error) {// {{{
|
||||||
var rows *sql.Rows
|
var rows *sql.Rows
|
||||||
rows, err = upgrader.schemaDb.db.Query(
|
rows, err = dbase.db.Query(
|
||||||
`SELECT version FROM _db.schema WHERE database=$1`,
|
`SELECT version FROM _db.schema ORDER BY version DESC LIMIT 1`,
|
||||||
dbName,
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -99,7 +89,7 @@ func (upgrader Upgrader) version(dbName string) (version int, err error) {// {{{
|
|||||||
if rows.Next() {
|
if rows.Next() {
|
||||||
err = rows.Scan(&version)
|
err = rows.Scan(&version)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf(`Database "%s" is missing an entry in _db.schema`, dbName)
|
err = fmt.Errorf(`Database "%s" is missing an entry in _db.schema`, dbase.DbName)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}// }}}
|
}// }}}
|
||||||
@ -109,17 +99,22 @@ func (upgrader Upgrader) AddDatabase(host string, port int, dbName, user, pass s
|
|||||||
if db, err = newDatabase(host, port, dbName, user, pass); err != nil {
|
if db, err = newDatabase(host, port, dbName, user, pass); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
db.upgrader = &upgrader
|
||||||
|
|
||||||
upgrader.databases[dbName] = db
|
upgrader.databases[dbName] = db
|
||||||
|
|
||||||
err = upgrader.verifySchemaEntry(db)
|
if err = db.verifySchemaTable(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.verifySchemaEntry()
|
||||||
return
|
return
|
||||||
}// }}}
|
}// }}}
|
||||||
func (upgrader Upgrader) Run() (err error) {// {{{
|
func (upgrader Upgrader) Run() (err error) {// {{{
|
||||||
var version int
|
var version int
|
||||||
|
|
||||||
for dbName, db := range upgrader.databases {
|
for dbName, dbase := range upgrader.databases {
|
||||||
version, err = upgrader.version(dbName)
|
version, err = dbase.version()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -133,16 +128,13 @@ func (upgrader Upgrader) Run() (err error) {// {{{
|
|||||||
}
|
}
|
||||||
|
|
||||||
upgrader.logCallback("exec", fmt.Sprintf("%s: %d", dbName, version))
|
upgrader.logCallback("exec", fmt.Sprintf("%s: %d", dbName, version))
|
||||||
if _, err = db.db.Exec(string(sql)); err != nil {
|
if _, err = dbase.db.Exec(string(sql)); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = upgrader.schemaDb.db.Exec(`
|
_, err = dbase.db.Exec(`
|
||||||
UPDATE _db.schema
|
INSERT INTO _db.schema(version)
|
||||||
SET
|
VALUES($1)
|
||||||
version=$1,
|
`, version)
|
||||||
updated=NOW()
|
|
||||||
WHERE database=$2
|
|
||||||
`, version, dbName)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user