Autocreate _internal.db

This commit is contained in:
Magnus Åhall 2023-06-18 22:25:34 +02:00
parent 85a2d0683e
commit 99a15aa567
4 changed files with 58 additions and 14 deletions

58
db.go
View File

@ -21,7 +21,7 @@ var (
embedded embed.FS
)
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,
@ -37,11 +37,57 @@ func dbInit() (err error) {
config.Database.Port,
)
db, err = sqlx.Connect("postgres", dbConn)
return
}
if db, err = sqlx.Connect("postgres", dbConn); err != nil {
return
}
func dbUpdate() (err error) {
if err = dbVerifyInternals(); err != nil {
return
}
err = dbUpdate()
return
}// }}}
func dbVerifyInternals() (err error) {// {{{
var rows *sqlx.Rows
if rows, err = db.Queryx(
`SELECT EXISTS (
SELECT FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = '_internal'
AND c.relname = 'db'
)`,
); err != nil {
return
}
defer rows.Close()
var exists bool
rows.Next()
if err = rows.Scan(&exists); err != nil {
return
}
if !exists {
log.Printf("\x1b[32mNotes\x1b[0m Creating _internal.db\n")
if _, err = db.Exec(`
CREATE SCHEMA "_internal";
CREATE TABLE "_internal".db (
"key" varchar NOT NULL,
value varchar NULL,
CONSTRAINT db_pk PRIMARY KEY (key)
);
INSERT INTO _internal.db("key", "value")
VALUES('schema', '0');
`,
); err != nil {
return
}
}
return
}// }}}
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
@ -82,6 +128,6 @@ func dbUpdate() (err error) {
}
return
}
}// }}}
// vim: foldmethod=marker

View File

@ -55,10 +55,6 @@ func main() {// {{{
fmt.Printf("%s\n", err)
os.Exit(1)
}
if err = dbUpdate(); err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
connectionManager = NewConnectionManager()
go connectionManager.BroadcastLoop()

View File

@ -136,7 +136,7 @@ header .menu {
-webkit-tap-highlight-color: transparent;
}
.node-name {
padding: 32px;
margin: 32px 0 16px 0;
background: #fff;
color: #000;
text-align: center;
@ -144,7 +144,7 @@ header .menu {
}
.node-content {
justify-self: center;
padding: 0px 32px;
padding: 16px 32px;
white-space: pre-wrap;
font-size: 0.85em;
color: #333;
@ -155,6 +155,7 @@ header .menu {
}
.node-content:empty {
background: #ddd;
border-radius: 8px;
height: 48px;
}
@media only screen and (max-width: 100ex) {

View File

@ -162,7 +162,7 @@ header {
}
.node-name {
padding: 32px;
margin: 32px 0 16px 0;
background: #fff;
color: #000;
text-align: center;
@ -171,7 +171,7 @@ header {
.node-content {
justify-self: center;
padding: 0px 32px;
padding: 16px 32px;
white-space: pre-wrap;
font-size: 0.85em;
color: #333;
@ -183,6 +183,7 @@ header {
&:empty {
background: #ddd;
border-radius: 8px;
height: 48px;
}
}