Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datastore): Update Model Class Check in getMutationForModelId() from using ModelSchema class name to mutatedItem modelName #2637

Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ <T extends Model> Completable merge(
.flatMapCompletable(shouldMerge -> {
Completable firstStep;
if (mutationOutbox.hasPendingMutation(model.getPrimaryKeyString(),
model.getClass().getName())) {
model.getModelName())) {
LOG.info("Mutation outbox has pending mutation for Model: " +
model.getModelName() + " with primary key: " + model.resolveIdentifier()
+ ". Saving the metadata, but not model itself.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ PendingMutation<? extends Model> getMutationForModelId(@NonNull String modelId,
try {
PendingMutation.PersistentRecord persistentRecord = results.next();
PendingMutation<?> pendingMutation = converter.fromRecord(persistentRecord);
if (pendingMutation.getModelSchema().getModelClass().getName().equals(modelClass)) {
if (pendingMutation.getModelSchema().getName().equals(modelClass)) {
mutationResult.set(pendingMutation);
}
} catch (Throwable throwable) {
Expand Down Expand Up @@ -153,7 +153,7 @@ public <T extends Model> Completable enqueue(@NonNull PendingMutation<T> incomin
// If there is no existing mutation for the model, then just apply the incoming
// mutation, and be done with this.
String modelId = incomingMutation.getMutatedItem().getPrimaryKeyString();
String modelClass = incomingMutation.getModelSchema().getModelClass().getName();
String modelClass = incomingMutation.getMutatedItem().getModelName();
@SuppressWarnings("unchecked")
PendingMutation<T> existingMutation = (PendingMutation<T>) getMutationForModelId(modelId, modelClass);
if (existingMutation == null || inFlightMutations.contains(existingMutation.getMutationId())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void canDrainMutationOutbox() throws DataStoreException {

// And that it is no longer in the outbox.
assertFalse(mutationOutbox.hasPendingMutation(tony.getPrimaryKeyString(),
tony.getClass().getName()));
tony.getClass().getSimpleName()));

// And that it was passed to AppSync for publication.
verify(appSync).create(eq(tony), any(), any(), any());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void enqueuePersistsMutationAndNotifiesObserver() throws DataStoreExcepti
Collections.singletonList(converter.toRecord(createJameson)),
storage.query(PersistentRecord.class)
);
assertTrue(mutationOutbox.hasPendingMutation(jameson.getId(), jameson.getClass().getName()));
assertTrue(mutationOutbox.hasPendingMutation(jameson.getId(), jameson.getClass().getSimpleName()));
assertEquals(createJameson, mutationOutbox.peek());
}

Expand Down Expand Up @@ -208,8 +208,8 @@ public void loadPreparesOutbox() throws DataStoreException, InterruptedException
loadObserver.dispose();

// Assert: items are in the outbox.
assertTrue(mutationOutbox.hasPendingMutation(tony.getId(), tony.getClass().getName()));
assertTrue(mutationOutbox.hasPendingMutation(sam.getId(), sam.getClass().getName()));
assertTrue(mutationOutbox.hasPendingMutation(tony.getId(), tony.getClass().getSimpleName()));
assertTrue(mutationOutbox.hasPendingMutation(sam.getId(), sam.getClass().getSimpleName()));

// Tony is first, since he is the older of the two mutations.
assertEquals(updateTony, mutationOutbox.peek());
Expand Down Expand Up @@ -240,7 +240,7 @@ public void removeRemovesChangesFromQueue() throws DataStoreException, Interrupt
assertEquals(0, storage.query(PersistentRecord.class).size());

assertNull(mutationOutbox.peek());
assertFalse(mutationOutbox.hasPendingMutation(bill.getId(), bill.getClass().getName()));
assertFalse(mutationOutbox.hasPendingMutation(bill.getId(), bill.getClass().getSimpleName()));
}

/**
Expand Down Expand Up @@ -330,8 +330,8 @@ public void hasPendingMutationReturnsTrueForExistingModelMutation() {
boolean completed = mutationOutbox.enqueue(pendingMutation).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS);

assertTrue(completed);
assertTrue(mutationOutbox.hasPendingMutation(modelId, joe.getClass().getName()));
assertFalse(mutationOutbox.hasPendingMutation(mutationId.toString(), mutationId.getClass().getName()));
assertTrue(mutationOutbox.hasPendingMutation(modelId, joe.getClass().getSimpleName()));
assertFalse(mutationOutbox.hasPendingMutation(mutationId.toString(), mutationId.getClass().getSimpleName()));
}

/**
Expand All @@ -357,9 +357,9 @@ public void hasPendingMutationReturnsFalseForItemNotInStore() throws DataStoreEx
mutationId, joe, schema, PendingMutation.Type.CREATE, QueryPredicates.all()
);

assertFalse(mutationOutbox.hasPendingMutation(joeId, joe.getClass().getName()));
assertFalse(mutationOutbox.hasPendingMutation(joeId, joe.getClass().getSimpleName()));
assertFalse(mutationOutbox.hasPendingMutation(unrelatedMutation.getMutationId().toString(),
unrelatedMutation.getClass().getName()));
unrelatedMutation.getClass().getSimpleName()));
}

/**
Expand All @@ -385,7 +385,7 @@ public void hasPendingMutationReturnsFalseForModelMutationWithSamePrimaryKeyForD

// Act & Assert: Enqueue and verify BlogOwner
assertTrue(mutationOutbox.enqueue(pendingBlogOwnerMutation).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS));
assertTrue(mutationOutbox.hasPendingMutation(modelId, blogOwner.getClass().getName()));
assertTrue(mutationOutbox.hasPendingMutation(modelId, blogOwner.getClass().getSimpleName()));

// Act & Assert: Enqueue and verify Author
Author author = Author.builder()
Expand All @@ -394,7 +394,7 @@ public void hasPendingMutationReturnsFalseForModelMutationWithSamePrimaryKeyForD
.build();

// Check hasPendingMutation returns False for Author with same Primary Key (id) as BlogOwner
assertFalse(mutationOutbox.hasPendingMutation(modelId, author.getClass().getName()));
assertFalse(mutationOutbox.hasPendingMutation(modelId, author.getClass().getSimpleName()));

PendingMutation<Author> pendingAuthorMutation = PendingMutation.instance(
mutationId, author, ModelSchema.fromModelClass(Author.class),
Expand All @@ -403,7 +403,7 @@ public void hasPendingMutationReturnsFalseForModelMutationWithSamePrimaryKeyForD
assertTrue(mutationOutbox.enqueue(pendingAuthorMutation).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS));

// Make sure Author Mutation is stored
assertTrue(mutationOutbox.hasPendingMutation(modelId, author.getClass().getName()));
assertTrue(mutationOutbox.hasPendingMutation(modelId, author.getClass().getSimpleName()));

// Act & Assert: Enqueue and verify Author
Post post = Post.builder()
Expand All @@ -414,7 +414,7 @@ public void hasPendingMutationReturnsFalseForModelMutationWithSamePrimaryKeyForD
.build();

// Check hasPendingMutation returns False for Post with same Primary Key (id) as BlogOwner
assertFalse(mutationOutbox.hasPendingMutation(modelId, post.getClass().getName()));
assertFalse(mutationOutbox.hasPendingMutation(modelId, post.getClass().getSimpleName()));

PendingMutation<Post> pendingPostMutation = PendingMutation.instance(
mutationId, post, ModelSchema.fromModelClass(Post.class),
Expand All @@ -423,7 +423,7 @@ public void hasPendingMutationReturnsFalseForModelMutationWithSamePrimaryKeyForD
assertTrue(mutationOutbox.enqueue(pendingPostMutation).blockingAwait(TIMEOUT_MS, TimeUnit.MILLISECONDS));

// Make sure Post Mutation is stored
assertTrue(mutationOutbox.hasPendingMutation(modelId, post.getClass().getName()));
assertTrue(mutationOutbox.hasPendingMutation(modelId, post.getClass().getSimpleName()));
}

/**
Expand Down Expand Up @@ -474,7 +474,7 @@ public void mutationEnqueueForModelWithDuplicatePrimaryKeyThrowsDatastoreExcepti

// Additional Checks: Peek the Mutation outbox, existing mutation should be present.
assertTrue(mutationOutbox.hasPendingMutation(existingBlogOwner.getPrimaryKeyString(),
existingBlogOwner.getClass().getName()));
existingBlogOwner.getClass().getSimpleName()));
assertEquals(existingCreation, mutationOutbox.peek());
}

Expand Down Expand Up @@ -521,7 +521,7 @@ public void existingCreationIncomingCreationYieldsError() throws AmplifyExceptio

// Existing mutation still attainable as next mutation (right now, its the ONLY mutation in outbox)
assertTrue(mutationOutbox.hasPendingMutation(modelInExistingMutation.getPrimaryKeyString(),
modelInExistingMutation.getClass().getName()));
modelInExistingMutation.getClass().getSimpleName()));
assertEquals(existingCreation, mutationOutbox.peek());
}

Expand Down Expand Up @@ -567,7 +567,7 @@ public void existingUpdateIncomingCreationYieldsError() throws AmplifyException,

// Existing mutation still attainable as next mutation (right now, its the ONLY mutation in outbox)
assertTrue(mutationOutbox.hasPendingMutation(modelInExistingMutation.getPrimaryKeyString(),
modelInExistingMutation.getClass().getName()));
modelInExistingMutation.getClass().getSimpleName()));
assertEquals(existingUpdate, mutationOutbox.peek());
}

Expand Down Expand Up @@ -614,7 +614,7 @@ public void existingDeletionIncomingCreationYieldsError() throws AmplifyExceptio

// Existing mutation still attainable as next mutation (right now, its the ONLY mutation in outbox)
assertTrue(mutationOutbox.hasPendingMutation(modelInExistingMutation.getPrimaryKeyString(),
modelInExistingMutation.getClass().getName()));
modelInExistingMutation.getClass().getSimpleName()));
assertEquals(existingDeletion, mutationOutbox.peek());
}

Expand Down Expand Up @@ -660,7 +660,7 @@ public void existingDeletionIncomingUpdateYieldsError() throws AmplifyException,

// Existing mutation still attainable as next mutation (right now, its the ONLY mutation in outbox)
assertTrue(mutationOutbox.hasPendingMutation(modelInExistingMutation.getPrimaryKeyString(),
modelInExistingMutation.getClass().getName()));
modelInExistingMutation.getClass().getSimpleName()));
assertEquals(existingDeletion, mutationOutbox.peek());
}

Expand Down Expand Up @@ -1186,7 +1186,7 @@ public void nextItemForModelIdReturnsFirstEnqueued() throws DataStoreException {
assertTrue(completed);
assertEquals(
firstMutation,
mutationOutbox.getMutationForModelId(originalJoe.getId(), originalJoe.getClass().getName())
mutationOutbox.getMutationForModelId(originalJoe.getId(), originalJoe.getClass().getSimpleName())
);
}

Expand Down
Loading