Skip to content

Commit

Permalink
change how --reset works in init-db
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Sep 24, 2018
1 parent 07f361c commit 3a1aa88
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
24 changes: 16 additions & 8 deletions tinode-db/gendb.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,32 @@ import (
"github.com/tinode/chat/server/store/types"
)

func genDb(reset bool, dbSource string, data *Data) {
func genDb(reset bool, config string, data *Data) {
var err error

defer store.Close()

log.Println("Initializing DB...")

err = store.InitDb(dbSource, reset)
err = store.Open(1, config)
if err != nil {
if strings.Contains(err.Error(), " already exists") {
log.Println("DB already exists, NOT reinitializing")
if strings.Contains(err.Error(), "Database not initialized") {
log.Println("Database not found. Creating.", err)
} else if strings.Contains(err.Error(), "Invalid database version") {
log.Println("Wrong DB version, dropping and recreating the database.", err)
} else {
log.Fatal("Failed to init DB: ", err)
log.Fatal("Failed to init DB adapter:", err)
}
} else if reset {
log.Println("Database reset requested")
} else {
log.Println("Successfully initialized", store.GetAdapterName())
log.Println("Database exists, DB version is correct. All done.", store.GetAdapterName())
return
}

err = store.InitDb("", true)
if err != nil {
log.Fatal("Failed to init DB:", err)
}

if data.Users == nil {
log.Println("No data provided, stopping")
return
Expand Down
5 changes: 3 additions & 2 deletions tinode-db/makedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ func getPassword(n int) string {
}

func main() {
var reset = flag.Bool("reset", false, "first delete the database if one exists")
var datafile = flag.String("data", "", "name of file with sample data")
var reset = flag.Bool("reset", false, "force database reset")
var datafile = flag.String("data", "", "name of file with sample data to load")
var conffile = flag.String("config", "./tinode.conf", "config of the database connection")

flag.Parse()

var data Data
Expand Down

0 comments on commit 3a1aa88

Please sign in to comment.