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

56
db.go
View File

@ -21,7 +21,7 @@ var (
embedded embed.FS embedded embed.FS
) )
func dbInit() (err error) { func dbInit() (err error) {// {{{
dbConn = fmt.Sprintf( dbConn = fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
config.Database.Host, config.Database.Host,
@ -37,11 +37,57 @@ func dbInit() (err error) {
config.Database.Port, config.Database.Port,
) )
db, err = sqlx.Connect("postgres", dbConn) if db, err = sqlx.Connect("postgres", dbConn); err != nil {
return 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. /* 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
@ -82,6 +128,6 @@ func dbUpdate() (err error) {
} }
return return
} }// }}}
// vim: foldmethod=marker // vim: foldmethod=marker

View File

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

View File

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

View File

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