Skip to content

Commit

Permalink
fix: db:setup error #211
Browse files Browse the repository at this point in the history
  • Loading branch information
dosco committed Apr 18, 2021
1 parent ecb876b commit 8390a56
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
27 changes: 17 additions & 10 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/cmd_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)

Expand All @@ -100,6 +102,7 @@ func cmdDBNew() func(*cobra.Command, []string) {
}

initCmd(cpath)
initDB(false)

name := args[0]
migrationsPath := conf.RelPath(conf.MigrationsPath)
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/cmd_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/cmd_seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions serv/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8390a56

Please sign in to comment.