Skip to content

Commit

Permalink
Merge d4e0bc1 into 3f2b1cd
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmckenzie committed Jul 15, 2020
2 parents 3f2b1cd + d4e0bc1 commit a281b24
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
[Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- indexes added to stream_id and position_in_stream in event_log table
### Changed
- DefaultEventStoreDataSourceProvider changed to a singleton,
so the caching of the DataSource works properly
- Test util class DatabaseCleaner has an additional method
'cleanEventStoreTables(...)' for truncating specified tables
in the event-store

## [7.0.5] - 2020-07-08
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import uk.gov.justice.subscription.domain.eventsource.EventSourceDefinition;
import uk.gov.justice.subscription.registry.EventSourceDefinitionRegistry;

import javax.enterprise.context.ApplicationScoped;
import javax.ejb.Singleton;
import javax.inject.Inject;
import javax.sql.DataSource;

@ApplicationScoped
@Singleton
public class DefaultEventStoreDataSourceProvider implements EventStoreDataSourceProvider {

private DataSource dataSource;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.3.xsd">

<changeSet id="event-store-023" author="TechPod"
logicalFilePath="023-add-indexes-on-event-log-for-stream-id-and-position-in-stream.changelog.xml">

<createIndex indexName="event_log_stream_id_idx"
tableName="event_log">
<column name="stream_id" type="uuid"/>
</createIndex>
<createIndex indexName="event_log_position_in_stream_idx"
tableName="event_log">
<column name="position_in_stream" type="bigint"/>
</createIndex>

<rollback>
<dropIndex indexName="event_log_stream_id_idx" tableName="event_log"/>
<dropIndex indexName="event_log_position_in_stream_idx" tableName="event_log"/>
</rollback>

</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ public void cleanEventStoreTables(final String contextName) {
}
}

/**
* Deletes all the data from the specified Event-Store tables
*
* @param contextName the name of the context to clean the tables from
*/
public void cleanEventStoreTables(final String contextName, final String tableName, final String... additionalTableNames) {
try (final Connection connection = testJdbcConnectionProvider.getEventStoreConnection(contextName)) {

cleanTable(tableName, connection);

for(String additionalTable: additionalTableNames) {
cleanTable(additionalTable, connection);
}

} catch (SQLException e) {
throw new DataAccessException("Failed to commit or close database connection", e);
}
}

/**
* Deprecated from 3.2.0, please use {@link #cleanEventStoreTables(String)} to clean all tables
* belonging to the event-store.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,30 @@ public void shouldThrowADatAccessExceptionIfClosingTheEventStoreConnectionFails(

verify(connection).close();
}

@Test
public void shouldCleanSomeEventStoreTables() throws Exception {

final String table_1 = "table_1";
final String table_2 = "table_2";

final String contextName = "my-context";

final Connection connection = mock(Connection.class);
final PreparedStatement preparedStatement_1 = mock(PreparedStatement.class);
final PreparedStatement preparedStatement_2 = mock(PreparedStatement.class);

when(testJdbcConnectionProvider.getEventStoreConnection(contextName)).thenReturn(connection);
when(connection.prepareStatement(format(SQL_PATTERN, table_1))).thenReturn(preparedStatement_1);
when(connection.prepareStatement(format(SQL_PATTERN, table_2))).thenReturn(preparedStatement_2);

databaseCleaner.cleanEventStoreTables(contextName, table_1, table_2);

verify(preparedStatement_1).executeUpdate();
verify(preparedStatement_2).executeUpdate();

verify(connection).close();
verify(preparedStatement_1).close();
verify(preparedStatement_2).close();
}
}

0 comments on commit a281b24

Please sign in to comment.