Dynamic SQL schema
This commit is contained in:
parent
6cb4833dbd
commit
251186389d
31
db.go
31
db.go
@ -8,7 +8,9 @@ import (
|
|||||||
// Standard
|
// Standard
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -86,13 +88,15 @@ func dbVerifyInternals() (err error) {// {{{
|
|||||||
func dbUpdate() (err error) { // {{{
|
func dbUpdate() (err error) { // {{{
|
||||||
/* Current schema revision is read from database.
|
/* Current schema revision is read from database.
|
||||||
* Used to iterate through the embedded SQL updates
|
* 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. */
|
* program is made for. */
|
||||||
var rows *sqlx.Rows
|
var rows *sqlx.Rows
|
||||||
var schemaStr string
|
var schemaStr string
|
||||||
var schema int
|
var schema int
|
||||||
rows, err = db.Queryx(`SELECT value FROM _internal.db WHERE "key"='schema'`)
|
rows, err = db.Queryx(`SELECT value FROM _internal.db WHERE "key"='schema'`)
|
||||||
if err != nil { return }
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
if !rows.Next() {
|
if !rows.Next() {
|
||||||
@ -108,7 +112,8 @@ func dbUpdate() (err error) {// {{{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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)
|
log.Printf("\x1b[32mNotes\x1b[0m Upgrading SQL schema to revision %d...", i)
|
||||||
sql, _ := embedded.ReadFile(
|
sql, _ := embedded.ReadFile(
|
||||||
fmt.Sprintf("sql/%04d.sql", i),
|
fmt.Sprintf("sql/%04d.sql", i),
|
||||||
@ -126,5 +131,25 @@ func dbUpdate() (err error) {// {{{
|
|||||||
|
|
||||||
return
|
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
|
// vim: foldmethod=marker
|
||||||
|
6
main.go
6
main.go
@ -17,11 +17,9 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const LISTEN_HOST = "0.0.0.0"
|
const LISTEN_HOST = "0.0.0.0"
|
||||||
const DB_SCHEMA = 13
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
flagPort int
|
flagPort int
|
||||||
@ -48,10 +46,6 @@ func init() { // {{{
|
|||||||
flag.BoolVar(&flagCreateUser, "createuser", false, "Create a user and exit")
|
flag.BoolVar(&flagCreateUser, "createuser", false, "Create a user and exit")
|
||||||
flag.StringVar(&flagConfig, "config", configFilename, "Filename of configuration file")
|
flag.StringVar(&flagConfig, "config", configFilename, "Filename of configuration file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if false {
|
|
||||||
time.Sleep(time.Second * 1)
|
|
||||||
}
|
|
||||||
} // }}}
|
} // }}}
|
||||||
func main() { // {{{
|
func main() { // {{{
|
||||||
var err error
|
var err error
|
||||||
|
Loading…
Reference in New Issue
Block a user