@@ -39,6 +39,8 @@ type MigrationSet struct {
39
39
IgnoreUnknown bool
40
40
// DisableCreateTable disable the creation of the migration table
41
41
DisableCreateTable bool
42
+ // LazyLoad enable migration file to be loaded only when needed.
43
+ LazyLoad bool
42
44
}
43
45
44
46
var migSet = MigrationSet {}
@@ -121,13 +123,27 @@ func SetIgnoreUnknown(v bool) {
121
123
migSet .IgnoreUnknown = v
122
124
}
123
125
126
+ // SetLazyLoad sets the boolean to enable migration file to be loaded only when needed.
127
+ func SetLazyLoad (v bool ) {
128
+ migSet .LazyLoad = v
129
+ }
130
+
131
+ type migrationFile struct {
132
+ dir http.FileSystem
133
+ root string
134
+ baseName string
135
+ }
136
+
124
137
type Migration struct {
125
138
Id string
126
139
Up []string
127
140
Down []string
128
141
129
142
DisableTransactionUp bool
130
143
DisableTransactionDown bool
144
+
145
+ // lazyLoadFile is information of migration file, which is used to load migration file later if not nil.
146
+ lazyLoadFile * migrationFile
131
147
}
132
148
133
149
func (m Migration ) Less (other * Migration ) bool {
@@ -160,6 +176,31 @@ func (m Migration) VersionInt() int64 {
160
176
return value
161
177
}
162
178
179
+ // Load parses migration file if not yet
180
+ func (m * Migration ) Load () error {
181
+ if m .lazyLoadFile == nil {
182
+ return nil
183
+ }
184
+ root := m .lazyLoadFile .root
185
+ name := m .lazyLoadFile .baseName
186
+ file , err := m .lazyLoadFile .dir .Open (path .Join (root , name ))
187
+ if err != nil {
188
+ return fmt .Errorf ("Error while opening %s: %s" , name , err )
189
+ }
190
+ defer func () { _ = file .Close () }()
191
+
192
+ parsed , err := sqlparse .ParseMigration (file )
193
+ if err != nil {
194
+ return fmt .Errorf ("Error parsing migration (%s): %s" , m .Id , err )
195
+ }
196
+ m .Up = parsed .UpStatements
197
+ m .Down = parsed .DownStatements
198
+ m .DisableTransactionUp = parsed .DisableTransactionUp
199
+ m .DisableTransactionDown = parsed .DisableTransactionDown
200
+ m .lazyLoadFile = nil
201
+ return nil
202
+ }
203
+
163
204
type PlannedMigration struct {
164
205
* Migration
165
206
@@ -266,12 +307,23 @@ func findMigrations(dir http.FileSystem, root string) ([]*Migration, error) {
266
307
267
308
for _ , info := range files {
268
309
if strings .HasSuffix (info .Name (), ".sql" ) {
269
- migration , err := migrationFromFile (dir , root , info )
270
- if err != nil {
271
- return nil , err
310
+ if migSet .LazyLoad {
311
+ migration := & Migration {
312
+ Id : info .Name (),
313
+ lazyLoadFile : & migrationFile {
314
+ dir : dir ,
315
+ root : root ,
316
+ baseName : info .Name (),
317
+ },
318
+ }
319
+ migrations = append (migrations , migration )
320
+ } else {
321
+ migration , err := migrationFromFile (dir , root , info )
322
+ if err != nil {
323
+ return nil , err
324
+ }
325
+ migrations = append (migrations , migration )
272
326
}
273
-
274
- migrations = append (migrations , migration )
275
327
}
276
328
}
277
329
@@ -575,7 +627,11 @@ func (ms MigrationSet) PlanMigration(db *sql.DB, dialect string, m MigrationSour
575
627
// Add missing migrations up to the last run migration.
576
628
// This can happen for example when merges happened.
577
629
if len (existingMigrations ) > 0 {
578
- result = append (result , ToCatchup (migrations , existingMigrations , record )... )
630
+ catchUp , err := ToCatchup (migrations , existingMigrations , record )
631
+ if err != nil {
632
+ return nil , nil , err
633
+ }
634
+ result = append (result , catchUp ... )
579
635
}
580
636
581
637
// Figure out which migrations to apply
@@ -585,6 +641,10 @@ func (ms MigrationSet) PlanMigration(db *sql.DB, dialect string, m MigrationSour
585
641
toApplyCount = max
586
642
}
587
643
for _ , v := range toApply [0 :toApplyCount ] {
644
+ err = v .Load ()
645
+ if err != nil {
646
+ return nil , nil , err
647
+ }
588
648
589
649
if dir == Up {
590
650
result = append (result , & PlannedMigration {
@@ -683,7 +743,7 @@ func ToApply(migrations []*Migration, current string, direction MigrationDirecti
683
743
panic ("Not possible" )
684
744
}
685
745
686
- func ToCatchup (migrations , existingMigrations []* Migration , lastRun * Migration ) []* PlannedMigration {
746
+ func ToCatchup (migrations , existingMigrations []* Migration , lastRun * Migration ) ( []* PlannedMigration , error ) {
687
747
missing := make ([]* PlannedMigration , 0 )
688
748
for _ , migration := range migrations {
689
749
found := false
@@ -694,14 +754,18 @@ func ToCatchup(migrations, existingMigrations []*Migration, lastRun *Migration)
694
754
}
695
755
}
696
756
if ! found && migration .Less (lastRun ) {
757
+ err := migration .Load ()
758
+ if err != nil {
759
+ return nil , err
760
+ }
697
761
missing = append (missing , & PlannedMigration {
698
762
Migration : migration ,
699
763
Queries : migration .Up ,
700
764
DisableTransaction : migration .DisableTransactionUp ,
701
765
})
702
766
}
703
767
}
704
- return missing
768
+ return missing , nil
705
769
}
706
770
707
771
func GetMigrationRecords (db * sql.DB , dialect string ) ([]* MigrationRecord , error ) {
0 commit comments