@@ -17,18 +17,18 @@ public struct MigrationRunner {
1717 public static func execute( migrations: [ String ] , tx: borrowing Transaction ) throws {
1818 try createTableIfNeeded ( tx: tx)
1919
20- // let lastMigration = try lastMigration
21- // .replaceNil(with: 0)
22- // .execute(with: (), tx: tx)
23- //
24- // let pendingMigrations = migrations.enumerated()
25- // .map { (number: $0.offset + 1, migration: $0.element) }
26- // .filter { $0.number > lastMigration }
27- // .sorted { $0.number < $1.number }
28- //
29- // for (number, migration) in pendingMigrations {
30- // try execute(migration: migration, number: number, tx: tx)
31- // }
20+ let lastMigration = try lastMigration
21+ . replaceNil ( with: 0 )
22+ . execute ( with: ( ) , tx: tx)
23+
24+ let pendingMigrations = migrations. enumerated ( )
25+ . map { ( number: $0. offset + 1 , migration: $0. element) }
26+ . filter { $0. number > lastMigration }
27+ . sorted { $0. number < $1. number }
28+
29+ for (number, migration) in pendingMigrations {
30+ try execute ( migration: migration, number: number, tx: tx)
31+ }
3232 }
3333
3434 static func createTableIfNeeded( tx: borrowing Transaction ) throws ( FeatherError) {
@@ -41,24 +41,24 @@ public struct MigrationRunner {
4141
4242 static func execute( migration: String , number: Int , tx: borrowing Transaction ) throws {
4343 try tx. execute ( sql: migration)
44- // try insertMigration.execute(with: number, tx: tx)
44+ try insertMigration. execute ( with: number, tx: tx)
4545 }
4646
47- // private static var lastMigration: FetchSingleQuery<(), Int> {
48- // return FetchSingleQuery(.read) { _, transaction in
49- // try Statement(in: transaction) {
50- // "SELECT MAX(number) FROM \(MigrationRunner.migrationTableName)"
51- // }
52- // }
53- // }
54- //
55- // private static var insertMigration: VoidQuery<Int> {
56- // return VoidQuery(.write) { input, transaction in
57- // try Statement(in: transaction) {
58- // "INSERT INTO \(MigrationRunner.migrationTableName) (number) VALUES (?)"
59- // } bind: { statement in
60- // try statement.bind(value: input, to: 1)
61- // }
62- // }
63- // }
47+ private static var lastMigration : FetchSingleQuery < ( ) , Int > {
48+ return FetchSingleQuery ( . read) { _, transaction in
49+ try Statement ( in: transaction) {
50+ " SELECT MAX(number) FROM \( MigrationRunner . migrationTableName) "
51+ }
52+ }
53+ }
54+
55+ private static var insertMigration : VoidQuery < Int > {
56+ return VoidQuery ( . write) { input, transaction in
57+ try Statement ( in: transaction) {
58+ " INSERT INTO \( MigrationRunner . migrationTableName) (number) VALUES (?) "
59+ } bind: { statement in
60+ try statement. bind ( value: input, to: 1 )
61+ }
62+ }
63+ }
6464}
0 commit comments