Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ EntityInstance addInstance(EntityInstance instance) {
// should only do this if we actually add the item
AutoIncrement counter = counters.get(autoIncrementFieldSet);
if (counter.peekNextValue()
< instance.getFieldValue(autoIncrementFieldSet).asInteger()) {
<= instance.getFieldValue(autoIncrementFieldSet).asInteger()) {
counter.incrementToNextAbove(
instance.getFieldValue(autoIncrementFieldSet).asInteger());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,10 @@ void setNextIdCountersToAccomodate(
if (field != null && field.getType() == FieldType.AUTO_INCREMENT) {
AutoIncrement counter = countersFor(entity).get(field.getName());
if (counter != null) {
counter.incrementToNextAbove(Integer.parseInt(fieldNameValue.value));
int value = Integer.parseInt(fieldNameValue.value);
if (counter.peekNextValue() <= value) {
counter.incrementToNextAbove(value);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,10 @@ void setNextIdCountersToAccomodate(
Field field = entity.getField(fieldNameValue.getName());
if (field != null && field.getType() == FieldType.AUTO_INCREMENT) {
AutoIncrement counter = counterFor(entity, field);
counter.incrementToNextAbove(Integer.parseInt(fieldNameValue.value));
int value = Integer.parseInt(fieldNameValue.value);
if (counter.peekNextValue() <= value) {
counter.incrementToNextAbove(value);
}
}
}
ensureSchemaReady();
Expand Down Expand Up @@ -600,7 +603,7 @@ && countInstances(entity) >= entity.getMaxInstanceLimit()) {
for (String fieldName : explicitAutoIncrementFields) {
AutoIncrement counter = countersFor(entity).get(fieldName);
int value = instance.getFieldValue(fieldName).asInteger();
if (counter.peekNextValue() < value) {
if (counter.peekNextValue() <= value) {
counter.incrementToNextAbove(value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,21 @@ public void sqliteRepositoryGeneratesAutoIdsThroughTheContract() {
}
}

@Test
public void inMemoryRepositoryAccommodatesRestoredAutoIdsInAnyOrder() {
ThingStore repository = new InMemoryThingStore(EntityRelModel.DEFAULT_DATABASE_NAME);

exerciseRestoredAutoIdAccommodation(repository);
}

@Test
public void sqliteRepositoryAccommodatesRestoredAutoIdsInAnyOrder() {
try (ThingStore repository =
SqliteThingStore.inMemory(EntityRelModel.DEFAULT_DATABASE_NAME)) {
exerciseRestoredAutoIdAccommodation(repository);
}
}

@Test
public void sqliteRepositoryThrowsTypedMaxInstanceLimitFailure() {
ERSchema schema = ticketSchema(1);
Expand Down Expand Up @@ -831,6 +846,26 @@ private void exerciseAutoIdGeneration(final ThingStore repository) {
Assertions.assertEquals("26", ticketAfterExplicit.getPrimaryKeyValue());
}

private void exerciseRestoredAutoIdAccommodation(final ThingStore repository) {
ERSchema schema = autoIdSchema();
repository.administration().initializeFrom(schema);

EntityDefinition ticket = schema.getEntityDefinitionNamed("ticket");
String[] restoredIds = {"4", "9", "8", "3", "6", "2", "10", "5", "7", "1"};
for (String restoredId : restoredIds) {
repository
.entities()
.create(
EntityInstanceDraft.forEntity(ticket)
.withProtectedField("id", restoredId));
}

EntityInstance ticketAfterRestore =
repository.entities().create(EntityInstanceDraft.forEntity(ticket));

Assertions.assertEquals("11", ticketAfterRestore.getPrimaryKeyValue());
}

private void exerciseMandatoryRelationshipValidationAndCascade(final ThingStore repository) {
ERSchema schema = mandatoryRelationshipSchema();
repository.administration().initializeFrom(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public void canPostAndCreateAnItemWithJson() {

// header should give me the guid
String guid = response.getHeaders().get(ApiResponse.PRIMARY_KEY_HEADER);
String location = response.getHeaders().get("Location");

final EntityInstance aTodo =
todoManager
Expand All @@ -216,6 +217,7 @@ public void canPostAndCreateAnItemWithJson() {
.findByPrimaryKey(todo, guid);

Assertions.assertEquals("title from json", aTodo.getFieldValue("title").asString());
Assertions.assertEquals("/todos/" + guid, location);

Assertions.assertTrue(
response.getBody().startsWith("{\"guid\":\""), "Should have returned json");
Expand Down
Loading