db: add schema table, initial schema-upgrade system
This commit is contained in:
parent
993bd6c4f3
commit
91887bcee5
75
DB.go
75
DB.go
@ -3,6 +3,7 @@ package yatwiki
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -33,40 +34,66 @@ func NewWikiDB(dbFilePath string, compressionLevel int) (*WikiDB, error) {
|
|||||||
return &wdb, nil
|
return &wdb, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *WikiDB) multiTx(stmts ...string) (err error) {
|
||||||
|
tx, err := this.db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, stmt := range stmts {
|
||||||
|
_, err := tx.Exec(stmt)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
||||||
|
|
||||||
func (this *WikiDB) assertSchema() error {
|
func (this *WikiDB) assertSchema() error {
|
||||||
_, err := this.db.Exec(`
|
_, err := this.db.Exec(`
|
||||||
CREATE TABLE IF NOT EXISTS articles (
|
CREATE TABLE IF NOT EXISTS schema (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY
|
||||||
article INTEGER,
|
|
||||||
modified INTEGER,
|
|
||||||
body BLOB,
|
|
||||||
author TEXT
|
|
||||||
);`)
|
);`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = this.db.Exec(`
|
// Look up current value from schema table
|
||||||
CREATE TABLE IF NOT EXISTS titles (
|
schemaLookup := this.db.QueryRow(`SELECT MAX(id) mid FROM schema`)
|
||||||
id INTEGER PRIMARY KEY,
|
currentSchema := int64(0)
|
||||||
title TEXT
|
if err = schemaLookup.Scan(¤tSchema); err == nil {
|
||||||
);`)
|
// That's fine
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = this.db.Exec(`
|
log.Printf("Found DB version %d\n", currentSchema)
|
||||||
CREATE INDEX IF NOT EXISTS articles_modified_index ON articles (modified)
|
|
||||||
`)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = this.db.Exec(`
|
// Schema 0 ==> Schema 1
|
||||||
CREATE INDEX IF NOT EXISTS articles_title_index ON articles (article)
|
|
||||||
`)
|
if currentSchema == 0 {
|
||||||
if err != nil {
|
log.Println("Upgrading to DB version 1")
|
||||||
return err
|
|
||||||
|
err := this.multiTx(
|
||||||
|
`CREATE TABLE IF NOT EXISTS articles (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
article INTEGER,
|
||||||
|
modified INTEGER,
|
||||||
|
body BLOB,
|
||||||
|
author TEXT
|
||||||
|
);`,
|
||||||
|
`CREATE TABLE IF NOT EXISTS titles (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
title TEXT
|
||||||
|
);`,
|
||||||
|
`CREATE INDEX IF NOT EXISTS articles_modified_index ON articles (modified)`,
|
||||||
|
`CREATE INDEX IF NOT EXISTS articles_title_index ON articles (article)`,
|
||||||
|
`INSERT INTO schema (id) VALUES (1);`,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
currentSchema = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user