Skip to content

Commit

Permalink
register hotload with pgx driver
Browse files Browse the repository at this point in the history
  • Loading branch information
ssuman2-infoblox committed Dec 4, 2023
1 parent f83486d commit 10e6ff8
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions internal/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cli

import (
"database/sql"
"database/sql/driver"
"fmt"
"github.com/jackc/pgx/v4/stdlib"
"net/url"
"os"
"os/signal"
Expand Down Expand Up @@ -41,10 +43,6 @@ const (
forceUsage = `force V Set version V but don't run migration (ignores dirty state)`
)

func init() {
hotload.RegisterSQLDriver("postgres", pq.Driver{})
}

func handleSubCmdHelp(help bool, usage string, flagSet *flag.FlagSet) {
if help {
fmt.Fprintln(os.Stderr, usage)
Expand Down Expand Up @@ -76,6 +74,10 @@ func dbMakeConnectionString(driver, user, password, address, name, ssl string) s
)
}

func registerHotloadWithDriver(name string, driver driver.Driver) {
hotload.RegisterSQLDriver(name, driver)
}

// Main function of a cli application. It is public for backwards compatibility with `cli` package
func Main(version string) {
help := viper.GetBool("help")
Expand Down Expand Up @@ -166,7 +168,12 @@ Database drivers: `+strings.Join(database.List(), ", ")+"\n", createUsage, gotoU
log.fatalErr(fmt.Errorf("could not parse hotload dsn %v: %s", databasePtr, err))
}
hostname := u.Hostname()
if !(hostname == "postgres" || hostname == "pgx") {
switch hostname {
case "postgres":
registerHotloadWithDriver(hostname, pq.Driver{})
case "pgx":
registerHotloadWithDriver(hostname, stdlib.GetDefaultDriver())
default:
log.fatalErr(fmt.Errorf("unsupported hotload base driver: %s", hostname))
}
db, err := sql.Open(driver, databasePtr)
Expand Down

0 comments on commit 10e6ff8

Please sign in to comment.