Skip to content

Commit f82a988

Browse files
committed
Add functional options to constructors
This is a preparatory commit that adds the ability to add new options to any of the constructor functions, without breaking backward compatibility. An actual option is going to be added in the next commit, this just introduces the mechanism.
1 parent 604248c commit f82a988

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

migrate.go

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,29 @@ func (e ErrDirty) Error() string {
5555
return fmt.Sprintf("Dirty database version %v. Fix and force version.", e.Version)
5656
}
5757

58+
// options is a set of optional options that can be set when a Migrate instance
59+
// is created.
60+
type options struct {
61+
}
62+
63+
// defaultOptions returns a new options struct with default values.
64+
func defaultOptions() options {
65+
return options{}
66+
}
67+
68+
// Option is a function that can be used to set options on a Migrate instance.
69+
type Option func(*options)
70+
5871
type Migrate struct {
5972
sourceName string
6073
sourceDrv source.Driver
6174
databaseName string
6275
databaseDrv database.Driver
6376

77+
// opts is a set of options that can be used to modify the behavior
78+
// of the Migrate instance.
79+
opts options
80+
6481
// Log accepts a Logger interface
6582
Log Logger
6683

@@ -84,8 +101,8 @@ type Migrate struct {
84101

85102
// New returns a new Migrate instance from a source URL and a database URL.
86103
// The URL scheme is defined by each driver.
87-
func New(sourceURL, databaseURL string) (*Migrate, error) {
88-
m := newCommon()
104+
func New(sourceURL, databaseURL string, opts ...Option) (*Migrate, error) {
105+
m := newCommon(opts)
89106

90107
sourceName, err := iurl.SchemeFromURL(sourceURL)
91108
if err != nil {
@@ -118,8 +135,10 @@ func New(sourceURL, databaseURL string) (*Migrate, error) {
118135
// and an existing database instance. The source URL scheme is defined by each driver.
119136
// Use any string that can serve as an identifier during logging as databaseName.
120137
// You are responsible for closing the underlying database client if necessary.
121-
func NewWithDatabaseInstance(sourceURL string, databaseName string, databaseInstance database.Driver) (*Migrate, error) {
122-
m := newCommon()
138+
func NewWithDatabaseInstance(sourceURL string, databaseName string,
139+
databaseInstance database.Driver, opts ...Option) (*Migrate, error) {
140+
141+
m := newCommon(opts)
123142

124143
sourceName, err := iurl.SchemeFromURL(sourceURL)
125144
if err != nil {
@@ -144,8 +163,10 @@ func NewWithDatabaseInstance(sourceURL string, databaseName string, databaseInst
144163
// and a database URL. The database URL scheme is defined by each driver.
145164
// Use any string that can serve as an identifier during logging as sourceName.
146165
// You are responsible for closing the underlying source client if necessary.
147-
func NewWithSourceInstance(sourceName string, sourceInstance source.Driver, databaseURL string) (*Migrate, error) {
148-
m := newCommon()
166+
func NewWithSourceInstance(sourceName string, sourceInstance source.Driver,
167+
databaseURL string, opts ...Option) (*Migrate, error) {
168+
169+
m := newCommon(opts)
149170

150171
databaseName, err := iurl.SchemeFromURL(databaseURL)
151172
if err != nil {
@@ -170,8 +191,11 @@ func NewWithSourceInstance(sourceName string, sourceInstance source.Driver, data
170191
// database instance. Use any string that can serve as an identifier during logging
171192
// as sourceName and databaseName. You are responsible for closing down
172193
// the underlying source and database client if necessary.
173-
func NewWithInstance(sourceName string, sourceInstance source.Driver, databaseName string, databaseInstance database.Driver) (*Migrate, error) {
174-
m := newCommon()
194+
func NewWithInstance(sourceName string, sourceInstance source.Driver,
195+
databaseName string, databaseInstance database.Driver,
196+
opts ...Option) (*Migrate, error) {
197+
198+
m := newCommon(opts)
175199

176200
m.sourceName = sourceName
177201
m.databaseName = databaseName
@@ -182,8 +206,13 @@ func NewWithInstance(sourceName string, sourceInstance source.Driver, databaseNa
182206
return m, nil
183207
}
184208

185-
func newCommon() *Migrate {
209+
func newCommon(optFunctions []Option) *Migrate {
210+
opts := defaultOptions()
211+
for _, opt := range optFunctions {
212+
opt(&opts)
213+
}
186214
return &Migrate{
215+
opts: opts,
187216
GracefulStop: make(chan bool, 1),
188217
PrefetchMigrations: DefaultPrefetchMigrations,
189218
LockTimeout: DefaultLockTimeout,

0 commit comments

Comments
 (0)