-
Notifications
You must be signed in to change notification settings - Fork 2
User specified database cleanup #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3dee595
06d65a6
70555d4
4315d42
efa35b0
ac242c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.exasol.dbcleaner; | ||
|
||
/** | ||
* A request enabling the user to enable or disable the drop of certain database objects | ||
* while using {@link ExasolDatabaseCleaner}. | ||
*/ | ||
public final class CleanRequest { | ||
private final boolean cleanObjects; | ||
private final boolean cleanConnections; | ||
private final boolean cleanUsers; | ||
private final boolean cleanRoles; | ||
|
||
/** | ||
* Create an instance of {@link CleanRequestBuilder} | ||
* @return instance of {@link CleanRequestBuilder} | ||
*/ | ||
public static CleanRequestBuilder builder() { | ||
return new CleanRequestBuilder(); | ||
} | ||
|
||
CleanRequest(final boolean cleanObjects, final boolean cleanConnections, final boolean cleanUsers, final boolean cleanRoles) { | ||
this.cleanObjects = cleanObjects; | ||
this.cleanConnections = cleanConnections; | ||
this.cleanUsers = cleanUsers; | ||
this.cleanRoles = cleanRoles; | ||
} | ||
|
||
/** | ||
* Should all database objects be dropped? | ||
* @return true if all objects should be dropped | ||
*/ | ||
public boolean isCleanObjects() { | ||
return cleanObjects; | ||
} | ||
|
||
/** | ||
* Should all connections be dropped? | ||
* @return true if all connections should be dropped | ||
*/ | ||
public boolean isCleanConnections() { | ||
return cleanConnections; | ||
} | ||
|
||
/** | ||
* Should all user be dropped? | ||
* @return true if users should be dropped | ||
*/ | ||
public boolean isCleanUsers() { | ||
return cleanUsers; | ||
} | ||
|
||
/** | ||
* Should all roles be dropped? | ||
* @return true if all roles should be dropped | ||
*/ | ||
public boolean isCleanRoles() { | ||
return cleanRoles; | ||
} | ||
|
||
/** | ||
* Should everything be dropped? | ||
* @return true if everything should be dropped | ||
*/ | ||
public boolean isCleanAll() { | ||
return cleanObjects && cleanConnections && cleanUsers && cleanRoles; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.exasol.dbcleaner; | ||
|
||
/** | ||
* Builder to create instances of {@link CleanRequest}. | ||
*/ | ||
public class CleanRequestBuilder { | ||
private boolean cleanObjects = true; | ||
exa-mn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private boolean cleanConnections = true; | ||
private boolean cleanUsers = true; | ||
private boolean cleanRoles = true; | ||
|
||
CleanRequestBuilder() { | ||
} | ||
|
||
/** | ||
* Set whether database objects should be dropped | ||
* @param cleanObjects true if all database objects should be dropped | ||
* @return itself to enable chaining | ||
*/ | ||
public CleanRequestBuilder setCleanObjects(final boolean cleanObjects) { | ||
this.cleanObjects = cleanObjects; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set whether connections should be dropped | ||
* @param cleanConnections true if all connections should be dropped | ||
* @return itself to enable chaining | ||
*/ | ||
public CleanRequestBuilder setCleanConnections(final boolean cleanConnections) { | ||
this.cleanConnections = cleanConnections; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set whether users should be dropped | ||
* @param cleanUsers true if all users should be dropped | ||
* @return itself to enable chaining | ||
*/ | ||
public CleanRequestBuilder setCleanUsers(final boolean cleanUsers) { | ||
this.cleanUsers = cleanUsers; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set whether roles should be dropped | ||
* @param cleanRoles true if all roles should be dropped | ||
* @return itself to enable chaining | ||
*/ | ||
public CleanRequestBuilder setCleanRoles(final boolean cleanRoles) { | ||
this.cleanRoles = cleanRoles; | ||
return this; | ||
} | ||
|
||
/** | ||
* Build the instance of {@link CleanRequest}. | ||
* @return the instance of {@link CleanRequest}. | ||
*/ | ||
public CleanRequest build() { | ||
return new CleanRequest(cleanObjects, cleanConnections, cleanUsers, cleanRoles); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,24 @@ | ||||||||
package com.exasol.dbcleaner; | ||||||||
|
||||||||
import org.junit.jupiter.api.Test; | ||||||||
|
||||||||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||||||||
|
||||||||
class CleanRequestBuilderTest { | ||||||||
|
||||||||
@Test | ||||||||
Comment on lines
+8
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||||||||
void test_build() { | ||||||||
final CleanRequest underTest = CleanRequest.builder() | ||||||||
.setCleanConnections(false) | ||||||||
.setCleanObjects(false) | ||||||||
.setCleanUsers(false) | ||||||||
.setCleanRoles(false) | ||||||||
.build(); | ||||||||
|
||||||||
assertFalse(underTest.isCleanConnections()); | ||||||||
assertFalse(underTest.isCleanAll()); | ||||||||
assertFalse(underTest.isCleanObjects()); | ||||||||
assertFalse(underTest.isCleanRoles()); | ||||||||
assertFalse(underTest.isCleanUsers()); | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,9 @@ | |
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
import org.junit.jupiter.api.*; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
|
@@ -39,6 +41,22 @@ void testPurgeSchema() throws SQLException { | |
assertDoesNotThrow(this::createSchema); | ||
} | ||
|
||
@Test | ||
void testPurgeSchema_userSpecified() throws SQLException { | ||
createSchema(); | ||
|
||
final CleanRequest request = CleanRequest.builder() | ||
.setCleanConnections(false) | ||
.setCleanRoles(false) | ||
.setCleanUsers(false) | ||
.setCleanConnections(false) | ||
.setCleanObjects(true) | ||
.build(); | ||
|
||
CLEANER.cleanDatabase(request); | ||
assertDoesNotThrow(this::createSchema); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This tests only if the object is deleted, not if the other ones are untouched. 50% done. :-) |
||
} | ||
|
||
@Test | ||
void testPurgeTable() throws SQLException { | ||
createSchema(); | ||
|
@@ -48,27 +66,95 @@ void testPurgeTable() throws SQLException { | |
assertDoesNotThrow(this::createTable); | ||
} | ||
|
||
@Test | ||
void testPurgeTable_userSpecified() throws SQLException { | ||
createSchema(); | ||
createTable(); | ||
|
||
final CleanRequest request = CleanRequest.builder() | ||
.setCleanConnections(false) | ||
.setCleanRoles(false) | ||
.setCleanUsers(false) | ||
.setCleanConnections(false) | ||
.setCleanObjects(true) | ||
.build(); | ||
|
||
CLEANER.cleanDatabase(request); | ||
|
||
createSchema(); | ||
assertDoesNotThrow(this::createTable); | ||
} | ||
|
||
@Test | ||
void testPurgeConnection() throws SQLException { | ||
createConnection(); | ||
CLEANER.cleanDatabase(); | ||
assertDoesNotThrow(this::createConnection); | ||
} | ||
|
||
@Test | ||
void testPurgeConnection_userSpecified() throws SQLException { | ||
createConnection(); | ||
|
||
final CleanRequest request = CleanRequest.builder() | ||
.setCleanConnections(true) | ||
.setCleanRoles(false) | ||
.setCleanUsers(false) | ||
.setCleanConnections(false) | ||
.setCleanObjects(false) | ||
.build(); | ||
|
||
CLEANER.cleanDatabase(request); | ||
assertDoesNotThrow(this::createConnection); | ||
} | ||
|
||
@Test | ||
void testPurgeUser() throws SQLException { | ||
createUser(); | ||
CLEANER.cleanDatabase(); | ||
assertDoesNotThrow(this::createUser); | ||
} | ||
|
||
@Test | ||
void testPurgeUser_userSpecified() throws SQLException { | ||
createUser(); | ||
|
||
final CleanRequest request = CleanRequest.builder() | ||
.setCleanConnections(false) | ||
.setCleanRoles(false) | ||
.setCleanUsers(true) | ||
.setCleanConnections(false) | ||
.setCleanObjects(false) | ||
.build(); | ||
|
||
CLEANER.cleanDatabase(request); | ||
assertDoesNotThrow(this::createUser); | ||
} | ||
|
||
@Test | ||
void testPurgeRole() throws SQLException { | ||
createRole(); | ||
CLEANER.cleanDatabase(); | ||
assertDoesNotThrow(this::createRole); | ||
} | ||
|
||
@Test | ||
void testPurgeRole_userSpecified() throws SQLException { | ||
createRole(); | ||
|
||
final CleanRequest request = CleanRequest.builder() | ||
.setCleanConnections(false) | ||
.setCleanRoles(true) | ||
.setCleanUsers(false) | ||
.setCleanConnections(false) | ||
.setCleanObjects(false) | ||
.build(); | ||
|
||
CLEANER.cleanDatabase(request); | ||
|
||
assertDoesNotThrow(this::createRole); | ||
} | ||
|
||
@Test | ||
void testPurgeFunction() throws SQLException { | ||
createFunction("S1"); | ||
|
@@ -84,6 +170,22 @@ void testPurgeFunctionWithNonImplicitSchemaName() throws SQLException { | |
assertDoesNotThrow(() -> createFunction("S1")); | ||
} | ||
|
||
@Test | ||
void testPurgeFunction_userSpecified() throws SQLException { | ||
createFunction("S1"); | ||
|
||
final CleanRequest request = CleanRequest.builder() | ||
.setCleanConnections(false) | ||
.setCleanRoles(false) | ||
.setCleanUsers(false) | ||
.setCleanConnections(false) | ||
.setCleanObjects(true) | ||
.build(); | ||
|
||
CLEANER.cleanDatabase(request); | ||
assertDoesNotThrow(() -> createFunction("S1")); | ||
} | ||
|
||
private void createFunction(final String schemaName) throws SQLException { | ||
STATEMENT.executeUpdate("CREATE SCHEMA " + schemaName + ";"); | ||
STATEMENT.executeUpdate("CREATE FUNCTION " + schemaName | ||
|
Uh oh!
There was an error while loading. Please reload this page.