Dynamic SQL schema
This commit is contained in:
parent
6cb4833dbd
commit
251186389d
43
db.go
43
db.go
@ -8,7 +8,9 @@ import (
|
||||
// Standard
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@ -17,7 +19,7 @@ var (
|
||||
db *sqlx.DB
|
||||
)
|
||||
|
||||
func dbInit() (err error) {// {{{
|
||||
func dbInit() (err error) { // {{{
|
||||
dbConn = fmt.Sprintf(
|
||||
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
|
||||
config.Database.Host,
|
||||
@ -43,8 +45,8 @@ func dbInit() (err error) {// {{{
|
||||
|
||||
err = dbUpdate()
|
||||
return
|
||||
}// }}}
|
||||
func dbVerifyInternals() (err error) {// {{{
|
||||
} // }}}
|
||||
func dbVerifyInternals() (err error) { // {{{
|
||||
var rows *sqlx.Rows
|
||||
if rows, err = db.Queryx(
|
||||
`SELECT EXISTS (
|
||||
@ -82,17 +84,19 @@ func dbVerifyInternals() (err error) {// {{{
|
||||
}
|
||||
}
|
||||
return
|
||||
}// }}}
|
||||
func dbUpdate() (err error) {// {{{
|
||||
} // }}}
|
||||
func dbUpdate() (err error) { // {{{
|
||||
/* Current schema revision is read from database.
|
||||
* Used to iterate through the embedded SQL updates
|
||||
* up to the DB_SCHEMA version currently compiled
|
||||
* up to the db schema version currently compiled
|
||||
* program is made for. */
|
||||
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 }
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
if !rows.Next() {
|
||||
@ -108,7 +112,8 @@ func dbUpdate() (err error) {// {{{
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i := (schema+1); i <= DB_SCHEMA; i++ {
|
||||
sqlSchemaVersion := sqlSchema()
|
||||
for i := (schema + 1); i <= sqlSchemaVersion; i++ {
|
||||
log.Printf("\x1b[32mNotes\x1b[0m Upgrading SQL schema to revision %d...", i)
|
||||
sql, _ := embedded.ReadFile(
|
||||
fmt.Sprintf("sql/%04d.sql", i),
|
||||
@ -125,6 +130,26 @@ func dbUpdate() (err error) {// {{{
|
||||
}
|
||||
|
||||
return
|
||||
}// }}}
|
||||
} // }}}
|
||||
func sqlSchema() (max int) { // {{{
|
||||
var num int
|
||||
|
||||
files, _ := fs.ReadDir(embedded, "sql")
|
||||
sqlFilename := regexp.MustCompile(`^([0-9]+)\.sql$`)
|
||||
|
||||
for _, file := range files {
|
||||
fname := sqlFilename.FindStringSubmatch(file.Name())
|
||||
if len(fname) != 2 {
|
||||
continue
|
||||
}
|
||||
num, _ = strconv.Atoi(fname[1])
|
||||
}
|
||||
|
||||
if num > max {
|
||||
max = num
|
||||
}
|
||||
|
||||
return
|
||||
} // }}}
|
||||
|
||||
// vim: foldmethod=marker
|
||||
|
6
main.go
6
main.go
@ -17,11 +17,9 @@ import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const LISTEN_HOST = "0.0.0.0"
|
||||
const DB_SCHEMA = 13
|
||||
|
||||
var (
|
||||
flagPort int
|
||||
@ -48,10 +46,6 @@ func init() { // {{{
|
||||
flag.BoolVar(&flagCreateUser, "createuser", false, "Create a user and exit")
|
||||
flag.StringVar(&flagConfig, "config", configFilename, "Filename of configuration file")
|
||||
flag.Parse()
|
||||
|
||||
if false {
|
||||
time.Sleep(time.Second * 1)
|
||||
}
|
||||
} // }}}
|
||||
func main() { // {{{
|
||||
var err error
|
||||
|
Loading…
Reference in New Issue
Block a user