diff --git a/cmd/internal/serv/cmd_migrate.go b/cmd/internal/serv/cmd_migrate.go index b133bd8..0a5a75a 100644 --- a/cmd/internal/serv/cmd_migrate.go +++ b/cmd/internal/serv/cmd_migrate.go @@ -55,7 +55,7 @@ func cmdDBReset(cmd *cobra.Command, args []string) { func cmdDBCreate(cmd *cobra.Command, args []string) { initConfOnce() - db, err := initDB(conf) + db, err := initDB(conf, false) if err != nil { log.Fatalf("ERR failed to connect to database: %s", err) } @@ -74,7 +74,7 @@ func cmdDBCreate(cmd *cobra.Command, args []string) { func cmdDBDrop(cmd *cobra.Command, args []string) { initConfOnce() - db, err := initDB(conf) + db, err := initDB(conf, false) if err != nil { log.Fatalf("ERR failed to connect to database: %s", err) } @@ -131,7 +131,7 @@ func cmdDBMigrate(cmd *cobra.Command, args []string) { initConfOnce() dest := args[0] - conn, err := initDB(conf) + conn, err := initDB(conf, true) if err != nil { log.Fatalf("ERR failed to connect to database: %s", err) } @@ -223,7 +223,7 @@ func cmdDBMigrate(cmd *cobra.Command, args []string) { func cmdDBStatus(cmd *cobra.Command, args []string) { initConfOnce() - db, err := initDB(conf) + db, err := initDB(conf, true) if err != nil { log.Fatalf("ERR failed to connect to database: %s", err) } diff --git a/cmd/internal/serv/cmd_seed.go b/cmd/internal/serv/cmd_seed.go index 18ee186..ae20ac9 100644 --- a/cmd/internal/serv/cmd_seed.go +++ b/cmd/internal/serv/cmd_seed.go @@ -28,7 +28,7 @@ func cmdDBSeed(cmd *cobra.Command, args []string) { conf.Production = false - db, err = initDB(conf) + db, err = initDB(conf, true) if err != nil { log.Fatalf("ERR failed to connect to database: %s", err) } diff --git a/cmd/internal/serv/cmd_serv.go b/cmd/internal/serv/cmd_serv.go index 3a20f73..254c416 100644 --- a/cmd/internal/serv/cmd_serv.go +++ b/cmd/internal/serv/cmd_serv.go @@ -19,7 +19,7 @@ func cmdServ(cmd *cobra.Command, args []string) { initWatcher() - db, err = initDB(conf) + db, err = initDB(conf, true) if err != nil { fatalInProd(err, "failed to connect to database") } diff --git a/cmd/internal/serv/init.go b/cmd/internal/serv/init.go index 9dc9cfa..15c509d 100644 --- a/cmd/internal/serv/init.go +++ b/cmd/internal/serv/init.go @@ -79,7 +79,7 @@ func initConf() (*Config, error) { return c, nil } -func initDB(c *Config) (*sql.DB, error) { +func initDB(c *Config, useDB bool) (*sql.DB, error) { var db *sql.DB var err error @@ -107,7 +107,6 @@ func initDB(c *Config) (*sql.DB, error) { config, _ := pgx.ParseConfig("") config.Host = c.DB.Host config.Port = c.DB.Port - config.Database = c.DB.DBName config.User = c.DB.User config.Password = c.DB.Password config.RuntimeParams = map[string]string{ @@ -115,6 +114,10 @@ func initDB(c *Config) (*sql.DB, error) { "search_path": c.DB.Schema, } + if useDB { + config.Database = c.DB.DBName + } + // switch c.LogLevel { // case "debug": // config.LogLevel = pgx.LogLevelDebug