diff --git a/app/sepuling-java/src/it/java/smecalculus/bezmen/storage/SepulkaDaoIT.java b/app/sepuling-java/src/it/java/smecalculus/bezmen/storage/SepulkaDaoIT.java index 503e6a4f..b9fe2632 100644 --- a/app/sepuling-java/src/it/java/smecalculus/bezmen/storage/SepulkaDaoIT.java +++ b/app/sepuling-java/src/it/java/smecalculus/bezmen/storage/SepulkaDaoIT.java @@ -27,21 +27,21 @@ void shouldAddOneSepulka() { var expected2 = SepulkaStateDmEg.existence().internalId(expected1.internalId()).build(); // when - var actualSaved = sepulkaDao.add(expected1); + var actualInserted = sepulkaDao.addNew(expected1); // and var actualSelected = sepulkaDao.getBy(expected1.externalId()); // then - assertThat(actualSaved).usingRecursiveComparison().isEqualTo(expected1); + assertThat(actualInserted).usingRecursiveComparison().isEqualTo(expected1); // and assertThat(actualSelected).contains(expected2); } @Test - void shouldViewOneSepulka() { + void shouldGetOneSepulka() { // given var aggregate = SepulkaStateDmEg.aggregateRoot().build(); // and - sepulkaDao.add(aggregate); + sepulkaDao.addNew(aggregate); // and var expected = SepulkaStateDmEg.viewing(aggregate).build(); // when @@ -51,17 +51,17 @@ void shouldViewOneSepulka() { } @Test - void shouldUpdateOneSepulka() { + void shouldTouchOneSepulka() { // given var aggregate = SepulkaStateDmEg.aggregateRoot().build(); // and - sepulkaDao.add(aggregate); + sepulkaDao.addNew(aggregate); // and var updatedAt = aggregate.updatedAt().plusSeconds(1); // and var touch = SepulkaStateDmEg.touch(aggregate).updatedAt(updatedAt).build(); // when - sepulkaDao.updateBy(aggregate.internalId(), touch); + sepulkaDao.touchBy(aggregate.internalId(), touch); // then // no exception } diff --git a/app/sepuling-java/src/main/java/smecalculus/bezmen/core/SepulkaServiceImpl.java b/app/sepuling-java/src/main/java/smecalculus/bezmen/core/SepulkaServiceImpl.java index 1941b7bf..4c86d53f 100644 --- a/app/sepuling-java/src/main/java/smecalculus/bezmen/core/SepulkaServiceImpl.java +++ b/app/sepuling-java/src/main/java/smecalculus/bezmen/core/SepulkaServiceImpl.java @@ -35,7 +35,7 @@ public RegistrationResponse register(RegistrationRequest request) { .createdAt(now) .updatedAt(now) .build(); - var sepulkaSaved = dao.add(sepulkaCreated); + var sepulkaSaved = dao.addNew(sepulkaCreated); return converter.toMessage(sepulkaSaved); } diff --git a/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/SepulkaDao.java b/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/SepulkaDao.java index b9084910..e08a8c2c 100644 --- a/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/SepulkaDao.java +++ b/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/SepulkaDao.java @@ -8,11 +8,11 @@ * Port: server side */ public interface SepulkaDao { - SepulkaStateDm.AggregateRoot add(SepulkaStateDm.AggregateRoot state); + SepulkaStateDm.AggregateRoot addNew(SepulkaStateDm.AggregateRoot state); Optional getBy(String externalId); Optional getBy(UUID internalId); - void updateBy(UUID internalId, SepulkaStateDm.Touch state); + void touchBy(UUID internalId, SepulkaStateDm.Touch state); } diff --git a/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/SepulkaDaoMyBatis.java b/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/SepulkaDaoMyBatis.java index db08c557..8dcd6de1 100644 --- a/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/SepulkaDaoMyBatis.java +++ b/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/SepulkaDaoMyBatis.java @@ -17,7 +17,7 @@ public class SepulkaDaoMyBatis implements SepulkaDao { private SepulkaSqlMapper sqlMapper; @Override - public SepulkaStateDm.AggregateRoot add(@NonNull SepulkaStateDm.AggregateRoot state) { + public SepulkaStateDm.AggregateRoot addNew(@NonNull SepulkaStateDm.AggregateRoot state) { var stateEdge = stateMapper.toEdge(state); sqlMapper.insert(stateEdge); return state; @@ -30,13 +30,13 @@ public Optional getBy(@NonNull String externalId) { @Override public Optional getBy(@NonNull UUID internalId) { - return sqlMapper.findByInternalId(internalId.toString()).map(stateMapper::toDomain); + return sqlMapper.findByInternalId(internalId).map(stateMapper::toDomain); } @Override - public void updateBy(UUID internalId, SepulkaStateDm.Touch state) { + public void touchBy(@NonNull UUID internalId, @NonNull SepulkaStateDm.Touch state) { var stateEdge = stateMapper.toEdge(state); - var matchedCount = sqlMapper.updateBy(stateEdge, internalId.toString()); + var matchedCount = sqlMapper.updateBy(internalId, stateEdge); if (matchedCount == 0) { throw new ContentionException(); } diff --git a/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/SepulkaDaoSpringData.java b/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/SepulkaDaoSpringData.java index 66c9d0f6..6519b2c9 100644 --- a/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/SepulkaDaoSpringData.java +++ b/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/SepulkaDaoSpringData.java @@ -17,9 +17,10 @@ public class SepulkaDaoSpringData implements SepulkaDao { private SepulkaRepository repository; @Override - public SepulkaStateDm.AggregateRoot add(@NonNull SepulkaStateDm.AggregateRoot state) { - var stateEdge = repository.save(mapper.toEdge(state)); - return mapper.toDomain(stateEdge); + public SepulkaStateDm.AggregateRoot addNew(@NonNull SepulkaStateDm.AggregateRoot state) { + var stateEdge = mapper.toEdge(state); + repository.insert(stateEdge); + return state; } @Override @@ -33,7 +34,7 @@ public Optional getBy(@NonNull UUID internalId) { } @Override - public void updateBy(UUID internalId, SepulkaStateDm.Touch state) { + public void touchBy(@NonNull UUID internalId, @NonNull SepulkaStateDm.Touch state) { var stateEdge = mapper.toEdge(state); var matchedCount = repository.updateBy(internalId, stateEdge); if (matchedCount == 0) { diff --git a/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/mybatis/SepulkaSqlMapper.java b/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/mybatis/SepulkaSqlMapper.java index 2c09340f..bb85eae1 100644 --- a/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/mybatis/SepulkaSqlMapper.java +++ b/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/mybatis/SepulkaSqlMapper.java @@ -1,6 +1,7 @@ package smecalculus.bezmen.storage.mybatis; import java.util.Optional; +import java.util.UUID; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; @@ -36,7 +37,7 @@ INSERT INTO sepulkas ( SELECT internal_id as internalId FROM sepulkas - WHERE external_id = #{id, jdbcType=VARCHAR} + WHERE external_id = #{id} """) Optional findByExternalId(@Param("id") String id); @@ -45,17 +46,17 @@ INSERT INTO sepulkas ( SELECT external_id as externalId FROM sepulkas - WHERE internal_id = #{id, jdbcType=OTHER}::uuid + WHERE internal_id = #{id} """) - Optional findByInternalId(@Param("id") String id); + Optional findByInternalId(@Param("id") UUID id); @Update( """ UPDATE sepulkas SET revision = revision + 1, updated_at = #{state.updatedAt} - WHERE internal_id = #{id, jdbcType=OTHER}::uuid + WHERE internal_id = #{id} AND revision = #{state.revision} """) - int updateBy(@Param("state") Touch state, @Param("id") String id); + int updateBy(@Param("id") UUID internalId, @Param("state") Touch state); } diff --git a/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/springdata/SepulkaRepository.java b/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/springdata/SepulkaRepository.java index a2e13743..6226faa9 100644 --- a/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/springdata/SepulkaRepository.java +++ b/app/sepuling-java/src/main/java/smecalculus/bezmen/storage/springdata/SepulkaRepository.java @@ -11,9 +11,29 @@ public interface SepulkaRepository extends CrudRepository { - Optional findByExternalId(@NonNull String externalId); + @Modifying + @Query( + """ + INSERT INTO sepulkas ( + internal_id, + external_id, + revision, + created_at, + updated_at + ) + VALUES ( + :#{#state.internalId}, + :#{#state.externalId}, + :#{#state.revision}, + :#{#state.createdAt}, + :#{#state.updatedAt} + ) + """) + void insert(@Param("state") SepulkaStateEm.AggregateRoot state); + + Optional findByExternalId(@NonNull String id); - Optional findByInternalId(@NonNull UUID internalId); + Optional findByInternalId(@NonNull UUID id); @Modifying @Query( diff --git a/app/sepuling-kotlin/src/it/kotlin/smecalculus/bezmen/storage/SepulkaDaoIT.kt b/app/sepuling-kotlin/src/it/kotlin/smecalculus/bezmen/storage/SepulkaDaoIT.kt index 789f70f3..8238e875 100644 --- a/app/sepuling-kotlin/src/it/kotlin/smecalculus/bezmen/storage/SepulkaDaoIT.kt +++ b/app/sepuling-kotlin/src/it/kotlin/smecalculus/bezmen/storage/SepulkaDaoIT.kt @@ -24,21 +24,21 @@ abstract class SepulkaDaoIT { // and val expected2 = SepulkaStateDmEg.existence().internalId(expected1.internalId).build() // when - val actualSaved = sepulkaDao.add(expected1) + val actualInserted = sepulkaDao.addNew(expected1) // and val actualSelected = sepulkaDao.getBy(expected1.externalId) // then - assertThat(actualSaved).usingRecursiveComparison().isEqualTo(expected1) + assertThat(actualInserted).usingRecursiveComparison().isEqualTo(expected1) // and assertThat(actualSelected).usingRecursiveComparison().isEqualTo(expected2) } @Test - fun shouldViewOneSepulka() { + fun shouldGetOneSepulka() { // given val aggregate = SepulkaStateDmEg.aggregateRoot().build() // and - sepulkaDao.add(aggregate) + sepulkaDao.addNew(aggregate) // and val expected = SepulkaStateDmEg.viewing(aggregate).build() // when @@ -48,11 +48,11 @@ abstract class SepulkaDaoIT { } @Test - fun shouldUpdateOneSepulka() { + fun shouldTouchOneSepulka() { // given val aggregate = SepulkaStateDmEg.aggregateRoot().build() // and - sepulkaDao.add(aggregate) + sepulkaDao.addNew(aggregate) // and val updatedAt = aggregate.updatedAt.plusSeconds(1) // and diff --git a/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/core/SepulkaServiceImpl.kt b/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/core/SepulkaServiceImpl.kt index c2843465..6ae3f65a 100644 --- a/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/core/SepulkaServiceImpl.kt +++ b/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/core/SepulkaServiceImpl.kt @@ -21,7 +21,7 @@ class SepulkaServiceImpl( .createdAt(now) .updatedAt(now) .build() - val sepulkaSaved = dao.add(sepulkaCreated) + val sepulkaSaved = dao.addNew(sepulkaCreated) return converter.toMessage(sepulkaSaved) } diff --git a/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/SepulkaDao.kt b/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/SepulkaDao.kt index 5ef46af9..8699a564 100644 --- a/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/SepulkaDao.kt +++ b/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/SepulkaDao.kt @@ -7,7 +7,7 @@ import java.util.UUID * Port: server side */ interface SepulkaDao { - fun add(state: SepulkaStateDm.AggregateRoot): SepulkaStateDm.AggregateRoot + fun addNew(state: SepulkaStateDm.AggregateRoot): SepulkaStateDm.AggregateRoot fun getBy(externalId: String): SepulkaStateDm.Existence? diff --git a/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/SepulkaDaoMyBatis.kt b/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/SepulkaDaoMyBatis.kt index 43780e91..8b6668a9 100644 --- a/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/SepulkaDaoMyBatis.kt +++ b/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/SepulkaDaoMyBatis.kt @@ -8,7 +8,7 @@ class SepulkaDaoMyBatis( private val stateMapper: SepulkaStateMapper, private val sqlMapper: SepulkaSqlMapper, ) : SepulkaDao { - override fun add(state: SepulkaStateDm.AggregateRoot): SepulkaStateDm.AggregateRoot { + override fun addNew(state: SepulkaStateDm.AggregateRoot): SepulkaStateDm.AggregateRoot { val stateEdge = stateMapper.toEdge(state) sqlMapper.insert(stateEdge) return state @@ -19,7 +19,7 @@ class SepulkaDaoMyBatis( } override fun getBy(internalId: UUID): SepulkaStateDm.Viewing? { - return sqlMapper.findByInternalId(internalId.toString())?.let { stateMapper.toDomain(it) } + return sqlMapper.findByInternalId(internalId)?.let { stateMapper.toDomain(it) } } override fun touchBy( @@ -27,7 +27,7 @@ class SepulkaDaoMyBatis( state: SepulkaStateDm.Touch, ) { val stateEdge = stateMapper.toEdge(state) - val matchedCount = sqlMapper.updateBy(stateEdge, internalId.toString()) + val matchedCount = sqlMapper.updateBy(internalId, stateEdge) if (matchedCount == 0) { throw ContentionException() } diff --git a/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/SepulkaDaoSpringData.kt b/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/SepulkaDaoSpringData.kt index cbf222fc..dc4be765 100644 --- a/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/SepulkaDaoSpringData.kt +++ b/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/SepulkaDaoSpringData.kt @@ -8,9 +8,10 @@ class SepulkaDaoSpringData( private val mapper: SepulkaStateMapper, private val repository: SepulkaRepository, ) : SepulkaDao { - override fun add(state: SepulkaStateDm.AggregateRoot): SepulkaStateDm.AggregateRoot { - val stateEdge = repository.save(mapper.toEdge(state)) - return mapper.toDomain(stateEdge) + override fun addNew(state: SepulkaStateDm.AggregateRoot): SepulkaStateDm.AggregateRoot { + val stateEdge = mapper.toEdge(state) + repository.insert(stateEdge) + return state } override fun getBy(externalId: String): SepulkaStateDm.Existence? { diff --git a/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/mybatis/SepulkaSqlMapper.kt b/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/mybatis/SepulkaSqlMapper.kt index 010c019f..872fda6b 100644 --- a/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/mybatis/SepulkaSqlMapper.kt +++ b/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/mybatis/SepulkaSqlMapper.kt @@ -5,6 +5,7 @@ import org.apache.ibatis.annotations.Param import org.apache.ibatis.annotations.Select import org.apache.ibatis.annotations.Update import smecalculus.bezmen.storage.SepulkaStateEm +import java.util.UUID interface SepulkaSqlMapper { @Insert( @@ -32,7 +33,7 @@ interface SepulkaSqlMapper { SELECT internal_id as internalId FROM sepulkas - WHERE external_id = #{id, jdbcType=VARCHAR} + WHERE external_id = #{id} """, ) fun findByExternalId( @@ -44,11 +45,11 @@ interface SepulkaSqlMapper { SELECT external_id as externalId FROM sepulkas - WHERE internal_id = #{id, jdbcType=OTHER}::uuid + WHERE internal_id = #{id} """, ) fun findByInternalId( - @Param("id") id: String, + @Param("id") id: UUID, ): SepulkaStateEm.Viewing? @Update( @@ -56,12 +57,12 @@ interface SepulkaSqlMapper { UPDATE sepulkas SET revision = revision + 1, updated_at = #{state.updatedAt} - WHERE internal_id = #{id, jdbcType=OTHER}::uuid + WHERE internal_id = #{id} AND revision = #{state.revision} """, ) fun updateBy( + @Param("id") internalId: UUID, @Param("state") state: SepulkaStateEm.Touch, - @Param("id") id: String, ): Int } diff --git a/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/springdata/SepulkaRepository.kt b/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/springdata/SepulkaRepository.kt index 6803e59a..971f2701 100644 --- a/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/springdata/SepulkaRepository.kt +++ b/app/sepuling-kotlin/src/main/kotlin/smecalculus/bezmen/storage/springdata/SepulkaRepository.kt @@ -8,9 +8,32 @@ import smecalculus.bezmen.storage.SepulkaStateEm import java.util.UUID interface SepulkaRepository : CrudRepository { - fun findByExternalId(externalId: String): SepulkaStateEm.Existence? + @Modifying + @Query( + """ + INSERT INTO sepulkas ( + internal_id, + external_id, + revision, + created_at, + updated_at + ) + VALUES ( + :#{#state.internalId}, + :#{#state.externalId}, + :#{#state.revision}, + :#{#state.createdAt}, + :#{#state.updatedAt} + ) + """, + ) + fun insert( + @Param("state") state: SepulkaStateEm.AggregateRoot, + ) + + fun findByExternalId(id: String): SepulkaStateEm.Existence? - fun findByInternalId(internalId: UUID): SepulkaStateEm.Viewing? + fun findByInternalId(id: UUID): SepulkaStateEm.Viewing? @Modifying @Query( diff --git a/lib/storage/src/main/java/smecalculus/bezmen/storage/SepulkaStateEm.java b/lib/storage/src/main/java/smecalculus/bezmen/storage/SepulkaStateEm.java index 1a2c55d0..8c4f1669 100644 --- a/lib/storage/src/main/java/smecalculus/bezmen/storage/SepulkaStateEm.java +++ b/lib/storage/src/main/java/smecalculus/bezmen/storage/SepulkaStateEm.java @@ -4,7 +4,6 @@ import java.util.UUID; import lombok.Data; import org.springframework.data.annotation.Id; -import org.springframework.data.domain.Persistable; import org.springframework.data.relational.core.mapping.Column; import org.springframework.data.relational.core.mapping.Table; @@ -27,7 +26,7 @@ public static class Touch { @Data @Table("sepulkas") - public static class AggregateRoot implements Persistable { + public static class AggregateRoot { @Id UUID internalId; @@ -42,15 +41,5 @@ public static class AggregateRoot implements Persistable { @Column LocalDateTime updatedAt; - - @Override - public UUID getId() { - return internalId; - } - - @Override - public boolean isNew() { - return true; - } } } diff --git a/lib/storage/src/main/java/smecalculus/bezmen/storage/mybatis/UuidTypeHandler.java b/lib/storage/src/main/java/smecalculus/bezmen/storage/mybatis/UuidTypeHandler.java index 7df9f3ec..1c9afe24 100644 --- a/lib/storage/src/main/java/smecalculus/bezmen/storage/mybatis/UuidTypeHandler.java +++ b/lib/storage/src/main/java/smecalculus/bezmen/storage/mybatis/UuidTypeHandler.java @@ -4,7 +4,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Types; import java.util.UUID; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; @@ -19,24 +18,21 @@ public class UuidTypeHandler extends BaseTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, UUID parameter, JdbcType jdbcType) throws SQLException { - ps.setObject(i, parameter.toString(), Types.OTHER); + ps.setObject(i, parameter); } @Override public UUID getNullableResult(ResultSet rs, String columnName) throws SQLException { - var uuidString = rs.getString(columnName); - return uuidString == null ? null : UUID.fromString(uuidString); + return rs.getObject(columnName, UUID.class); } @Override public UUID getNullableResult(ResultSet rs, int columnIndex) throws SQLException { - var uuidString = rs.getString(columnIndex); - return uuidString == null ? null : UUID.fromString(uuidString); + return rs.getObject(columnIndex, UUID.class); } @Override public UUID getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { - var uuidString = cs.getString(columnIndex); - return uuidString == null ? null : UUID.fromString(uuidString); + return cs.getObject(columnIndex, UUID.class); } } diff --git a/stack/product/ility/programmability/compose.yaml.jinja b/stack/product/ility/programmability/compose.yaml.jinja index 651e90cf..c85ba66c 100644 --- a/stack/product/ility/programmability/compose.yaml.jinja +++ b/stack/product/ility/programmability/compose.yaml.jinja @@ -53,7 +53,6 @@ services: --default-schema-name=public --username={{ storage.dba.username }} --password={{ storage.dba.password }} - --labels=database,user update -Ddatabase.name={{ database.name }} -Downer.username={{ storage.owner.username }} diff --git a/stack/product/ility/resilience/compose.yaml.jinja b/stack/product/ility/resilience/compose.yaml.jinja index b460786f..9191c72b 100644 --- a/stack/product/ility/resilience/compose.yaml.jinja +++ b/stack/product/ility/resilience/compose.yaml.jinja @@ -135,7 +135,6 @@ services: --default-schema-name=public --username={{ storage.dba.username }} --password={{ storage.dba.password }} - --labels=database,user update -Ddatabase.name={{ database.name }} -Downer.username={{ storage.owner.username }}