Skip to content

Commit

Permalink
Merge pull request #234 from CJSCommonPlatform/databasecleaner-to-tru…
Browse files Browse the repository at this point in the history
…ncate-cascade-instead-of-delete

DatabaseCleaner to truncate cascade instead of delete
  • Loading branch information
allanmckenzie committed Jun 2, 2020
2 parents 490a71f + 9ed0c1c commit 2967800
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
public class DatabaseCleaner {

private static final String SQL_PATTERN = "DELETE FROM %s";
private static final String SQL_PATTERN = "TRUNCATE TABLE %s CASCADE";

private final TestJdbcConnectionProvider testJdbcConnectionProvider;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.justice.services.test.utils.persistence;

import static java.lang.String.format;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
Expand All @@ -18,6 +19,7 @@
import org.junit.Test;

public class DatabaseCleanerTest {
private static final String SQL_PATTERN = "TRUNCATE TABLE %s CASCADE";

private final TestJdbcConnectionProvider testJdbcConnectionProvider = mock(TestJdbcConnectionProvider.class);

Expand All @@ -36,8 +38,8 @@ public void shouldCleanSomeViewStoreTables() throws Exception {
final PreparedStatement preparedStatement_2 = mock(PreparedStatement.class);

when(testJdbcConnectionProvider.getViewStoreConnection(contextName)).thenReturn(connection);
when(connection.prepareStatement("DELETE FROM " + table_1)).thenReturn(preparedStatement_1);
when(connection.prepareStatement("DELETE FROM " + table_2)).thenReturn(preparedStatement_2);
when(connection.prepareStatement(format(SQL_PATTERN, table_1))).thenReturn(preparedStatement_1);
when(connection.prepareStatement(format(SQL_PATTERN, table_2))).thenReturn(preparedStatement_2);

databaseCleaner.cleanViewStoreTables(contextName, table_1, table_2);

Expand All @@ -58,11 +60,11 @@ public void shouldCleanTheEventStoreTables() throws Exception {
final PreparedStatement preparedStatement = mock(PreparedStatement.class);

when(testJdbcConnectionProvider.getEventStoreConnection(contextName)).thenReturn(connection);
when(connection.prepareStatement("DELETE FROM " + "event_log")).thenReturn(preparedStatement);
when(connection.prepareStatement("DELETE FROM " + "event_stream")).thenReturn(preparedStatement);
when(connection.prepareStatement("DELETE FROM " + "pre_publish_queue")).thenReturn(preparedStatement);
when(connection.prepareStatement("DELETE FROM " + "publish_queue")).thenReturn(preparedStatement);
when(connection.prepareStatement("DELETE FROM " + "published_event")).thenReturn(preparedStatement);
when(connection.prepareStatement(format(SQL_PATTERN, "event_log"))).thenReturn(preparedStatement);
when(connection.prepareStatement(format(SQL_PATTERN, "event_stream"))).thenReturn(preparedStatement);
when(connection.prepareStatement(format(SQL_PATTERN, "pre_publish_queue"))).thenReturn(preparedStatement);
when(connection.prepareStatement(format(SQL_PATTERN, "publish_queue"))).thenReturn(preparedStatement);
when(connection.prepareStatement(format(SQL_PATTERN, "published_event"))).thenReturn(preparedStatement);

databaseCleaner.cleanEventStoreTables(contextName);

Expand All @@ -80,7 +82,7 @@ public void shouldCleanTheSystemTables() throws Exception {
final PreparedStatement preparedStatement = mock(PreparedStatement.class);

when(testJdbcConnectionProvider.getSystemConnection(contextName)).thenReturn(connection);
when(connection.prepareStatement("DELETE FROM " + "stored_command")).thenReturn(preparedStatement);
when(connection.prepareStatement(format(SQL_PATTERN, "stored_command"))).thenReturn(preparedStatement);

databaseCleaner.cleanSystemTables(contextName);

Expand All @@ -99,7 +101,7 @@ public void shouldCleanTheStreamBufferTable() throws Exception {
final PreparedStatement preparedStatement = mock(PreparedStatement.class);

when(testJdbcConnectionProvider.getViewStoreConnection(contextName)).thenReturn(connection);
when(connection.prepareStatement("DELETE FROM " + tableName)).thenReturn(preparedStatement);
when(connection.prepareStatement(format(SQL_PATTERN, tableName))).thenReturn(preparedStatement);

databaseCleaner.cleanStreamBufferTable(contextName);

Expand All @@ -118,7 +120,7 @@ public void shouldCleanTheStreamStatusTable() throws Exception {
final PreparedStatement preparedStatement = mock(PreparedStatement.class);

when(testJdbcConnectionProvider.getViewStoreConnection(contextName)).thenReturn(connection);
when(connection.prepareStatement("DELETE FROM " + tableName)).thenReturn(preparedStatement);
when(connection.prepareStatement(format(SQL_PATTERN, tableName))).thenReturn(preparedStatement);

databaseCleaner.cleanStreamStatusTable(contextName);

Expand All @@ -137,7 +139,7 @@ public void shouldCleanTheProcessedEventTable() throws Exception {
final PreparedStatement preparedStatement = mock(PreparedStatement.class);

when(testJdbcConnectionProvider.getViewStoreConnection(contextName)).thenReturn(connection);
when(connection.prepareStatement("DELETE FROM " + tableName)).thenReturn(preparedStatement);
when(connection.prepareStatement(format(SQL_PATTERN, tableName))).thenReturn(preparedStatement);

databaseCleaner.cleanProcessedEventTable(contextName);

Expand All @@ -157,7 +159,7 @@ public void shouldThrowADataAccessExceptionIfCleaningAViewStoreTableFails() thro
final Connection connection = mock(Connection.class);

when(testJdbcConnectionProvider.getViewStoreConnection(contextName)).thenReturn(connection);
when(connection.prepareStatement("DELETE FROM " + tableName)).thenThrow(sqlException);
when(connection.prepareStatement(format(SQL_PATTERN, tableName))).thenThrow(sqlException);

try {
databaseCleaner.cleanStreamBufferTable(contextName);
Expand All @@ -181,7 +183,7 @@ public void shouldThrowADataAccessExceptionIfCleaningTheEventStoreTablesFails()
final Connection connection = mock(Connection.class);

when(testJdbcConnectionProvider.getEventStoreConnection(contextName)).thenReturn(connection);
when(connection.prepareStatement("DELETE FROM " + tableName)).thenThrow(sqlException);
when(connection.prepareStatement(format(SQL_PATTERN, tableName))).thenThrow(sqlException);

try {
databaseCleaner.cleanEventLogTable(contextName);
Expand All @@ -206,7 +208,7 @@ public void shouldThrowADatAccessExceptionIfClosingTheViewStoreConnectionFails()
final PreparedStatement preparedStatement = mock(PreparedStatement.class);

when(testJdbcConnectionProvider.getViewStoreConnection(contextName)).thenReturn(connection);
when(connection.prepareStatement("DELETE FROM " + tableName)).thenReturn(preparedStatement);
when(connection.prepareStatement(format(SQL_PATTERN, tableName))).thenReturn(preparedStatement);
doThrow(sqlException).when(preparedStatement).close();

try {
Expand All @@ -231,7 +233,7 @@ public void shouldThrowADatAccessExceptionIfClosingTheSystemConnectionFails() th
final PreparedStatement preparedStatement = mock(PreparedStatement.class);

when(testJdbcConnectionProvider.getSystemConnection(contextName)).thenReturn(connection);
when(connection.prepareStatement("DELETE FROM " + tableName)).thenReturn(preparedStatement);
when(connection.prepareStatement(format(SQL_PATTERN, tableName))).thenReturn(preparedStatement);
doThrow(sqlException).when(preparedStatement).close();

try {
Expand All @@ -256,7 +258,7 @@ public void shouldThrowADatAccessExceptionIfClosingTheEventStoreConnectionFails(
final PreparedStatement preparedStatement = mock(PreparedStatement.class);

when(testJdbcConnectionProvider.getEventStoreConnection(contextName)).thenReturn(connection);
when(connection.prepareStatement("DELETE FROM " + tableName)).thenReturn(preparedStatement);
when(connection.prepareStatement(format(SQL_PATTERN, tableName))).thenReturn(preparedStatement);
doThrow(sqlException).when(preparedStatement).close();

try {
Expand Down

0 comments on commit 2967800

Please sign in to comment.