Skip to content

Commit

Permalink
fix(DataStore): Store larger than 32-bit values in Int64 over Int (#3367
Browse files Browse the repository at this point in the history
)

* fix(DataStore): Store larger than 32-bit values in Int64 over Int

* fix: Removing ModelFieldType.int64
  • Loading branch information
lawmicha authored Dec 1, 2023
1 parent b9b5918 commit 66c46a7
Show file tree
Hide file tree
Showing 27 changed files with 102 additions and 75 deletions.
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 @@ -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 @@ -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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ public struct SQLiteModelValueConverter: ModelValueConverter {
case .string:
return value as? String
case .int:
return value as? Int
if let intValue = value as? Int {
return intValue
}
if let int64Value = value as? Int64 {
return int64Value
}
return nil
case .double:
return value as? Double
case .date, .dateTime, .time:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,19 @@ extension ModelSchema {
value2Optional: value2Optional)
.sortComparator(sortOrder: sortOrder)
case .int, .timestamp:
guard let value1Optional = value1 as? Int?, let value2Optional = value2 as? Int? else {
return false
if let value1Optional = value1 as? Int?, let value2Optional = value2 as? Int? {
return ModelValueCompare(value1Optional: value1Optional,
value2Optional: value2Optional)
.sortComparator(sortOrder: sortOrder)
}
return ModelValueCompare(value1Optional: value1Optional,
value2Optional: value2Optional)
.sortComparator(sortOrder: sortOrder)


if let value1Optional = value1 as? Int64?, let value2Optional = value2 as? Int64? {
return ModelValueCompare(value1Optional: value1Optional,
value2Optional: value2Optional)
.sortComparator(sortOrder: sortOrder)
}

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 @@ -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 @@ -56,12 +56,17 @@ extension ModelSchema {
return false
}
case .int:
guard let value1Optional = value1 as? Int?, let value2Optional = value2 as? Int? else {
return false
if let value1Optional = value1 as? Int?, let value2Optional = value2 as? Int? {
if !compare(value1Optional, value2Optional) {
return false
}
}
if !compare(value1Optional, value2Optional) {
return false
if let value1Optional = value1 as? Int64?, let value2Optional = value2 as? Int64? {
if !compare(value1Optional, value2Optional) {
return false
}
}
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 66c46a7

Please sign in to comment.