File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ public actor Driver {
5757 // create the migrations and try to build to see if things work before
5858 // they create their first query. If it can find the migrations folder
5959 // thats probably good enough.
60- let queriesFiles = try fileSystem. exists ( at: queriesPath)
60+ let queriesFiles = fileSystem. exists ( at: queriesPath)
6161 ? try fileSystem. files ( atPath: queriesPath)
6262 : [ ]
6363
@@ -86,9 +86,9 @@ public actor Driver {
8686 // An array of all migrations source code
8787 let migrations = results. values
8888 . filter { $0. usage == . migration }
89- . sorted ( by : { $0. fileName < $1. fileName } )
90- . flatMap ( \ . statements )
91- . map ( \. sanitizedSource )
89+ . reduce ( into : [ : ] ) { $0 [ $1 . fileName, default : [ ] ] . append ( contentsOf : $1. statements ) }
90+ . map { ( $0 . key , $0 . value . map ( \ . sanitizedSource ) . joined ( separator : " \n " ) ) }
91+ . map ( \. 1 )
9292
9393 // An array of all queries grouped by their file name
9494 let queries = results. values
Original file line number Diff line number Diff line change @@ -127,6 +127,7 @@ public struct SwiftLanguage: Language {
127127 /// file and requires a little extra treatment
128128 public func macro(
129129 databaseName: String ,
130+ migrations: [ String ] ,
130131 tables: [ GeneratedModel ] ,
131132 queries: [ GeneratedQuery ] ,
132133 addConnection: Bool ,
@@ -146,6 +147,20 @@ public struct SwiftLanguage: Language {
146147 self . adapters ( adapters: adapters)
147148 take ( )
148149
150+ writer. write ( " static var sanitizedMigrations: [String] " )
151+ writer. braces {
152+ writer. write ( line: " return " )
153+ writer. brackets {
154+ for (position, migration) in migrations. positional ( ) {
155+ multilineStringLiteral ( of: migration)
156+
157+ if !position. isLast {
158+ writer. write ( " , " )
159+ }
160+ }
161+ }
162+ }
163+
149164 for table in tables {
150165 declaration ( for: table, isOutput: true )
151166 take ( )
@@ -238,7 +253,7 @@ public struct SwiftLanguage: Language {
238253 }
239254 }
240255 }
241- writer. write ( line: " ) " )
256+ writer. write ( line: " ) " )
242257
243258 writer. braces {
244259 for adapter in adapters {
Original file line number Diff line number Diff line change @@ -15,9 +15,16 @@ public protocol Database: ConnectionWrapper {
1515 init ( connection: any Connection , adapters: Adapters )
1616 /// An ordered list of migrations to be run.
1717 static var migrations : [ String ] { get }
18+ /// The `migrations` sanitized with all non-valid SQL removed.
19+ ///
20+ /// Note: This only exists for the @Database macro. The macro will
21+ /// generate this. Not needed for the build tool plugin
22+ static var sanitizedMigrations : [ String ] { get }
1823}
1924
2025public extension Database {
26+ static var sanitizedMigrations : [ String ] { migrations }
27+
2128 /// Opens a connection pool to the database at the given URL.
2229 ///
2330 /// - Parameter url: The url of the database file
@@ -41,13 +48,13 @@ public extension Database {
4148 try ConnectionPool (
4249 path: path,
4350 limit: config. maxConnectionCount,
44- migrations: Self . migrations
51+ migrations: Self . sanitizedMigrations
4552 )
4653 } else {
4754 try ConnectionPool (
4855 path: " :memory: " ,
4956 limit: 1 ,
50- migrations: Self . migrations
57+ migrations: Self . sanitizedMigrations
5158 )
5259 }
5360
Original file line number Diff line number Diff line change @@ -35,10 +35,17 @@ extension DatabaseMacro: MemberMacro {
3535 }
3636
3737 var compiler = Compiler ( )
38+ var sanitizedMigrations : [ String ] = [ ]
3839 var queries : [ Statement ] = [ ]
3940
4041 for (migration, expr) in migrations {
41- let ( _, diagnostics) = compiler. compile ( migration: migration)
42+ let ( statements, diagnostics) = compiler. compile ( migration: migration)
43+
44+ sanitizedMigrations. append (
45+ statements
46+ . map ( \. sanitizedSource)
47+ . joined ( separator: " \n " )
48+ )
4249
4350 for diag in diagnostics {
4451 context. addDiagnostics ( from: diag, node: expr)
@@ -78,6 +85,7 @@ extension DatabaseMacro: MemberMacro {
7885
7986 let raw = swift. macro (
8087 databaseName: structDecl. name. text,
88+ migrations: sanitizedMigrations,
8189 tables: values. tables,
8290 queries: values. queries. flatMap ( \. 1 ) ,
8391 addConnection: variables [ " connection " ] == nil ,
You can’t perform that action at this time.
0 commit comments