Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

register pgx in main cli, not internal #30

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/migrate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
migrate
8 changes: 8 additions & 0 deletions cmd/migrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"strings"

"github.com/golang-migrate/migrate/v4/internal/cli"
"github.com/infobloxopen/hotload"
_ "github.com/infobloxopen/hotload/fsnotify"
"github.com/jackc/pgx/v4/stdlib"
"github.com/lib/pq"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand All @@ -12,7 +16,7 @@

func init() {
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)

Check failure on line 19 in cmd/migrate/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `viper.BindPFlags` is not checked (errcheck)

Check failure on line 19 in cmd/migrate/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `viper.BindPFlags` is not checked (errcheck)
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AddConfigPath(viper.GetString("config.source"))
Expand All @@ -25,6 +29,10 @@
// logrus formatter
customFormatter := new(logrus.JSONFormatter)
logrus.SetFormatter(customFormatter)

hotload.RegisterSQLDriver("pgx", stdlib.GetDefaultDriver())
hotload.RegisterSQLDriver("postgres", pq.Driver{})
hotload.RegisterSQLDriver("postgresql", pq.Driver{})
}

func main() {
Expand Down
26 changes: 13 additions & 13 deletions database/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ var drivers = make(map[string]Driver)
// Driver is the interface every database driver must implement.
//
// How to implement a database driver?
// 1. Implement this interface.
// 2. Optionally, add a function named `WithInstance`.
// This function should accept an existing DB instance and a Config{} struct
// and return a driver instance.
// 3. Add a test that calls database/testing.go:Test()
// 4. Add own tests for Open(), WithInstance() (when provided) and Close().
// All other functions are tested by tests in database/testing.
// Saves you some time and makes sure all database drivers behave the same way.
// 5. Call Register in init().
// 6. Create a internal/cli/build_<driver-name>.go file
// 7. Add driver name in 'DATABASE' variable in Makefile
// 1. Implement this interface.
// 2. Optionally, add a function named `WithInstance`.
// This function should accept an existing DB instance and a Config{} struct
// and return a driver instance.
// 3. Add a test that calls database/testing.go:Test()
// 4. Add own tests for Open(), WithInstance() (when provided) and Close().
// All other functions are tested by tests in database/testing.
// Saves you some time and makes sure all database drivers behave the same way.
// 5. Call Register in init().
// 6. Create a internal/cli/build_<driver-name>.go file
// 7. Add driver name in 'DATABASE' variable in Makefile
//
// Guidelines:
// * Don't try to correct user input. Don't assume things.
// - Don't try to correct user input. Don't assume things.
// When in doubt, return an error and explain the situation to the user.
// * All configuration input must come from the URL string in func Open()
// - All configuration input must come from the URL string in func Open()
// or the Config{} struct in WithInstance. Don't os.Getenv().
type Driver interface {
// Open returns a new driver instance configured with parameters
Expand Down
4 changes: 2 additions & 2 deletions database/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (m *Mongo) Open(dsn string) (database.Driver, error) {
return mc, nil
}

//Parse the url param, convert it to boolean
// Parse the url param, convert it to boolean
// returns error if param invalid. returns defaultValue if param not present
func parseBoolean(urlParam string, defaultValue bool) (bool, error) {

Expand All @@ -199,7 +199,7 @@ func parseBoolean(urlParam string, defaultValue bool) (bool, error) {
return defaultValue, nil
}

//Parse the url param, convert it to int
// Parse the url param, convert it to int
// returns error if param invalid. returns defaultValue if param not present
func parseInt(urlParam string, defaultValue int) (int, error) {

Expand Down
22 changes: 0 additions & 22 deletions internal/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package cli

import (
"database/sql"
"database/sql/driver"
"fmt"
"github.com/jackc/pgx/v4/stdlib"
"net/url"
"os"
"os/signal"
Expand All @@ -13,9 +11,6 @@ import (
"syscall"
"time"

"github.com/infobloxopen/hotload"
_ "github.com/infobloxopen/hotload/fsnotify"
"github.com/lib/pq"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"

Expand Down Expand Up @@ -74,10 +69,6 @@ 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 @@ -163,19 +154,6 @@ Database drivers: `+strings.Join(database.List(), ", ")+"\n", createUsage, gotoU
var migraterErr error

if driver := viper.GetString("database.driver"); driver == "hotload" {
u, err := url.Parse(databasePtr)
if err != nil {
log.fatalErr(fmt.Errorf("could not parse hotload dsn %v: %s", databasePtr, err))
}
hostname := u.Hostname()
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)
if err != nil {
log.fatalErr(fmt.Errorf("could not open hotload dsn %s: %s", databasePtr, err))
Expand Down
5 changes: 3 additions & 2 deletions migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (

// sourceStubMigrations hold the following migrations:
// u = up migration, d = down migration, n = version
// | 1 | - | 3 | 4 | 5 | - | 7 |
// | u d | - | u | u d | d | - | u d |
//
// | 1 | - | 3 | 4 | 5 | - | 7 |
// | u d | - | u | u d | d | - | u d |
var sourceStubMigrations *source.Migrations

const (
Expand Down
24 changes: 12 additions & 12 deletions source/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ var drivers = make(map[string]Driver)
// Driver is the interface every source driver must implement.
//
// How to implement a source driver?
// 1. Implement this interface.
// 2. Optionally, add a function named `WithInstance`.
// This function should accept an existing source instance and a Config{} struct
// and return a driver instance.
// 3. Add a test that calls source/testing.go:Test()
// 4. Add own tests for Open(), WithInstance() (when provided) and Close().
// All other functions are tested by tests in source/testing.
// Saves you some time and makes sure all source drivers behave the same way.
// 5. Call Register in init().
// 1. Implement this interface.
// 2. Optionally, add a function named `WithInstance`.
// This function should accept an existing source instance and a Config{} struct
// and return a driver instance.
// 3. Add a test that calls source/testing.go:Test()
// 4. Add own tests for Open(), WithInstance() (when provided) and Close().
// All other functions are tested by tests in source/testing.
// Saves you some time and makes sure all source drivers behave the same way.
// 5. Call Register in init().
//
// Guidelines:
// * All configuration input must come from the URL string in func Open()
// - All configuration input must come from the URL string in func Open()
// or the Config{} struct in WithInstance. Don't os.Getenv().
// * Drivers are supposed to be read only.
// * Ideally don't load any contents (into memory) in Open or WithInstance.
// - Drivers are supposed to be read only.
// - Ideally don't load any contents (into memory) in Open or WithInstance.
type Driver interface {
// Open returns a new driver instance configured with parameters
// coming from the URL string. Migrate will call this function
Expand Down
12 changes: 7 additions & 5 deletions source/go_bindata/examples/migrations/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions source/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ var (
)

// Regex matches the following pattern:
// 123_name.up.ext
// 123_name.down.ext
//
// 123_name.up.ext
// 123_name.down.ext
var Regex = regexp.MustCompile(`^([0-9]+)_(.*)\.(` + string(Down) + `|` + string(Up) + `)\.(.*)$`)

// Parse returns Migration for matching Regex pattern.
Expand Down
5 changes: 3 additions & 2 deletions source/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
// It assumes that the driver tests has access to the following migrations:
//
// u = up migration, d = down migration, n = version
// | 1 | - | 3 | 4 | 5 | - | 7 |
// | u d | - | u | u d | d | - | u d |
//
// | 1 | - | 3 | 4 | 5 | - | 7 |
// | u d | - | u | u d | d | - | u d |
//
// See source/stub/stub_test.go or source/file/file_test.go for an example.
func Test(t *testing.T, d source.Driver) {
Expand Down
1 change: 0 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type MultiError struct {
// NewMultiError returns an error type holding multiple errors.
//
// Deprecated: Use github.com/hashicorp/go-multierror instead
//
func NewMultiError(errs ...error) MultiError {
compactErrs := make([]error, 0)
for _, e := range errs {
Expand Down
Loading