Skip to content

Commit

Permalink
Refactor commodity repository and update database schema (#158)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
pveeckhout authored Mar 25, 2024
1 parent eb1954b commit 6177548
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 44 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MybatisCommodityEntity> findAll();

@Select("SELECT * FROM commodity WHERE id = #{id}")
@ResultMap("commodityResultMap")
Optional<MybatisCommodityEntity> findById(@Param("id") UUID id);

@Select("SELECT * FROM commodity WHERE name = #{name}")
@ResultMap("commodityResultMap")
Optional<MybatisCommodityEntity> 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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

<changeSet id="add_commodity_combined_columns" author="pveeckhout">
<renameColumn tableName="commodity"
oldColumnName="name"
newColumnName="technical_name"
columnDataType="varchar(255)"/>

<addColumn tableName="commodity">
<column name="display_name" type="varchar(255)"/>
<column name="type" type="varchar(255)"/>
<column name="is_rare" type="boolean"/>
</addColumn>

<createIndex indexName="idx_commodity_is_rare" tableName="commodity">
<column name="is_rare"/>
</createIndex>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

<changeSet id="create_validated_commodity_view" author="daniel-j-mason" runAlways="true">
<changeSet id="create_validated_commodity_view" author="daniel-j-mason" runOnChange="true" runAlways="true">
<createView viewName="validated_commodity_view" replaceIfExists="true">
<![CDATA[
SELECT c.id,
cw.technical_name,
cw.display_name,
cw.type,
cw.is_rare
FROM commodity_whitelist cw
INNER JOIN commodity c ON c.name = cw.technical_name
c.technical_name,
c.display_name,
c.type,
c.is_rare
FROM commodity c
WHERE is_rare IS NOT NULL
ORDER BY technical_name;
]]>
</createView>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

<changeSet id="create_commodity_combined_table" author="pveeckhout">
<sql>
<![CDATA[
UPDATE commodity c
SET display_name = (SELECT cw.display_name
FROM commodity_whitelist cw
WHERE c.technical_name = cw.technical_name),
type = (SELECT cw.type FROM commodity_whitelist cw WHERE c.technical_name = cw.technical_name),
is_rare = (SELECT cw.is_rare
FROM commodity_whitelist cw
WHERE c.technical_name = cw.technical_name)
WHERE EXISTS (SELECT 1 FROM commodity_whitelist cw WHERE c.technical_name = cw.technical_name)
]]>
</sql>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
<include file="/db/trademodule/changelog/add_unique_index_commodity_name.xml"/>
<include file="/db/trademodule/changelog/add_latest_market_datum_demand_supply_price_indexes.xml"/>
<include file="/db/trademodule/changelog/add_station_arrival_distance_index.xml"/>
<include file="/db/trademodule/changelog/add_commodity_combined_columns.xml"/>
<include file="/db/trademodule/changelog/fill_commodity_combined_table.xml"/>

<!-- views always last as they might depend on new columns -->
<include file="/db/trademodule/changelog/create_validated_commodity_view.xml"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public void shouldReceiveMessage() {
new CommodityMessage.V3.Payload(
"system",
"station",
null,
null,
123456L,
true,
true,
Expand Down

0 comments on commit 6177548

Please sign in to comment.