Skip to content

Commit

Permalink
fix(DataStore): Store larger than 32-bit values in Int64 over Int
Browse files Browse the repository at this point in the history
  • Loading branch information
lawmicha committed Nov 16, 2023
1 parent 7b507f3 commit b622589
Show file tree
Hide file tree
Showing 32 changed files with 97 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum ModelFieldType {

case string
case int
case int64
case double
case date
case dateTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import Foundation
public struct ConflictResolutionDecorator: ModelBasedGraphQLDocumentDecorator {

private let version: Int?
private let lastSync: Int?
private let lastSync: Int64?
private let graphQLType: GraphQLOperationType
private var primaryKeysOnly: Bool

public init(version: Int? = nil,
lastSync: Int? = nil,
lastSync: Int64? = nil,
graphQLType: GraphQLOperationType,
primaryKeysOnly: Bool = true) {
self.version = version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protocol ModelSyncGraphQLRequestFactory {
where predicate: QueryPredicate?,
limit: Int?,
nextToken: String?,
lastSync: Int?,
lastSync: Int64?,
authType: AWSAuthorizationType?) -> GraphQLRequest<SyncQueryResult>

}
Expand Down Expand Up @@ -110,7 +110,7 @@ extension GraphQLRequest: ModelSyncGraphQLRequestFactory {
where predicate: QueryPredicate? = nil,
limit: Int? = nil,
nextToken: String? = nil,
lastSync: Int? = nil,
lastSync: Int64? = nil,
authType: AWSAuthorizationType? = nil) -> GraphQLRequest<SyncQueryResult> {
syncQuery(modelSchema: modelType.schema,
where: predicate,
Expand Down Expand Up @@ -215,7 +215,7 @@ extension GraphQLRequest: ModelSyncGraphQLRequestFactory {
where predicate: QueryPredicate? = nil,
limit: Int? = nil,
nextToken: String? = nil,
lastSync: Int? = nil,
lastSync: Int64? = nil,
authType: AWSAuthorizationType? = nil) -> GraphQLRequest<SyncQueryResult> {
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelSchema: modelSchema,
operationType: .query,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ extension Int: GraphQLDocumentValueRepresentable {
}
}

extension Int64: GraphQLDocumentValueRepresentable {
public var graphQLDocumentValue: String {
return "\(self)"
}

public var graphQLInlineValue: String {
return "\(self)"
}
}

extension String: GraphQLDocumentValueRepresentable {
public var graphQLDocumentValue: String {
return self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ extension Model {
return Fatal.preconditionFailure("Could not turn into json object from \(value)")
}
}
case .string, .int, .double, .timestamp, .bool:
case .string, .int, .int64, .double, .timestamp, .bool:
input[name] = value
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension ModelSyncMetadata {

definition.fields(
.id(),
.field(keys.lastSync, is: .optional, ofType: .int)
.field(keys.lastSync, is: .optional, ofType: .int64)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public struct ModelSyncMetadata: Model {
public let id: String

/// The timestamp (in Unix seconds) at which the last sync was started, as reported by the service
public var lastSync: Int?
public var lastSync: Int64?

public init(id: String,
lastSync: Int?) {
lastSync: Int64?) {
self.id = id
self.lastSync = lastSync
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public struct MutationSync<ModelType: Model>: Decodable {
self.syncMetadata = MutationSyncMetadata(modelId: modelIdentifier,
modelName: resolvedModelName,
deleted: deleted,
lastChangedAt: Int(lastChangedAt),
lastChangedAt: Int64(lastChangedAt),
version: Int(version))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension MutationSyncMetadata {
definition.fields(
.id(),
.field(sync.deleted, is: .required, ofType: .bool),
.field(sync.lastChangedAt, is: .required, ofType: .int),
.field(sync.lastChangedAt, is: .required, ofType: .int64),
.field(sync.version, is: .required, ofType: .int)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public struct MutationSyncMetadata: Model {

public let id: MutationSyncIdentifier
public var deleted: Bool
public var lastChangedAt: Int
public var lastChangedAt: Int64
public var version: Int

static let deliminator = "|"
Expand All @@ -30,14 +30,14 @@ public struct MutationSyncMetadata: Model {
The format of the `id` has changed to support unique ids across mutiple model types.
Use init(modelId:modelName:deleted:lastChangedAt) to pass in the `modelName`.
""")
public init(id: MutationSyncIdentifier, deleted: Bool, lastChangedAt: Int, version: Int) {
public init(id: MutationSyncIdentifier, deleted: Bool, lastChangedAt: Int64, version: Int) {
self.id = id
self.deleted = deleted
self.lastChangedAt = lastChangedAt
self.version = version
}

public init(modelId: ModelId, modelName: String, deleted: Bool, lastChangedAt: Int, version: Int) {
public init(modelId: ModelId, modelName: String, deleted: Bool, lastChangedAt: Int64, version: Int) {
self.id = Self.identifier(modelName: modelName, modelId: modelId)
self.deleted = deleted
self.lastChangedAt = lastChangedAt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ import Foundation
public struct PaginatedList<ModelType: Model>: Decodable {
public let items: [MutationSync<ModelType>]
public let nextToken: String?
public let startedAt: Int?
public let startedAt: Int64?
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class GraphQLSyncQueryTests: XCTestCase {
XCTAssertEqual(variables["nextToken"] as? String, "token")
XCTAssertNotNil(variables["filter"])
XCTAssertNotNil(variables["lastSync"])
XCTAssertEqual(variables["lastSync"] as? Int, 123)
XCTAssertEqual(variables["lastSync"] as? Int64, 123)
}

func testSyncGraphQLQueryForComment() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class GraphQLRequestAnyModelWithSyncTests: XCTestCase {
let modelType = Post.self as Model.Type
let nextToken = "nextToken"
let limit = 100
let lastSync = 123
let lastSync: Int64 = 123
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelSchema: modelType.schema, operationType: .query)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .sync))
documentBuilder.add(decorator: PaginationDecorator(limit: limit, nextToken: nextToken))
Expand Down Expand Up @@ -274,14 +274,14 @@ class GraphQLRequestAnyModelWithSyncTests: XCTestCase {
XCTAssertNotNil(variables["nextToken"])
XCTAssertEqual(variables["nextToken"] as? String, nextToken)
XCTAssertNotNil(variables["lastSync"])
XCTAssertEqual(variables["lastSync"] as? Int, lastSync)
XCTAssertEqual(variables["lastSync"] as? Int64, lastSync)
}

func testOptimizedSyncQueryGraphQLRequestWithFilter() {
let modelType = Post.self as Model.Type
let nextToken = "nextToken"
let limit = 100
let lastSync = 123
let lastSync: Int64 = 123
let postId = "123"
let predicate = Post.CodingKeys.id.eq(postId)
let request = GraphQLRequest<SyncQueryResult>.syncQuery(
Expand Down Expand Up @@ -310,7 +310,7 @@ class GraphQLRequestAnyModelWithSyncTests: XCTestCase {
let modelType = Post.self as Model.Type
let nextToken = "nextToken"
let limit = 100
let lastSync = 123
let lastSync: Int64 = 123
let postId = "123"
let altPostId = "456"
let predicate = Post.CodingKeys.id.eq(postId) || Post.CodingKeys.id.eq(altPostId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class GraphQLRequestAuthRuleTests: XCTestCase {
let modelType = Article.self as Model.Type
let nextToken = "nextToken"
let limit = 100
let lastSync = 123
let lastSync: Int64 = 123
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelSchema: modelType.schema, operationType: .query)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .sync))
documentBuilder.add(decorator: PaginationDecorator(limit: limit, nextToken: nextToken))
Expand Down Expand Up @@ -347,6 +347,6 @@ class GraphQLRequestAuthRuleTests: XCTestCase {
XCTAssertNotNil(variables["nextToken"])
XCTAssertEqual(variables["nextToken"] as? String, nextToken)
XCTAssertNotNil(variables["lastSync"])
XCTAssertEqual(variables["lastSync"] as? Int, lastSync)
XCTAssertEqual(variables["lastSync"] as? Int64, lastSync)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class GraphQLRequestSyncCustomPrimaryKeyWithMultipleFieldsTests: XCTestCase {
func testSyncQueryGraphQLRequestWithDateInPK() throws {
let nextToken = "nextToken"
let limit = 100
let lastSync = 123
let lastSync: Int64 = 123
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: CustomerWithMultipleFieldsinPK.modelName,
operationType: .query)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .sync))
Expand Down Expand Up @@ -188,7 +188,7 @@ class GraphQLRequestSyncCustomPrimaryKeyWithMultipleFieldsTests: XCTestCase {
XCTAssertNotNil(variables["nextToken"])
XCTAssertEqual(variables["nextToken"] as? String, nextToken)
XCTAssertNotNil(variables["lastSync"])
XCTAssertEqual(variables["lastSync"] as? Int, lastSync)
XCTAssertEqual(variables["lastSync"] as? Int64, lastSync)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension MutationSyncMetadataMigration {
public struct MutationSyncMetadataCopy: Model {
public let id: String
public var deleted: Bool
public var lastChangedAt: Int
public var lastChangedAt: Int64
public var version: Int

// MARK: - CodingKeys
Expand All @@ -36,7 +36,7 @@ extension MutationSyncMetadataMigration {
definition.fields(
.id(),
.field(sync.deleted, is: .required, ofType: .bool),
.field(sync.lastChangedAt, is: .required, ofType: .int),
.field(sync.lastChangedAt, is: .required, ofType: .int64),
.field(sync.version, is: .required, ofType: .int)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ extension ModelField: SQLColumn {
switch type {
case .string, .enum, .date, .dateTime, .time, .model:
return .text
case .int, .bool, .timestamp:
case .int, .int64, .bool, .timestamp:
return .integer
case .double:
return .real
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public struct SQLiteModelValueConverter: ModelValueConverter {
return value as? String
case .int:
return value as? Int
case .int64:
return value as? Int64
case .double:
return value as? Double
case .date, .dateTime, .time:
Expand Down Expand Up @@ -68,7 +70,7 @@ public struct SQLiteModelValueConverter: ModelValueConverter {
switch fieldType {
case .string, .date, .dateTime, .time:
return value as? String
case .int:
case .int, .int64:
return value as? Int64
case .double:
return value as? Double
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ extension ModelSchema {
return ModelValueCompare(value1Optional: value1Optional,
value2Optional: value2Optional)
.sortComparator(sortOrder: sortOrder)

case .int64:
guard let value1Optional = value1 as? Int64?, let value2Optional = value2 as? Int64? else {
return false
}
return ModelValueCompare(value1Optional: value1Optional,
value2Optional: value2Optional)
.sortComparator(sortOrder: sortOrder)
case .double:
guard let value1Optional = value1 as? Double?, let value2Optional = value2 as? Double? else {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public struct OutboxMutationEvent {
public struct OutboxMutationEventElement {
public let model: Model
public var version: Int?
public var lastChangedAt: Int?
public var lastChangedAt: Int64?
public var deleted: Bool?
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ final class InitialSyncOperation: AsynchronousOperation {
}
}

private func getLastSyncTime() -> Int? {
private func getLastSyncTime() -> Int64? {
guard !isCancelled else {
finish(result: .successfulVoid)
return nil
Expand Down Expand Up @@ -109,7 +109,7 @@ final class InitialSyncOperation: AsynchronousOperation {
}
}

private func query(lastSyncTime: Int?, nextToken: String? = nil) async {
private func query(lastSyncTime: Int64?, nextToken: String? = nil) async {
guard !isCancelled else {
finish(result: .successfulVoid)
return
Expand Down Expand Up @@ -160,7 +160,7 @@ final class InitialSyncOperation: AsynchronousOperation {

/// Disposes of the query results: Stops if error, reconciles results if success, and kick off a new query if there
/// is a next token
private func handleQueryResults(lastSyncTime: Int?,
private func handleQueryResults(lastSyncTime: Int64?,
graphQLResult: Result<SyncQueryResult, GraphQLResponseError<SyncQueryResult>>) {
guard !isCancelled else {
finish(result: .successfulVoid)
Expand Down Expand Up @@ -198,7 +198,7 @@ final class InitialSyncOperation: AsynchronousOperation {
}
}

private func updateModelSyncMetadata(lastSyncTime: Int?) {
private func updateModelSyncMetadata(lastSyncTime: Int64?) {
guard !isCancelled else {
finish(result: .successfulVoid)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ extension ModelSchema {
if !compare(value1Optional, value2Optional) {
return false
}
case .int64:
guard let value1Optional = value1 as? Int64?, let value2Optional = value2 as? Int64? else {
return false
}
if !compare(value1Optional, value2Optional) {
return false
}
case .double:
guard let value1Optional = value1 as? Double?, let value2Optional = value2 as? Double? else {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ class SQLiteStorageEngineAdapterTests: BaseDataStoreTests {
let metadata = MutationSyncMetadata(modelId: modelId,
modelName: modelName,
deleted: false,
lastChangedAt: Int(Date().timeIntervalSince1970),
lastChangedAt: Int64(Date().timeIntervalSince1970),
version: 1)

storageAdapter.save(metadata) { result in
Expand All @@ -700,12 +700,12 @@ class SQLiteStorageEngineAdapterTests: BaseDataStoreTests {
let metadata1 = MutationSyncMetadata(modelId: UUID().uuidString,
modelName: modelName,
deleted: false,
lastChangedAt: Int(Date().timeIntervalSince1970),
lastChangedAt: Int64(Date().timeIntervalSince1970),
version: 1)
let metadata2 = MutationSyncMetadata(modelId: UUID().uuidString,
modelName: modelName,
deleted: false,
lastChangedAt: Int(Date().timeIntervalSince1970),
lastChangedAt: Int64(Date().timeIntervalSince1970),
version: 1)

let saveMetadata1 = expectation(description: "save metadata1 success")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class StorageAdapterMutationSyncTests: BaseDataStoreTests {
MutationSyncMetadata(modelId: $0.id,
modelName: Post.modelName,
deleted: false,
lastChangedAt: Int(Date().timeIntervalSince1970),
lastChangedAt: Int64(Date().timeIntervalSince1970),
version: 1)
}
populateData(syncMetadataList)
Expand Down
Loading

0 comments on commit b622589

Please sign in to comment.