Skip to content

Commit 642cdbb

Browse files
committed
Fix sync in demo app
1 parent f4cdd3c commit 642cdbb

5 files changed

Lines changed: 28 additions & 8 deletions

File tree

Demos/PowerSyncExample/PowerSyncExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/PowerSync/Implementation/sync/StreamingSyncClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,5 +440,5 @@ struct WriteCheckpointResponse: Codable {
440440
}
441441

442442
private func sleepForSeconds(seconds: TimeInterval) async throws {
443-
try await Task.sleep(nanoseconds: UInt64(seconds * 1_000_000_00))
443+
try await Task.sleep(nanoseconds: UInt64(seconds * 1_000_000_000))
444444
}

Sources/PowerSync/Protocol/Schema/Index.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public protocol IndexProtocol: Sendable {
1212
var columns: [IndexedColumnProtocol] { get }
1313
}
1414

15-
public struct Index: IndexProtocol, Encodable {
15+
public struct Index: IndexProtocol {
1616
public let name: String
1717
public let columns: [IndexedColumnProtocol]
1818

@@ -48,24 +48,31 @@ public struct Index: IndexProtocol, Encodable {
4848
return ascending(name: name, columns: [column])
4949
}
5050

51-
public func encode(to encoder: any Encoder) throws {
51+
internal func encode(table: borrowing Table, to: any UnkeyedEncodingContainer) throws {
5252
enum CodingKeys: CodingKey {
5353
case name
5454
case columns
5555
}
5656

57-
var container = encoder.container(keyedBy: CodingKeys.self)
57+
var to = to
58+
var container = to.nestedContainer(keyedBy: CodingKeys.self)
5859
try container.encode(name, forKey: .name)
5960
var columnsContainer = container.nestedUnkeyedContainer(forKey: .columns)
6061
for column in columns {
6162
enum IndexedColumnCodingKeys: CodingKey {
6263
case name
6364
case ascending
65+
case type
6466
}
6567

6668
var container = columnsContainer.nestedContainer(keyedBy: IndexedColumnCodingKeys.self)
6769
try container.encode(column.column, forKey: .name)
6870
try container.encode(column.ascending, forKey: .ascending)
71+
guard let tableColumn = table.columns.first(where: { c in c.name == column.column }) else {
72+
throw PowerSyncError.operationFailed(message: "Unserializable schema: Index \(self.name) references column \(column.column) which does not exist in \(table.name)")
73+
}
74+
75+
try container.encode(tableColumn.type, forKey: .type)
6976
}
7077
}
7178
}

Sources/PowerSync/Protocol/Schema/Table.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ public struct Table: TableProtocol, Encodable {
190190
try container.encode(name, forKey: .outer(.name))
191191
try container.encodeIfPresent(viewNameOverride, forKey: .outer(.viewName))
192192
try container.encode(columns, forKey: .outer(.columns))
193-
try container.encode(indexes, forKey: .outer(.indexes))
193+
let indexContainer = container.nestedUnkeyedContainer(forKey: .outer(.indexes))
194+
for index in indexes {
195+
try index.encode(table: self, to: indexContainer)
196+
}
194197
try options.serializeTo(container)
195198
}
196199
}

Tests/PowerSyncTests/Schema/TableTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ final class TableTests: XCTestCase {
272272
"columns" : [
273273
{
274274
"ascending" : true,
275-
"name" : "name"
275+
"name" : "name",
276+
"type" : "text"
276277
}
277278
],
278279
"name" : "test_index"

0 commit comments

Comments
 (0)