Skip to content

Commit

Permalink
Add a DB setup check on boot
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Nov 10, 2020
1 parent 7a9d11d commit cde0b4b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/install.go
Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions cmd/main.go
Expand Up @@ -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)
Expand Down

0 comments on commit cde0b4b

Please sign in to comment.