Skip to content

Commit 1da7906

Browse files
authored
fix(datastore): dont use where.id() function (#1130)
1 parent 9e99b9d commit 1da7906

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

aws-datastore/src/androidTest/java/com/amplifyframework/datastore/storage/sqlite/SQLiteStorageAdapterSaveTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.junit.BeforeClass;
3636
import org.junit.Test;
3737

38+
import java.util.Collections;
3839
import java.util.HashMap;
3940
import java.util.HashSet;
4041
import java.util.List;
@@ -322,4 +323,33 @@ public void patchItemOnlyHasChangedFields() throws AmplifyException, Interrupted
322323
observer.assertValueCount(1);
323324
observer.assertValueAt(0, storageItemChange -> storageItemChange.patchItem().equals(expectedItem));
324325
}
326+
327+
/**
328+
* Test save with predicate. Confirms that conditionally updating a nested model also works.
329+
* @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
330+
*/
331+
@Test
332+
public void saveModelWithPredicateUpdatesForNestedModels() throws DataStoreException {
333+
// Save a model
334+
final BlogOwner mark = BlogOwner.builder()
335+
.name("Mark")
336+
.build();
337+
adapter.save(mark);
338+
339+
// Save a model that belongs to another model
340+
final Blog marksBlog = Blog.builder()
341+
.name("Mark's very first blog.")
342+
.owner(mark)
343+
.build();
344+
adapter.save(marksBlog);
345+
346+
// Update a model that belongs to another model
347+
final Blog marksBlogEdit = marksBlog.copyOfBuilder()
348+
.name("Mark's edited blog.")
349+
.build();
350+
adapter.save(marksBlogEdit);
351+
352+
// Assert that update went through successfully
353+
assertEquals(Collections.singletonList(marksBlogEdit), adapter.query(Blog.class));
354+
}
325355
}

aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteStorageAdapter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,11 +852,17 @@ private boolean modelExists(Model model, QueryPredicate predicate) throws DataSt
852852
* @return the Model instance from SQLite, if it exists, otherwise null.
853853
*/
854854
private Model query(Model model) {
855+
final String modelName = getModelName(model);
856+
final ModelSchema schema = modelSchemaRegistry.getModelSchemaForModelClass(modelName);
857+
final SQLiteTable table = SQLiteTable.fromSchema(schema);
858+
final String primaryKeyName = table.getPrimaryKeyColumnName();
859+
final QueryPredicate matchId = QueryField.field(primaryKeyName).eq(model.getId());
860+
855861
Iterator<? extends Model> result = Single.<Iterator<? extends Model>>create(emitter -> {
856862
if (model instanceof SerializedModel) {
857-
query(getModelName(model), Where.id(model.getId()), emitter::onSuccess, emitter::onError);
863+
query(modelName, Where.matches(matchId), emitter::onSuccess, emitter::onError);
858864
} else {
859-
query(model.getClass(), Where.id(model.getId()), emitter::onSuccess, emitter::onError);
865+
query(model.getClass(), Where.matches(matchId), emitter::onSuccess, emitter::onError);
860866
}
861867
}).blockingGet();
862868
return result.hasNext() ? result.next() : null;

0 commit comments

Comments
 (0)