Skip to content

Commit c441785

Browse files
daniel-garciassuman2-infobloxychen-bloxerGowdasupriya
authored
Hotload support (#31)
* 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]>
1 parent f83486d commit c441785

File tree

13 files changed

+54
-56
lines changed

13 files changed

+54
-56
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ COPY . ./
1515

1616
RUN make build-docker
1717

18-
FROM alpine:latest
18+
FROM alpine:3.18
1919

2020
COPY --from=builder /go/src/github.com/infobloxopen/migrate/cmd/migrate/config /cli/config/
2121
COPY --from=builder /go/src/github.com/infobloxopen/migrate/build/migrate.linux-386 /usr/local/bin/migrate

Dockerfile.github-actions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM alpine:latest
1+
FROM alpine:3.18
22

33
RUN apk add --no-cache ca-certificates
44

cmd/migrate/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
migrate

cmd/migrate/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import (
55
"strings"
66

77
"github.com/golang-migrate/migrate/v4/internal/cli"
8+
"github.com/infobloxopen/hotload"
9+
_ "github.com/infobloxopen/hotload/fsnotify"
10+
"github.com/jackc/pgx/v4/stdlib"
11+
"github.com/lib/pq"
812
"github.com/sirupsen/logrus"
913
"github.com/spf13/pflag"
1014
"github.com/spf13/viper"
@@ -25,6 +29,10 @@ func init() {
2529
// logrus formatter
2630
customFormatter := new(logrus.JSONFormatter)
2731
logrus.SetFormatter(customFormatter)
32+
33+
hotload.RegisterSQLDriver("pgx", stdlib.GetDefaultDriver())
34+
hotload.RegisterSQLDriver("postgres", pq.Driver{})
35+
hotload.RegisterSQLDriver("postgresql", pq.Driver{})
2836
}
2937

3038
func main() {

database/driver.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ var drivers = make(map[string]Driver)
2525
// Driver is the interface every database driver must implement.
2626
//
2727
// How to implement a database driver?
28-
// 1. Implement this interface.
29-
// 2. Optionally, add a function named `WithInstance`.
30-
// This function should accept an existing DB instance and a Config{} struct
31-
// and return a driver instance.
32-
// 3. Add a test that calls database/testing.go:Test()
33-
// 4. Add own tests for Open(), WithInstance() (when provided) and Close().
34-
// All other functions are tested by tests in database/testing.
35-
// Saves you some time and makes sure all database drivers behave the same way.
36-
// 5. Call Register in init().
37-
// 6. Create a internal/cli/build_<driver-name>.go file
38-
// 7. Add driver name in 'DATABASE' variable in Makefile
28+
// 1. Implement this interface.
29+
// 2. Optionally, add a function named `WithInstance`.
30+
// This function should accept an existing DB instance and a Config{} struct
31+
// and return a driver instance.
32+
// 3. Add a test that calls database/testing.go:Test()
33+
// 4. Add own tests for Open(), WithInstance() (when provided) and Close().
34+
// All other functions are tested by tests in database/testing.
35+
// Saves you some time and makes sure all database drivers behave the same way.
36+
// 5. Call Register in init().
37+
// 6. Create a internal/cli/build_<driver-name>.go file
38+
// 7. Add driver name in 'DATABASE' variable in Makefile
3939
//
4040
// Guidelines:
41-
// * Don't try to correct user input. Don't assume things.
41+
// - Don't try to correct user input. Don't assume things.
4242
// When in doubt, return an error and explain the situation to the user.
43-
// * All configuration input must come from the URL string in func Open()
43+
// - All configuration input must come from the URL string in func Open()
4444
// or the Config{} struct in WithInstance. Don't os.Getenv().
4545
type Driver interface {
4646
// Open returns a new driver instance configured with parameters

database/mongodb/mongodb.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (m *Mongo) Open(dsn string) (database.Driver, error) {
182182
return mc, nil
183183
}
184184

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

@@ -199,7 +199,7 @@ func parseBoolean(urlParam string, defaultValue bool) (bool, error) {
199199
return defaultValue, nil
200200
}
201201

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

internal/cli/main.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import (
1111
"syscall"
1212
"time"
1313

14-
"github.com/infobloxopen/hotload"
15-
_ "github.com/infobloxopen/hotload/fsnotify"
16-
"github.com/lib/pq"
1714
flag "github.com/spf13/pflag"
1815
"github.com/spf13/viper"
1916

@@ -41,10 +38,6 @@ const (
4138
forceUsage = `force V Set version V but don't run migration (ignores dirty state)`
4239
)
4340

44-
func init() {
45-
hotload.RegisterSQLDriver("postgres", pq.Driver{})
46-
}
47-
4841
func handleSubCmdHelp(help bool, usage string, flagSet *flag.FlagSet) {
4942
if help {
5043
fmt.Fprintln(os.Stderr, usage)
@@ -161,14 +154,6 @@ Database drivers: `+strings.Join(database.List(), ", ")+"\n", createUsage, gotoU
161154
var migraterErr error
162155

163156
if driver := viper.GetString("database.driver"); driver == "hotload" {
164-
u, err := url.Parse(databasePtr)
165-
if err != nil {
166-
log.fatalErr(fmt.Errorf("could not parse hotload dsn %v: %s", databasePtr, err))
167-
}
168-
hostname := u.Hostname()
169-
if !(hostname == "postgres" || hostname == "pgx") {
170-
log.fatalErr(fmt.Errorf("unsupported hotload base driver: %s", hostname))
171-
}
172157
db, err := sql.Open(driver, databasePtr)
173158
if err != nil {
174159
log.fatalErr(fmt.Errorf("could not open hotload dsn %s: %s", databasePtr, err))

migrate_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import (
1919

2020
// sourceStubMigrations hold the following migrations:
2121
// u = up migration, d = down migration, n = version
22-
// | 1 | - | 3 | 4 | 5 | - | 7 |
23-
// | u d | - | u | u d | d | - | u d |
22+
//
23+
// | 1 | - | 3 | 4 | 5 | - | 7 |
24+
// | u d | - | u | u d | d | - | u d |
2425
var sourceStubMigrations *source.Migrations
2526

2627
const (

source/driver.go

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

source/go_bindata/examples/migrations/bindata.go

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)