Skip to content

Commit

Permalink
Merge pull request #11 from noppoMan/check-migration-corruption
Browse files Browse the repository at this point in the history
Check migration corruption
  • Loading branch information
noppoMan authored Jan 17, 2017
2 parents 74ec7e7 + 0ae6be6 commit cc66062
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
29 changes: 28 additions & 1 deletion Sources/SwiftKnex/Migration/Migrator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,22 @@ extension Date {
public enum MigrationError: Error {
case invalidMigrationName(String)
case timestampFormatShouldBe(String)
case migrationIsCorrupt
case migrationClassIsMissing(String)
}

extension MigrationError: CustomStringConvertible {
public var description: String {
switch self {
case .invalidMigrationName(let name):
return "The migration class name [\(name)] is invalid format"

case .timestampFormatShouldBe(let format):
return "The timestamp format shoud be \(format)"

case .migrationClassIsMissing(let name):
return "The migration directory is corrupt, the following files are missing \(name)"
}
}
}

public protocol Migratable: class {
Expand Down Expand Up @@ -115,6 +130,9 @@ class MigrateRunner {
return
}

// check migration corruption
try checkMigrationCorruption(migrationsPeformed)

// After second times
for m in knexMigrations {
let name = try validateMigration(name: m.name)
Expand Down Expand Up @@ -164,6 +182,15 @@ class MigrateRunner {
}
}

fileprivate func checkMigrationCorruption(_ migrationsPeformed: [MigrationSchema]) throws {
let names = knexMigrations.map({ $0.name })
try migrationsPeformed.map({ $0.name }).forEach {
if !names.contains($0) {
throw MigrationError.migrationClassIsMissing($0)
}
}
}

fileprivate func fetchMigrations(trx: Connection) throws -> [MigrationSchema] {
guard let results = try con.knex().table(con.config.migration.table).fetch(trx: trx) else {
return []
Expand Down
21 changes: 20 additions & 1 deletion Tests/SwiftKnexTests/MigrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class MigrationTests: XCTestCase {
static var allTests : [(String, (MigrationTests) -> () throws -> Void)] {
return [
("testMigrateLatest", testMigrateLatest),
("testMigrateRollback", testMigrateRollback)
("testMigrateRollback", testMigrateRollback),
("testMigrateCorruption", testMigrateCorruption),
]
}

Expand Down Expand Up @@ -125,5 +126,23 @@ class MigrationTests: XCTestCase {
XCTAssertNil(try! con.knex().table("knex_migrations").fetch())
}

func testMigrateCorruption() {
do {
let runner1 = try MigrateRunner(config: basicKnexConfig(), knexMigrations: [Migration_20170101000000_CreateEmployee()])
try runner1.up()
XCTAssertEqual(1, try con.knex().table("knex_migrations").fetch()!.count)

let runner2 = try MigrateRunner(config: basicKnexConfig(), knexMigrations: [Migration_20170102000000_CreateCompany()])
try runner2.up()

XCTFail("Never called")

} catch MigrationError.migrationClassIsMissing(let name) {
XCTAssertEqual(name, "Migration_20170101000000_CreateEmployee")
} catch {
XCTFail("\(error)")
}
}

}

0 comments on commit cc66062

Please sign in to comment.