From 8390a5604fdd7dbe2ce1f92aee3390e0aef1ec1e Mon Sep 17 00:00:00 2001 From: Vikram Rangnekar Date: Sun, 18 Apr 2021 02:16:34 -0700 Subject: [PATCH] fix: db:setup error #211 --- internal/cmd/cmd.go | 27 +++++++++++++++++---------- internal/cmd/cmd_db.go | 3 +++ internal/cmd/cmd_migrate.go | 2 ++ internal/cmd/cmd_seed.go | 1 + serv/db.go | 4 ++-- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 7ea89188..1e6c308b 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -17,11 +17,13 @@ import ( ) var ( - bi serv.BuildInfo - log *zap.SugaredLogger - db *sql.DB - conf *serv.Config - cpath string + s *serv.Service + bi serv.BuildInfo + log *zap.SugaredLogger + db *sql.DB + dbUsed bool + conf *serv.Config + cpath string ) func Cmd() { @@ -178,13 +180,9 @@ Copyright 2021, Vikram Rangnekar } func initCmd(cpath string) { - var s *serv.Service - var err error - if conf != nil { return } - cp, err := filepath.Abs(cpath) if err != nil { log.Fatal(err) @@ -197,10 +195,19 @@ func initCmd(cpath string) { if s, err = serv.NewGraphJinService(conf); err != nil { log.Fatal(err) } +} + +func initDB(useDB bool) { + var err error + + if db != nil && useDB == dbUsed { + return + } - if db, err = s.NewDB(); err != nil { + if db, err = s.NewDB(useDB); err != nil { log.Fatalf("Failed to connect to database: %s", err) } + dbUsed = useDB } func GetConfigName() string { diff --git a/internal/cmd/cmd_db.go b/internal/cmd/cmd_db.go index b6399f47..df692ec6 100644 --- a/internal/cmd/cmd_db.go +++ b/internal/cmd/cmd_db.go @@ -49,6 +49,7 @@ func cmdDBReset() func(*cobra.Command, []string) { func cmdDBCreate() func(*cobra.Command, []string) { return func(cmd *cobra.Command, args []string) { initCmd(cpath) + initDB(false) if conf.DB.Type == "mysql" { log.Fatalf("Database creation not support with MySQL") @@ -81,6 +82,7 @@ func cmdDBCreate() func(*cobra.Command, []string) { func cmdDBDrop() func(*cobra.Command, []string) { return func(cmd *cobra.Command, args []string) { initCmd(cpath) + initDB(false) sql := fmt.Sprintf(`DROP DATABASE IF EXISTS "%s"`, conf.DB.DBName) @@ -100,6 +102,7 @@ func cmdDBNew() func(*cobra.Command, []string) { } initCmd(cpath) + initDB(false) name := args[0] migrationsPath := conf.RelPath(conf.MigrationsPath) diff --git a/internal/cmd/cmd_migrate.go b/internal/cmd/cmd_migrate.go index abf95ef7..bb9157fa 100644 --- a/internal/cmd/cmd_migrate.go +++ b/internal/cmd/cmd_migrate.go @@ -29,6 +29,7 @@ func cmdDBMigrate() func(*cobra.Command, []string) { dest := args[0] initCmd(cpath) + initDB(true) if conf.DB.Type == "mysql" { log.Fatal("Migrations not support with MySQL") @@ -120,6 +121,7 @@ func cmdDBMigrate() func(*cobra.Command, []string) { func cmdDBStatus() func(*cobra.Command, []string) { return func(cmd *cobra.Command, args []string) { initCmd(cpath) + initDB(true) if conf.DB.Type == "mysql" { log.Fatal("Migrations not support with MySQL") diff --git a/internal/cmd/cmd_seed.go b/internal/cmd/cmd_seed.go index d79512c4..42c58e2b 100644 --- a/internal/cmd/cmd_seed.go +++ b/internal/cmd/cmd_seed.go @@ -28,6 +28,7 @@ import ( func cmdDBSeed() func(*cobra.Command, []string) { return func(cmd *cobra.Command, args []string) { initCmd(cpath) + initDB(true) if conf.DB.Type == "mysql" { log.Fatalf("Seed scripts not support with MySQL") diff --git a/serv/db.go b/serv/db.go index 67b6f9ae..cc5ac4a7 100644 --- a/serv/db.go +++ b/serv/db.go @@ -33,8 +33,8 @@ type dbConf struct { connString string } -func (s *Service) NewDB() (*sql.DB, error) { - return s.newDB(false, false) +func (s *Service) NewDB(useDB bool) (*sql.DB, error) { + return s.newDB(useDB, false) } func (s *Service) newDB(useDB, useTelemetry bool) (*sql.DB, error) {