Skip to content

Commit

Permalink
Hotload support (#31)
Browse files Browse the repository at this point in the history
* pgx migrate test (#29)

* add hotload support (#15)

* import fsync

* Upgrade alpine 3.13 -> 3.18 (#21)

* upgrade alpine 3.13 -> 3.18

* add --pull to docker build

* adding pgx as the supported hotload base driver (#26)

* adding pgx as the supported hotload base driver

* updating go.mod file

* removing the pgx register in migrate (#27)

* register hotload with pgx driver

---------

Co-authored-by: Daniel Garcia <[email protected]>
Co-authored-by: Ying Chen <[email protected]>
Co-authored-by: Supriya Gowda <[email protected]>

* register pgx in main cli, not internal (#30)

* register pgx in main cli, not internal

* go fmt

---------

Co-authored-by: Sujay Kumar Suman <[email protected]>
Co-authored-by: Ying Chen <[email protected]>
Co-authored-by: Supriya Gowda <[email protected]>
  • Loading branch information
4 people authored Dec 8, 2023
1 parent f83486d commit c441785
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ COPY . ./

RUN make build-docker

FROM alpine:latest
FROM alpine:3.18

COPY --from=builder /go/src/github.com/infobloxopen/migrate/cmd/migrate/config /cli/config/
COPY --from=builder /go/src/github.com/infobloxopen/migrate/build/migrate.linux-386 /usr/local/bin/migrate
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.github-actions
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:latest
FROM alpine:3.18

RUN apk add --no-cache ca-certificates

Expand Down
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 @@ import (
"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 @@ -25,6 +29,10 @@ func init() {
// 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
15 changes: 0 additions & 15 deletions internal/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,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 @@ -41,10 +38,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 @@ -161,14 +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()
if !(hostname == "postgres" || hostname == "pgx") {
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

0 comments on commit c441785

Please sign in to comment.