浏览代码

Add a DB setup check on boot

Kailash Nadh 4 年之前
父节点
当前提交
cde0b4b42a
共有 2 个文件被更改,包括 19 次插入0 次删除
  1. 11 0
      cmd/install.go
  2. 8 0
      cmd/main.go

+ 11 - 0
cmd/install.go

@@ -180,3 +180,14 @@ func newConfigFile() error {
 
 
 	return ioutil.WriteFile("config.toml", b, 0644)
 	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 - 0
cmd/main.go

@@ -113,6 +113,14 @@ func init() {
 		install(migList[len(migList)-1].version, db, fs, !ko.Bool("yes"))
 		install(migList[len(migList)-1].version, db, fs, !ko.Bool("yes"))
 		os.Exit(0)
 		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") {
 	if ko.Bool("upgrade") {
 		upgrade(db, fs, !ko.Bool("yes"))
 		upgrade(db, fs, !ko.Bool("yes"))
 		os.Exit(0)
 		os.Exit(0)