From cde0b4b42a3e0e18b7e08d701e5ca4482cd6fe73 Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Tue, 10 Nov 2020 22:21:25 +0530 Subject: [PATCH] Add a DB setup check on boot --- cmd/install.go | 11 +++++++++++ cmd/main.go | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/cmd/install.go b/cmd/install.go index d097895..5695a35 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 02821f9..c2d2b4d 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)