@@ -34,6 +34,19 @@ func NewMigrator(db *pg.DB, cfg Config, rootDir string) *Migrator {
3434 return m
3535}
3636
37+ // writeMigrationToDB inserts log that migration was completed in postgres
38+ func writeMigrationToDB (ctx context.Context , mg Migration , tx * pg.Tx , start time.Time ) error {
39+ finish := time .Now ()
40+ pm := mg .ToDB ()
41+ pm .StartedAt = start
42+ pm .FinishedAt = & finish
43+
44+ if _ , err := tx .ModelContext (ctx , pm ).Insert (); err != nil {
45+ return fmt .Errorf (`add new migration "%s" failed: %w` , mg .Filename , err )
46+ }
47+ return nil
48+ }
49+
3750// readAllFiles read files from migrator root dir and return its filenames
3851func (m * Migrator ) readAllFiles () ([]string , error ) {
3952 dir , err := os .Open (m .rootDir )
@@ -191,17 +204,7 @@ func (m *Migrator) applyMigration(ctx context.Context, mg Migration) (err error)
191204 return fmt .Errorf (`apply migration failed: %w` , err )
192205 }
193206
194- // insert into pgMigrations
195- finish := time .Now ()
196- pm := mg .ToDB ()
197- pm .StartedAt = start
198- pm .FinishedAt = & finish
199-
200- if _ , err = tx .ModelContext (ctx , pm ).Insert (); err != nil {
201- return fmt .Errorf (`add new migration failed: %w` , err )
202- }
203-
204- return nil
207+ return writeMigrationToDB (ctx , mg , tx , start )
205208}
206209
207210// setStatementTimeout set statement timeout to transaction connection
@@ -293,14 +296,52 @@ func (m *Migrator) dryRunMigrations(ctx context.Context, mm Migrations, chCurren
293296 return fmt .Errorf (`apply migration "%s" failed: %w` , mg .Filename , err )
294297 }
295298
296- // insert into pgMigrations
297- finish := time .Now ()
298- pm := mg .ToDB ()
299- pm .StartedAt = start
300- pm .FinishedAt = & finish
299+ if err = writeMigrationToDB (ctx , mg , tx , start ); err != nil {
300+ return err
301+ }
302+ }
301303
302- if _ , err = tx .ModelContext (ctx , pm ).Insert (); err != nil {
303- return fmt .Errorf (`add new migration "%s" failed: %w` , mg .Filename , err )
304+ return nil
305+ }
306+
307+ // Skip marks migrations as completed
308+ func (m * Migrator ) Skip (ctx context.Context , filenames []string , chCurrentFile chan string ) error {
309+ defer close (chCurrentFile )
310+
311+ // create migration table if not exists
312+ if err := m .createMigratorTable (ctx ); err != nil {
313+ return err
314+ }
315+
316+ // prepare migrations
317+ mm , err := m .newMigrations (filenames )
318+ if err != nil {
319+ return fmt .Errorf ("prepare migrations failed: %w" , err )
320+ }
321+
322+ // skip migrations
323+ if err := m .skipMigrations (ctx , mm , chCurrentFile ); err != nil {
324+ return fmt .Errorf ("skip migrations failed: %w" , err )
325+ }
326+ return nil
327+ }
328+
329+ func (m * Migrator ) skipMigrations (ctx context.Context , mm Migrations , chCurrentFile chan string ) (err error ) {
330+ var tx * pg.Tx
331+ tx , err = m .db .Begin ()
332+ if err != nil {
333+ return fmt .Errorf (`begin transaction failed: %w` , err )
334+ }
335+
336+ defer func () {
337+ err = finishTxOnErr (tx , err )
338+ }()
339+
340+ // write migrations to pgMigrations table
341+ for _ , mg := range mm {
342+ chCurrentFile <- mg .Filename
343+ if err = writeMigrationToDB (ctx , mg , tx , time .Now ()); err != nil {
344+ return err
304345 }
305346 }
306347
0 commit comments