From 6f46a897419b369a3d3792700caeea78c1b2fd87 Mon Sep 17 00:00:00 2001 From: Steven Grimm <1248649+sgrimm@users.noreply.github.com> Date: Tue, 19 Nov 2024 08:58:34 -0800 Subject: [PATCH] SW-6260 Ignore deletion of outdated variable values (#2607) If a variable value has already been deleted or replaced with a newer value, don't attempt to delete it again. Previously, deleting an already-deleted value would cause the request to fail with a server error. --- .../documentproducer/db/VariableValueStore.kt | 25 +++++++++++++++++++ .../db/VariableValueStoreTest.kt | 20 +++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/main/kotlin/com/terraformation/backend/documentproducer/db/VariableValueStore.kt b/src/main/kotlin/com/terraformation/backend/documentproducer/db/VariableValueStore.kt index adb38e7e004..f571d520432 100644 --- a/src/main/kotlin/com/terraformation/backend/documentproducer/db/VariableValueStore.kt +++ b/src/main/kotlin/com/terraformation/backend/documentproducer/db/VariableValueStore.kt @@ -623,6 +623,31 @@ class VariableValueStore( val containingRowId = fetchContainingRowId(valueId) val listPosition = valuesRow.listPosition!! val projectId = operation.projectId + + val isOutdated = + dslContext + .selectOne() + .from(VARIABLE_VALUES) + .leftJoin(VARIABLE_VALUE_TABLE_ROWS) + .on(VARIABLE_VALUES.ID.eq(VARIABLE_VALUE_TABLE_ROWS.VARIABLE_VALUE_ID)) + .where(VARIABLE_VALUES.PROJECT_ID.eq(projectId)) + .and(VARIABLE_VALUES.VARIABLE_ID.eq(variableId)) + .and(VARIABLE_VALUES.LIST_POSITION.eq(listPosition)) + .and( + if (containingRowId != null) { + VARIABLE_VALUE_TABLE_ROWS.TABLE_ROW_VALUE_ID.eq(containingRowId) + } else { + VARIABLE_VALUE_TABLE_ROWS.TABLE_ROW_VALUE_ID.isNull + }) + .and(VARIABLE_VALUES.ID.gt(valueId)) + .fetch() + .isNotEmpty + + if (isOutdated) { + logVariableOperation("Ignoring deletion of outdated value", projectId, variablesRow, valueId) + return + } + val maxListPosition = fetchMaxListPosition(projectId, variableId, containingRowId) ?: throw IllegalStateException( diff --git a/src/test/kotlin/com/terraformation/backend/documentproducer/db/VariableValueStoreTest.kt b/src/test/kotlin/com/terraformation/backend/documentproducer/db/VariableValueStoreTest.kt index 68e6fdedc14..5e64596ccdd 100644 --- a/src/test/kotlin/com/terraformation/backend/documentproducer/db/VariableValueStoreTest.kt +++ b/src/test/kotlin/com/terraformation/backend/documentproducer/db/VariableValueStoreTest.kt @@ -349,6 +349,26 @@ class VariableValueStoreTest : DatabaseTest(), RunsAsUser { "Values after append, append, delete, append, delete, append") } + @Test + fun `deleting an already-deleted value does nothing`() { + val variableId = insertTextVariable(insertVariable(type = VariableType.Text)) + + val append1Result = + store.updateValues( + listOf(AppendValueOperation(NewTextValue(newValueProps(variableId), "1")))) + + store.updateValues( + listOf(DeleteValueOperation(inserted.projectId, append1Result.first().id))) + val round1Values = store.listValues(inserted.documentId) + + store.updateValues( + listOf(DeleteValueOperation(inserted.projectId, append1Result.first().id))) + val round2Values = store.listValues(inserted.documentId) + + assertEquals( + round1Values, round2Values, "Should not have made any additional changes to values") + } + @Test fun `can delete a table row whose values are all deleted`() { val tableVariableId = insertTableVariable()