This commit is contained in:
2026-06-22 11:40:27 +02:00
parent e965775809
commit 01be0c523e
6 changed files with 195 additions and 7 deletions
+26 -3
View File
@@ -2,6 +2,7 @@ package main
import (
"database/sql"
"strings"
_ "modernc.org/sqlite"
)
@@ -38,7 +39,9 @@ CREATE TABLE IF NOT EXISTS entry (
reply_to INTEGER NOT NULL DEFAULT 0,
reply_count INTEGER NOT NULL DEFAULT 0,
last_activity INTEGER NOT NULL DEFAULT 0,
deleted INTEGER NOT NULL DEFAULT 0
deleted INTEGER NOT NULL DEFAULT 0,
bump_count INTEGER NOT NULL DEFAULT 0,
last_bump INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS vote (
@@ -86,6 +89,26 @@ func initDB(dsn string) error {
if err = db.Ping(); err != nil {
return err
}
_, err = db.Exec(schema)
return err
if _, err = db.Exec(schema); err != nil {
return err
}
return migrate()
}
// migrate ergänzt Spalten, die nach dem ersten Deploy dazukamen. CREATE TABLE IF
// NOT EXISTS fasst eine bestehende Tabelle nicht an -> neue Spalten brauchen ein
// ALTER. SQLite kann ADD COLUMN nicht "IF NOT EXISTS", deshalb schlucken wir den
// "duplicate column"-Fehler: bei einer frischen DB sind die Spalten schon aus
// dem Schema da, bei einer alten werden sie hier nachgezogen.
func migrate() error {
alters := []string{
`ALTER TABLE entry ADD COLUMN bump_count INTEGER NOT NULL DEFAULT 0`,
`ALTER TABLE entry ADD COLUMN last_bump INTEGER NOT NULL DEFAULT 0`,
}
for _, stmt := range alters {
if _, err := db.Exec(stmt); err != nil && !strings.Contains(err.Error(), "duplicate column") {
return err
}
}
return nil
}