File tree Expand file tree Collapse file tree 2 files changed +25
-5
lines changed
Sources/PostgresMigrations
Tests/PostgresMigrationsTests Expand file tree Collapse file tree 2 files changed +25
-5
lines changed Original file line number Diff line number Diff line change @@ -269,8 +269,10 @@ public actor DatabaseMigrations {
269269
270270 public init ( rawValue: Int ) { self . rawValue = rawValue }
271271
272+ // Ignore migrations we don't know about
273+ static var ignoreUnknownMigrations : Self { . init( rawValue: 1 << 0 ) }
272274 // Remove database entry for migrations we don't know about
273- static var removeUnknownMigrations : Self { . init( rawValue: 1 << 0 ) }
275+ static var removeUnknownMigrations : Self { . init( rawValue: 1 << 1 ) }
274276 }
275277
276278 /// Revert database migrations that are inconsistent with the migration list
@@ -319,7 +321,10 @@ public actor DatabaseMigrations {
319321 // for each group revert migrations
320322 for group in groups {
321323 let groupMigrations = migrations. filter { $0. group == group }
322- let appliedGroupMigrations = appliedMigrations. filter { $0. group == group }
324+ var appliedGroupMigrations = appliedMigrations. filter { $0. group == group }
325+ if options. contains ( . ignoreUnknownMigrations) {
326+ appliedGroupMigrations = appliedGroupMigrations. filter { registeredMigrations [ $0. name] != nil }
327+ }
323328
324329 let minMigrationCount = min ( groupMigrations. count, appliedGroupMigrations. count)
325330 var i = 0
Original file line number Diff line number Diff line change @@ -339,16 +339,31 @@ final class MigrationTests: XCTestCase {
339339 XCTFail ( )
340340 } catch let error as DatabaseMigrationError where error == . cannotRevertMigration {
341341 }
342+ // Run revert ignoring unknown migrations
343+ try await migrations. revertInconsistent (
344+ client: client,
345+ groups: [ . default] ,
346+ options: . ignoreUnknownMigrations,
347+ logger: Self . logger,
348+ dryRun: false
349+ )
350+ var appliedMigrations = try await getAll ( client: client)
351+ XCTAssertEqual ( appliedMigrations. count, 3 )
352+ XCTAssertEqual ( appliedMigrations [ 0 ] , " test1 " )
353+ XCTAssertEqual ( appliedMigrations [ 1 ] , " test2 " )
354+ XCTAssertEqual ( appliedMigrations [ 2 ] , " test3 " )
355+
356+ // Run revert removing unknown migrations
342357 try await migrations. revertInconsistent (
343358 client: client,
344359 groups: [ . default] ,
345360 options: . removeUnknownMigrations,
346361 logger: Self . logger,
347362 dryRun: false
348363 )
349- let migrations = try await getAll ( client: client)
350- XCTAssertEqual ( migrations . count, 1 )
351- XCTAssertEqual ( migrations [ 0 ] , " test1 " )
364+ appliedMigrations = try await getAll ( client: client)
365+ XCTAssertEqual ( appliedMigrations . count, 1 )
366+ XCTAssertEqual ( appliedMigrations [ 0 ] , " test1 " )
352367 }
353368 }
354369
You can’t perform that action at this time.
0 commit comments