Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Add a DB setup check on boot

Kailash Nadhcde0b4b42a3e
diff --git a/cmd/install.go b/cmd/install.go
index d0978958..5695a35c 100644
--- a/cmd/install.go
+++ b/cmd/install.go
@@ -180,3 +180,14 @@ func newConfigFile() error {
return ioutil.WriteFile("config.toml", b, 0644)
}
+
+// checkSchema checks if the DB schema is installed.
+func checkSchema(db *sqlx.DB) (bool, error) {
+ if _, err := db.Exec(`SELECT id FROM templates LIMIT 1`); err != nil {
+ if isTableNotExistErr(err) {
+ return false, nil
+ }
+ return false, err
+ }
+ return true, nil
+}
diff --git a/cmd/main.go b/cmd/main.go
index 02821f95..c2d2b4d4 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -113,6 +113,14 @@ func init() {
install(migList[len(migList)-1].version, db, fs, !ko.Bool("yes"))
os.Exit(0)
}
+
+ // Check if the DB schema is installed.
+ if ok, err := checkSchema(db); err != nil {
+ log.Fatalf("error checking schema in DB: %v", err)
+ } else if !ok {
+ lo.Fatal("the database does not appear to be setup. Run --install.")
+ }
+
if ko.Bool("upgrade") {
upgrade(db, fs, !ko.Bool("yes"))
os.Exit(0)