From a38bd4304f5082ddf6fa9e695545ab8b4c2c57a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Tue, 1 Aug 2023 19:50:27 +0200 Subject: [PATCH] Removed schema database, storing metadata in each database instead --- schema.go | 45 +----------------------------------- upgrader.go | 66 +++++++++++++++++++++++------------------------------ 2 files changed, 30 insertions(+), 81 deletions(-) diff --git a/schema.go b/schema.go index 56050c2..836a836 100644 --- a/schema.go +++ b/schema.go @@ -9,7 +9,6 @@ import ( ) type Upgrader struct { - schemaDb Database databases map[string]Database logCallback func(string, string) sqlCallback func(string, int) ([]byte, bool) @@ -23,49 +22,7 @@ type Database struct { Password string 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 diff --git a/upgrader.go b/upgrader.go index 27e4f4f..13e7c38 100644 --- a/upgrader.go +++ b/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) {// {{{ upgrader.logCallback = defaultCallback upgrader.databases = map[string]Database{} - upgrader.schemaDb, err = newDatabase( - host, - port, - dbName, - user, - pass, - ) - err = upgrader.verifySchemaTable() return }// }}} @@ -29,9 +21,10 @@ func (upgrader *Upgrader) SetLogCallback(callback func(string, string)) {// {{{ func (upgrader *Upgrader) SetSqlCallback(callback func(string, int) ([]byte, bool)) {// {{{ upgrader.sqlCallback = callback }// }}} -func (upgrader Upgrader) verifySchemaTable() (err error) {// {{{ + +func (dbase Database) verifySchemaTable() (err error) {// {{{ var rows *sql.Rows - if rows, err = upgrader.schemaDb.db.Query( + if rows, err = dbase.db.Query( `SELECT EXISTS ( SELECT FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace @@ -49,35 +42,33 @@ func (upgrader Upgrader) verifySchemaTable() (err error) {// {{{ } if !exists { - upgrader.logCallback("create", "_db.schema") - upgrader.schemaDb.db.Exec(`CREATE SCHEMA "_db"`) + dbase.upgrader.logCallback("create", fmt.Sprintf("%s, _db.schema", dbase.DbName)) + dbase.db.Exec(`CREATE SCHEMA "_db"`) - if _, err = upgrader.schemaDb.db.Exec(` + if _, err = dbase.db.Exec(` CREATE TABLE "_db"."schema" ( - database varchar NOT NULL, version int4 NOT NULL, updated timestamp NOT NULL DEFAULT NOW(), - CONSTRAINT schema_pk PRIMARY KEY (database) - ); - `, + CONSTRAINT schema_pk PRIMARY KEY (version) + )`, ); err != nil { return } } return }// }}} -func (upgrader Upgrader) verifySchemaEntry(dbase Database) (err error) {// {{{ +func (dbase Database) verifySchemaEntry() (err error) {// {{{ 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 { return } defer rows.Close() if !rows.Next() { - upgrader.logCallback("initiate version", dbase.DbName) - _, err = upgrader.schemaDb.db.Exec(`INSERT INTO _db.schema(database, version) VALUES($1, 0)`, dbase.DbName) + dbase.upgrader.logCallback("initiate version", dbase.DbName) + _, err = dbase.db.Exec(`INSERT INTO _db.schema(version) VALUES(0)`) if err != nil { return } @@ -85,11 +76,10 @@ func (upgrader Upgrader) verifySchemaEntry(dbase Database) (err error) {// {{{ return }// }}} -func (upgrader Upgrader) version(dbName string) (version int, err error) {// {{{ +func (dbase Database) version() (version int, err error) {// {{{ var rows *sql.Rows - rows, err = upgrader.schemaDb.db.Query( - `SELECT version FROM _db.schema WHERE database=$1`, - dbName, + rows, err = dbase.db.Query( + `SELECT version FROM _db.schema ORDER BY version DESC LIMIT 1`, ) if err != nil { return @@ -99,7 +89,7 @@ func (upgrader Upgrader) version(dbName string) (version int, err error) {// {{{ if rows.Next() { err = rows.Scan(&version) } 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 }// }}} @@ -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 { return } + db.upgrader = &upgrader upgrader.databases[dbName] = db - err = upgrader.verifySchemaEntry(db) + if err = db.verifySchemaTable(); err != nil { + return + } + + err = db.verifySchemaEntry() return }// }}} func (upgrader Upgrader) Run() (err error) {// {{{ var version int - for dbName, db := range upgrader.databases { - version, err = upgrader.version(dbName) + for dbName, dbase := range upgrader.databases { + version, err = dbase.version() if err != nil { return } @@ -133,16 +128,13 @@ func (upgrader Upgrader) Run() (err error) {// {{{ } 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 } - _, err = upgrader.schemaDb.db.Exec(` - UPDATE _db.schema - SET - version=$1, - updated=NOW() - WHERE database=$2 - `, version, dbName) + _, err = dbase.db.Exec(` + INSERT INTO _db.schema(version) + VALUES($1) + `, version) if err != nil { return }