Skip to content

Commit

Permalink
Add DeployableSystem model and Dao
Browse files Browse the repository at this point in the history
Future updates will hook this into all the other models

Relates to #140
  • Loading branch information
chrisrohr committed Feb 25, 2023
1 parent ac02a13 commit 9da2ed6
Show file tree
Hide file tree
Showing 8 changed files with 375 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.kiwiproject.champagne.dao;

import org.jdbi.v3.sqlobject.config.RegisterRowMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.BindBean;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import org.kiwiproject.champagne.dao.mappers.DeployableSystemForUserMapper;
import org.kiwiproject.champagne.dao.mappers.DeployableSystemMapper;
import org.kiwiproject.champagne.model.DeployableSystem;

import java.util.List;

public interface DeployableSystemDao {

@SqlUpdate("insert into deployable_systems (name) values (:name)")
@GetGeneratedKeys
long insertDeployableSystem(@BindBean DeployableSystem system);

@SqlQuery("select s.*, us.system_admin from deployable_systems s join users_deployable_systems us on s.id = us.deployable_system_id where us.user_id = :userId")
@RegisterRowMapper(DeployableSystemForUserMapper.class)
List<DeployableSystem> findDeployableSystemsForUser(@Bind("userId") long userId);

@SqlQuery("select * from deployable_systems order by created_at desc offset :offset limit :limit")
@RegisterRowMapper(DeployableSystemMapper.class)
List<DeployableSystem> findPagedDeployableSystems(@Bind("offset") int offset, @Bind("limit") int limit);

@SqlQuery("select count(*) from deployable_systems")
long countDeployableSystems();

@SqlUpdate("update deployable_systems set dev_environment_id = :envId where id = :id")
int updateDevEnvironment(@Bind("id") long id, @Bind("envId") long envId);

@SqlUpdate("update deployable_systems set environment_promotion_order = :envCsv where id = :id")
int updateEnvironmentPromotionOrder(@Bind("id") long id, @Bind("envCsv") String envCsv);

@SqlUpdate("delete from deployable_systems where id = :id")
int deleteById(@Bind("id") long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.kiwiproject.champagne.dao.mappers;

import org.jdbi.v3.core.statement.StatementContext;
import org.kiwiproject.champagne.model.DeployableSystem;

import java.sql.ResultSet;
import java.sql.SQLException;

public class DeployableSystemForUserMapper extends DeployableSystemMapper {
@Override
public DeployableSystem map(ResultSet rs, StatementContext ctx) throws SQLException {
var deployableSystem = super.map(rs, ctx);

return deployableSystem.withAdmin(rs.getBoolean("system_admin"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.kiwiproject.champagne.dao.mappers;

import static org.kiwiproject.jdbc.KiwiJdbc.instantFromTimestamp;
import static org.kiwiproject.jdbc.KiwiJdbc.longValueOrNull;

import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.kiwiproject.champagne.model.DeployableSystem;

import java.sql.ResultSet;
import java.sql.SQLException;

public class DeployableSystemMapper implements RowMapper<DeployableSystem> {
@Override
public DeployableSystem map(ResultSet rs, StatementContext ctx) throws SQLException {
return DeployableSystem.builder()
.id(rs.getLong("id"))
.createdAt(instantFromTimestamp(rs, "created_at"))
.updatedAt(instantFromTimestamp(rs, "updated_at"))
.name(rs.getString("name"))
.devEnvironmentId(longValueOrNull(rs, "dev_environment_id"))
.environmentPromotionOrder(rs.getString("environment_promotion_order"))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.kiwiproject.champagne.model;

import lombok.Builder;
import lombok.Value;
import lombok.With;

import java.time.Instant;

@Builder
@Value
public class DeployableSystem {

Long id;
Instant createdAt;
Instant updatedAt;

String name;

Long devEnvironmentId;
String environmentPromotionOrder;

/**
* This is a transitive property populated through the users_system table and indicates if a specific user is
* an admin in this system. Listing all systems will NOT populate this field.
*/
@With
boolean admin;
}
36 changes: 36 additions & 0 deletions service/src/main/resources/migrations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,40 @@
<column name="port" type="integer"/>
</createTable>
</changeSet>

<changeSet id="add system table" author="crohr">
<createTable tableName="deployable_systems">
<column name="id" type="bigint" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="created_at" type="timestamp without time zone" defaultValueComputed="current_timestamp">
<constraints nullable="false"/>
</column>
<column name="updated_at" type="timestamp without time zone" defaultValueComputed="current_timestamp">
<constraints nullable="false"/>
</column>
<column name="name" type="text">
<constraints nullable="false"/>
</column>
<column name="dev_environment_id" type="bigint">
<constraints nullable="true" foreignKeyName="fk_system_deployment_environment" references="deployment_environments(id)" deleteCascade="true"/>
</column>
<column name="environment_promotion_order" type="text"/>
</createTable>

<createTable tableName="users_deployable_systems">
<column name="id" type="bigint" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="user_id" type="bigint">
<constraints nullable="false" foreignKeyName="fk_user_system_user" references="users(id)" deleteCascade="true"/>
</column>
<column name="deployable_system_id" type="bigint">
<constraints nullable="false" foreignKeyName="fk_user_system_deployable_system" references="deployable_systems(id)" deleteCascade="true"/>
</column>
<column name="system_admin" type="boolean" defaultValueBoolean="false">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
import static org.kiwiproject.test.constants.KiwiTestConstants.JSON_HELPER;
import static org.kiwiproject.test.util.DateTimeTestHelper.assertTimeDifferenceWithinTolerance;

import java.sql.SQLException;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Map;

import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.spi.JdbiPlugin;
Expand All @@ -28,6 +23,10 @@
import org.kiwiproject.test.junit.jupiter.Jdbi3DaoExtension;
import org.kiwiproject.test.junit.jupiter.PostgresLiquibaseTestExtension;

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Map;

@DisplayName("BuildDao")
class BuildDaoTest {

Expand All @@ -41,7 +40,7 @@ class BuildDaoTest {
.plugin(new JdbiPlugin() {

@Override
public void customizeJdbi(Jdbi jdbi) throws SQLException {
public void customizeJdbi(Jdbi jdbi) {
jdbi.registerRowMapper(Build.class, new BuildMapper(JSON_HELPER));
}
})
Expand All @@ -60,7 +59,7 @@ void setUp() {
class InsertBuild {

@Test
void shouldInsertUserSuccessfully() {
void shouldInsertBuildSuccessfully() {
var beforeInsert = ZonedDateTime.now();

var buildToInsert = Build.builder()
Expand Down Expand Up @@ -113,7 +112,7 @@ void shouldReturnListOfBuilds(String componentIdentifierFilter, String component
}

@Test
void shouldReturnEmptyListWhenNoReleasesFound() {
void shouldReturnEmptyListWhenNoBuildsFound() {
insertBuildRecord(handle, "champagne-service", "42.0");

var builds = dao.findPagedBuilds(10, 10, null, null);
Expand All @@ -122,7 +121,7 @@ void shouldReturnEmptyListWhenNoReleasesFound() {
}

@Nested
class CountReleases {
class CountBuilds {

@ParameterizedTest
@CsvSource(nullValues = "null", value = {
Expand All @@ -131,15 +130,15 @@ class CountReleases {
"champagne-service, null",
"champagne-service, 42.0"
})
void shouldReturnCountOfReleases(String componentIdentifierFilter, String componentVersionFilter) {
void shouldReturnCountOfBuilds(String componentIdentifierFilter, String componentVersionFilter) {
insertBuildRecord(handle, "champagne-service", "42.0");

var builds = dao.countBuilds(componentIdentifierFilter, componentVersionFilter);
assertThat(builds).isOne();
}

@Test
void shouldReturnEmptyListWhenNoReleasesFound() {
void shouldReturnEmptyListWhenNoBuildsFound() {
var builds = dao.countBuilds(null, null);
assertThat(builds).isZero();
}
Expand Down
Loading

0 comments on commit 9da2ed6

Please sign in to comment.