From f009d50631b7c1c515b4cf3366c239d3cd4dc64d Mon Sep 17 00:00:00 2001 From: Darin Kelkhoff Date: Fri, 10 May 2024 12:51:20 -0500 Subject: [PATCH] CE-1180 add option to setPrimaryKeyInInsertedRecords (on the INPUT records) --- .../core/actions/tables/ReplaceAction.java | 11 ++++++ .../actions/tables/replace/ReplaceInput.java | 36 +++++++++++++++++-- .../actions/tables/ReplaceActionTest.java | 25 +++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/actions/tables/ReplaceAction.java b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/actions/tables/ReplaceAction.java index 04177cdb..ca2d1413 100644 --- a/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/actions/tables/ReplaceAction.java +++ b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/actions/tables/ReplaceAction.java @@ -157,6 +157,17 @@ public class ReplaceAction extends AbstractQActionFunction records; private QQueryFilter filter; - private boolean performDeletes = true; - private boolean allowNullKeyValuesToEqual = false; + private boolean performDeletes = true; + private boolean allowNullKeyValuesToEqual = false; + private boolean setPrimaryKeyInInsertedRecords = false; private boolean omitDmlAudit = false; @@ -271,4 +272,35 @@ public class ReplaceInput extends AbstractTableActionInput return (this); } + + + /******************************************************************************* + ** Getter for setPrimaryKeyInInsertedRecords + *******************************************************************************/ + public boolean getSetPrimaryKeyInInsertedRecords() + { + return (this.setPrimaryKeyInInsertedRecords); + } + + + + /******************************************************************************* + ** Setter for setPrimaryKeyInInsertedRecords + *******************************************************************************/ + public void setSetPrimaryKeyInInsertedRecords(boolean setPrimaryKeyInInsertedRecords) + { + this.setPrimaryKeyInInsertedRecords = setPrimaryKeyInInsertedRecords; + } + + + + /******************************************************************************* + ** Fluent setter for setPrimaryKeyInInsertedRecords + *******************************************************************************/ + public ReplaceInput withSetPrimaryKeyInInsertedRecords(boolean setPrimaryKeyInInsertedRecords) + { + this.setPrimaryKeyInInsertedRecords = setPrimaryKeyInInsertedRecords; + return (this); + } + } diff --git a/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/actions/tables/ReplaceActionTest.java b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/actions/tables/ReplaceActionTest.java index da57c703..9eb5dc57 100644 --- a/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/actions/tables/ReplaceActionTest.java +++ b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/actions/tables/ReplaceActionTest.java @@ -24,6 +24,7 @@ package com.kingsrook.qqq.backend.core.actions.tables; import java.util.List; import java.util.Map; +import java.util.Optional; import com.kingsrook.qqq.backend.core.BaseTest; import com.kingsrook.qqq.backend.core.exceptions.QException; import com.kingsrook.qqq.backend.core.model.actions.tables.count.CountInput; @@ -38,8 +39,11 @@ import com.kingsrook.qqq.backend.core.model.data.QRecord; import com.kingsrook.qqq.backend.core.model.metadata.tables.UniqueKey; import com.kingsrook.qqq.backend.core.utils.TestUtils; import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /******************************************************************************* @@ -81,12 +85,27 @@ class ReplaceActionTest extends BaseTest replaceInput.setOmitDmlAudit(true); replaceInput.setRecords(newPeople); replaceInput.setFilter(null); + replaceInput.setSetPrimaryKeyInInsertedRecords(false); ReplaceOutput replaceOutput = new ReplaceAction().execute(replaceInput); assertEquals(1, replaceOutput.getInsertOutput().getRecords().size()); assertEquals(1, replaceOutput.getUpdateOutput().getRecords().size()); assertEquals(1, replaceOutput.getDeleteOutput().getDeletedRecordCount()); + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // due to false for SetPrimaryKeyInInsertedRecords, make sure primary keys aren't on the records that got inserted // + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Optional ned = newPeople.stream().filter(r -> r.getValueString("firstName").equals("Ned")).findFirst(); + assertThat(ned).isPresent(); + assertNull(ned.get().getValue("id"), "the record that got inserted should not have its primary key set"); + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // but note, homer (who was updated) would have had its primary key set too, as part of the internal processing that does the update. // + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + Optional homer = newPeople.stream().filter(r -> r.getValueString("firstName").equals("Homer")).findFirst(); + assertThat(homer).isPresent(); + assertNotNull(homer.get().getValue("id"), "the record that got updated should have its primary key set"); + ////////////////////////////// // assert homer was updated // ////////////////////////////// @@ -136,12 +155,18 @@ class ReplaceActionTest extends BaseTest replaceInput.setOmitDmlAudit(true); replaceInput.setRecords(newPeople); replaceInput.setFilter(null); + replaceInput.setSetPrimaryKeyInInsertedRecords(true); ReplaceOutput replaceOutput = new ReplaceAction().execute(replaceInput); assertEquals(2, replaceOutput.getInsertOutput().getRecords().size()); assertEquals(0, replaceOutput.getUpdateOutput().getRecords().size()); assertEquals(2, replaceOutput.getDeleteOutput().getDeletedRecordCount()); + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // due to true for SetPrimaryKeyInInsertedRecords, make sure primary keys ARE on all the records that got inserted // + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + assertTrue(newPeople.stream().allMatch(r -> r.getValue("id") != null), "All inserted records should have their primary key"); + /////////////////////////////////////// // assert homer & marge were deleted // ///////////////////////////////////////