| 
311 | 311 |         .select(\.file)  | 
312 | 312 |         .fetchOne(db)  | 
313 | 313 |       if let attachedMetadatabasePath {  | 
314 |  | -        let attachedMetadatabaseName = URL(filePath: metadatabase.path).lastPathComponent  | 
315 |  | -        let metadatabaseName = URL(filePath: attachedMetadatabasePath).lastPathComponent  | 
316 |  | -        if attachedMetadatabaseName != metadatabaseName {  | 
 | 314 | +        let metadatabaseName =  | 
 | 315 | +          metadatabase.path.isEmpty  | 
 | 316 | +          ? try URL.metadatabase(  | 
 | 317 | +            databasePath: "",  | 
 | 318 | +            containerIdentifier: self.container.containerIdentifier  | 
 | 319 | +          )  | 
 | 320 | +          .lastPathComponent  | 
 | 321 | +          : URL(filePath: metadatabase.path).lastPathComponent  | 
 | 322 | +        let attachedMetadatabaseName =  | 
 | 323 | +          URL(string: attachedMetadatabasePath)?.lastPathComponent ?? ""  | 
 | 324 | + | 
 | 325 | +        try URL.metadatabase(  | 
 | 326 | +          databasePath: attachedMetadatabasePath,  | 
 | 327 | +          containerIdentifier: self.container.containerIdentifier  | 
 | 328 | +        )  | 
 | 329 | +        .lastPathComponent  | 
 | 330 | +        if metadatabaseName != attachedMetadatabaseName {  | 
317 | 331 |           throw SchemaError(  | 
318 | 332 |             reason: .metadatabaseMismatch(  | 
319 | 333 |               attachedPath: attachedMetadatabasePath,  | 
 | 
1917 | 1931 |       databasePath: String,  | 
1918 | 1932 |       containerIdentifier: String?  | 
1919 | 1933 |     ) throws -> URL {  | 
 | 1934 | +      let databasePath = databasePath.isEmpty ? ":memory:" : databasePath  | 
1920 | 1935 |       guard let databaseURL = URL(string: databasePath)  | 
1921 | 1936 |       else {  | 
1922 | 1937 |         struct InvalidDatabasePath: Error {}  | 
 | 
1927 | 1942 |         return URL(string: "file:\(String.sqliteDataCloudKitSchemaName)?mode=memory&cache=shared")!  | 
1928 | 1943 |       }  | 
1929 | 1944 |       return  | 
1930 |  | -        databaseURL  | 
1931 |  | -        .deletingLastPathComponent()  | 
1932 |  | -        .appending(component: ".\(databaseURL.deletingPathExtension().lastPathComponent)")  | 
 | 1945 | +        databaseURL.deletingLastPathComponent().appending(  | 
 | 1946 | +          component: ".\(databaseURL.deletingPathExtension().lastPathComponent)"  | 
 | 1947 | +        )  | 
1933 | 1948 |         .appendingPathExtension("metadata\(containerIdentifier.map { "-\($0)" } ?? "").sqlite")  | 
1934 | 1949 |     }  | 
1935 | 1950 | 
 
  | 
 | 
2022 | 2037 |         databasePath: databasePath,  | 
2023 | 2038 |         containerIdentifier: containerIdentifier  | 
2024 | 2039 |       )  | 
2025 |  | -      let path = url.path(percentEncoded: false)  | 
 | 2040 | +      let path = url.isInMemory ? url.absoluteString : url.path(percentEncoded: false)  | 
2026 | 2041 |       try FileManager.default.createDirectory(  | 
2027 | 2042 |         at: .applicationSupportDirectory,  | 
2028 | 2043 |         withIntermediateDirectories: true  | 
2029 | 2044 |       )  | 
2030 |  | -      _ = try DatabasePool(path: path).write { db in  | 
 | 2045 | +      let database: any DatabaseWriter =  | 
 | 2046 | +        url.isInMemory  | 
 | 2047 | +        ? try DatabaseQueue(path: path)  | 
 | 2048 | +        : try DatabasePool(path: path)  | 
 | 2049 | +      _ = try database.write { db in  | 
2031 | 2050 |         try #sql("SELECT 1").execute(db)  | 
2032 | 2051 |       }  | 
2033 | 2052 |       try #sql(  | 
 | 
2044 | 2063 |     package struct SchemaError: LocalizedError {  | 
2045 | 2064 |       package enum Reason {  | 
2046 | 2065 |         case cycleDetected  | 
2047 |  | -        case inMemoryDatabase  | 
2048 | 2066 |         case invalidForeignKey(ForeignKey)  | 
2049 | 2067 |         case invalidForeignKeyAction(ForeignKey)  | 
2050 | 2068 |         case invalidTableName(String)  | 
 | 
0 commit comments