From 61775480b9a87c0451dc8665358eae8d03869e4b Mon Sep 17 00:00:00 2001 From: Pieter Van Eeckhout Date: Mon, 25 Mar 2024 01:42:06 +0100 Subject: [PATCH] Refactor commodity repository and update database schema (#158) * Refactor commodity repository and update database schema Updated MybatisCommodityRepository, replacing several methods with a createOrUpdateOnConflict method and added new pertinent fields to the database table "commodity". This includes adding and renaming columns, updating the "validated_commodity_view" and adding an index for the "is_rare" column. In "build.gradle", the backendMessageProcessorLibVersion has been updated. * Add null values in ReceiveCommodityMessageUseCaseTest In the 'ReceiveCommodityMessageUseCaseTest.java', two new null parameters were added to the 'CommodityMessage.V3.Payload()' invocation. This aligns with recent schema updates to the Commodity Message functionality. * #157 Update validated_commodity_view settings The settings of the validated_commodity_view have been updated to run on change, in addition to running always. The 'create_validated_commodity_view' changeset in 'create_validated_commodity_view.xml' now includes 'runOnChange' making the database changes more responsive to modifications. --- build.gradle | 2 +- .../MybatisCommodityRepository.java | 57 +++++++------------ .../add_commodity_combined_columns.xml | 24 ++++++++ .../create_validated_commodity_view.xml | 14 ++--- .../fill_commodity_combined_table.xml | 23 ++++++++ .../changelog/trademodule-changelog.xml | 2 + .../ReceiveCommodityMessageUseCaseTest.java | 2 + 7 files changed, 80 insertions(+), 44 deletions(-) create mode 100644 trade-module/src/main/resources/db/trademodule/changelog/add_commodity_combined_columns.xml create mode 100644 trade-module/src/main/resources/db/trademodule/changelog/fill_commodity_combined_table.xml diff --git a/build.gradle b/build.gradle index cfcdf5b3..e763a3b1 100644 --- a/build.gradle +++ b/build.gradle @@ -103,7 +103,7 @@ subprojects { prometheusVersion = '1.11.1' backendUtilVersion = '0.2.0-SNAPSHOT' backendMybatisUtilVersion = '0.0.4-SNAPSHOT' - backendMessageProcessorLibVersion = '1.0.0-SNAPSHOT' + backendMessageProcessorLibVersion = '1.0.2-SNAPSHOT' junitJupiterVersion = "5.9.3" mockitoVersion = "5.4.0" mockitoInlineVersion = "3.4.0" diff --git a/trade-module/src/main/java/io/edpn/backend/trade/adapter/persistence/repository/MybatisCommodityRepository.java b/trade-module/src/main/java/io/edpn/backend/trade/adapter/persistence/repository/MybatisCommodityRepository.java index 680ed332..b90d0019 100644 --- a/trade-module/src/main/java/io/edpn/backend/trade/adapter/persistence/repository/MybatisCommodityRepository.java +++ b/trade-module/src/main/java/io/edpn/backend/trade/adapter/persistence/repository/MybatisCommodityRepository.java @@ -1,45 +1,30 @@ package io.edpn.backend.trade.adapter.persistence.repository; -import io.edpn.backend.mybatisutil.UuidTypeHandler; import io.edpn.backend.trade.adapter.persistence.entity.MybatisCommodityEntity; -import org.apache.ibatis.annotations.Insert; -import org.apache.ibatis.annotations.Param; -import org.apache.ibatis.annotations.Result; -import org.apache.ibatis.annotations.ResultMap; -import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; -import java.util.List; -import java.util.Optional; -import java.util.UUID; - public interface MybatisCommodityRepository { - @Select("SELECT * FROM commodity") - @Results(id = "commodityResultMap", value = { - @Result(property = "id", column = "id", javaType = UUID.class, typeHandler = UuidTypeHandler.class), - @Result(property = "name", column = "name", javaType = String.class) - }) - List findAll(); - - @Select("SELECT * FROM commodity WHERE id = #{id}") - @ResultMap("commodityResultMap") - Optional findById(@Param("id") UUID id); - - @Select("SELECT * FROM commodity WHERE name = #{name}") - @ResultMap("commodityResultMap") - Optional findByName(@Param("name") String name); - - @Insert("INSERT INTO commodity (id, name) VALUES (#{id}, #{name})") - void insert(MybatisCommodityEntity commodity); - - @Select({ - "INSERT INTO commodity (id, name)", - "VALUES (#{id}, #{name})", - "ON CONFLICT (name)", - "DO UPDATE SET", - "name = COALESCE(commodity.name, EXCLUDED.name)", - "RETURNING *" - }) + @Select(""" + WITH enriched_data AS ( + SELECT v.id AS id, v.name AS technical_name, cw.display_name, cw.type, cw.is_rare + FROM (VALUES (#{id}, #{name})) AS v(id, name) + LEFT JOIN commodity_whitelist cw + ON v.name = cw.technical_name) + INSERT + INTO commodity (id, technical_name, display_name, type, is_rare) + SELECT id, + technical_name, + display_name, + type, + is_rare + FROM enriched_data + ON CONFLICT (technical_name) + DO UPDATE SET technical_name = COALESCE(commodity.technical_name, EXCLUDED.technical_name), + display_name = EXCLUDED.display_name, + type = EXCLUDED.type, + is_rare = EXCLUDED.is_rare + RETURNING *; + """) MybatisCommodityEntity createOrUpdateOnConflict(MybatisCommodityEntity commodity); } diff --git a/trade-module/src/main/resources/db/trademodule/changelog/add_commodity_combined_columns.xml b/trade-module/src/main/resources/db/trademodule/changelog/add_commodity_combined_columns.xml new file mode 100644 index 00000000..72ea69e7 --- /dev/null +++ b/trade-module/src/main/resources/db/trademodule/changelog/add_commodity_combined_columns.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + diff --git a/trade-module/src/main/resources/db/trademodule/changelog/create_validated_commodity_view.xml b/trade-module/src/main/resources/db/trademodule/changelog/create_validated_commodity_view.xml index 7b0707be..f8c0ab4e 100644 --- a/trade-module/src/main/resources/db/trademodule/changelog/create_validated_commodity_view.xml +++ b/trade-module/src/main/resources/db/trademodule/changelog/create_validated_commodity_view.xml @@ -3,16 +3,16 @@ xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd"> - + diff --git a/trade-module/src/main/resources/db/trademodule/changelog/fill_commodity_combined_table.xml b/trade-module/src/main/resources/db/trademodule/changelog/fill_commodity_combined_table.xml new file mode 100644 index 00000000..5b52fc1a --- /dev/null +++ b/trade-module/src/main/resources/db/trademodule/changelog/fill_commodity_combined_table.xml @@ -0,0 +1,23 @@ + + + + + + + + + + diff --git a/trade-module/src/main/resources/db/trademodule/changelog/trademodule-changelog.xml b/trade-module/src/main/resources/db/trademodule/changelog/trademodule-changelog.xml index 2fbca7e4..2a8ecb69 100644 --- a/trade-module/src/main/resources/db/trademodule/changelog/trademodule-changelog.xml +++ b/trade-module/src/main/resources/db/trademodule/changelog/trademodule-changelog.xml @@ -34,6 +34,8 @@ + + diff --git a/trade-module/src/test/java/io/edpn/backend/trade/application/service/ReceiveCommodityMessageUseCaseTest.java b/trade-module/src/test/java/io/edpn/backend/trade/application/service/ReceiveCommodityMessageUseCaseTest.java index cc0a3b9c..540a5242 100644 --- a/trade-module/src/test/java/io/edpn/backend/trade/application/service/ReceiveCommodityMessageUseCaseTest.java +++ b/trade-module/src/test/java/io/edpn/backend/trade/application/service/ReceiveCommodityMessageUseCaseTest.java @@ -88,6 +88,8 @@ public void shouldReceiveMessage() { new CommodityMessage.V3.Payload( "system", "station", + null, + null, 123456L, true, true,