@@ -55,12 +55,29 @@ func (e ErrDirty) Error() string {
55
55
return fmt .Sprintf ("Dirty database version %v. Fix and force version." , e .Version )
56
56
}
57
57
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
+
58
71
type Migrate struct {
59
72
sourceName string
60
73
sourceDrv source.Driver
61
74
databaseName string
62
75
databaseDrv database.Driver
63
76
77
+ // opts is a set of options that can be used to modify the behavior
78
+ // of the Migrate instance.
79
+ opts options
80
+
64
81
// Log accepts a Logger interface
65
82
Log Logger
66
83
@@ -84,8 +101,8 @@ type Migrate struct {
84
101
85
102
// New returns a new Migrate instance from a source URL and a database URL.
86
103
// 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 )
89
106
90
107
sourceName , err := iurl .SchemeFromURL (sourceURL )
91
108
if err != nil {
@@ -118,8 +135,10 @@ func New(sourceURL, databaseURL string) (*Migrate, error) {
118
135
// and an existing database instance. The source URL scheme is defined by each driver.
119
136
// Use any string that can serve as an identifier during logging as databaseName.
120
137
// 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 )
123
142
124
143
sourceName , err := iurl .SchemeFromURL (sourceURL )
125
144
if err != nil {
@@ -144,8 +163,10 @@ func NewWithDatabaseInstance(sourceURL string, databaseName string, databaseInst
144
163
// and a database URL. The database URL scheme is defined by each driver.
145
164
// Use any string that can serve as an identifier during logging as sourceName.
146
165
// 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 )
149
170
150
171
databaseName , err := iurl .SchemeFromURL (databaseURL )
151
172
if err != nil {
@@ -170,8 +191,11 @@ func NewWithSourceInstance(sourceName string, sourceInstance source.Driver, data
170
191
// database instance. Use any string that can serve as an identifier during logging
171
192
// as sourceName and databaseName. You are responsible for closing down
172
193
// 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 )
175
199
176
200
m .sourceName = sourceName
177
201
m .databaseName = databaseName
@@ -182,8 +206,13 @@ func NewWithInstance(sourceName string, sourceInstance source.Driver, databaseNa
182
206
return m , nil
183
207
}
184
208
185
- func newCommon () * Migrate {
209
+ func newCommon (optFunctions []Option ) * Migrate {
210
+ opts := defaultOptions ()
211
+ for _ , opt := range optFunctions {
212
+ opt (& opts )
213
+ }
186
214
return & Migrate {
215
+ opts : opts ,
187
216
GracefulStop : make (chan bool , 1 ),
188
217
PrefetchMigrations : DefaultPrefetchMigrations ,
189
218
LockTimeout : DefaultLockTimeout ,
0 commit comments