-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
eb1954b
commit 6177548
Showing
7 changed files
with
80 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 21 additions & 36 deletions
57
...java/io/edpn/backend/trade/adapter/persistence/repository/MybatisCommodityRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
24 changes: 24 additions & 0 deletions
24
trade-module/src/main/resources/db/trademodule/changelog/add_commodity_combined_columns.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
trade-module/src/main/resources/db/trademodule/changelog/fill_commodity_combined_table.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters