Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 27 additions & 9 deletions Sources/SQLiteData/CloudKit/SyncEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,23 @@
.select(\.file)
.fetchOne(db)
if let attachedMetadatabasePath {
let attachedMetadatabaseName = URL(filePath: metadatabase.path).lastPathComponent
let metadatabaseName = URL(filePath: attachedMetadatabasePath).lastPathComponent
if attachedMetadatabaseName != metadatabaseName {
let metadatabaseName =
metadatabase.path.isEmpty
? try URL.metadatabase(
databasePath: "",
containerIdentifier: self.container.containerIdentifier
)
.lastPathComponent
: URL(filePath: metadatabase.path).lastPathComponent
let attachedMetadatabaseName =
URL(string: attachedMetadatabasePath)?.lastPathComponent ?? ""

try URL.metadatabase(
databasePath: attachedMetadatabasePath,
containerIdentifier: self.container.containerIdentifier
)
.lastPathComponent
if metadatabaseName != attachedMetadatabaseName {
throw SchemaError(
reason: .metadatabaseMismatch(
attachedPath: attachedMetadatabasePath,
Expand Down Expand Up @@ -1917,6 +1931,7 @@
databasePath: String,
containerIdentifier: String?
) throws -> URL {
let databasePath = databasePath.isEmpty ? ":memory:" : databasePath
guard let databaseURL = URL(string: databasePath)
else {
struct InvalidDatabasePath: Error {}
Expand All @@ -1927,9 +1942,9 @@
return URL(string: "file:\(String.sqliteDataCloudKitSchemaName)?mode=memory&cache=shared")!
}
return
databaseURL
.deletingLastPathComponent()
.appending(component: ".\(databaseURL.deletingPathExtension().lastPathComponent)")
databaseURL.deletingLastPathComponent().appending(
component: ".\(databaseURL.deletingPathExtension().lastPathComponent)"
)
.appendingPathExtension("metadata\(containerIdentifier.map { "-\($0)" } ?? "").sqlite")
}

Expand Down Expand Up @@ -2022,12 +2037,16 @@
databasePath: databasePath,
containerIdentifier: containerIdentifier
)
let path = url.path(percentEncoded: false)
let path = url.isInMemory ? url.absoluteString : url.path(percentEncoded: false)
try FileManager.default.createDirectory(
at: .applicationSupportDirectory,
withIntermediateDirectories: true
)
_ = try DatabasePool(path: path).write { db in
let database: any DatabaseWriter =
url.isInMemory
? try DatabaseQueue(path: path)
: try DatabasePool(path: path)
_ = try database.write { db in
try #sql("SELECT 1").execute(db)
}
try #sql(
Expand All @@ -2044,7 +2063,6 @@
package struct SchemaError: LocalizedError {
package enum Reason {
case cycleDetected
case inMemoryDatabase
case invalidForeignKey(ForeignKey)
case invalidForeignKeyAction(ForeignKey)
case invalidTableName(String)
Expand Down