Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ch.ergon.adam.integrationtest.mariadb;

import ch.ergon.adam.integrationtest.testcases.SqlExecutorTest;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;

import java.sql.SQLException;

import static org.jooq.SQLDialect.MARIADB;

public class MariaDbSqlExecutorTest extends SqlExecutorTest {

private static final MariaDbTestDbUrlProvider TEST_DB_URL_PROVIDER = new MariaDbTestDbUrlProvider();

public MariaDbSqlExecutorTest() {
super(TEST_DB_URL_PROVIDER, MARIADB);
}

@Override
protected void verifyDroppedSchema() {
Exception e = Assertions.assertThrows(SQLException.class, () -> getTargetDbConnection());
Assertions.assertTrue(e.getMessage().contains("Unknown database 'test-target'"));
}

@AfterAll
public static void restartContainer() {
// MariaDB drops the whole database when dropping the schema, so we'll need to recreate the database
// as test containers are shared between tests.
TEST_DB_URL_PROVIDER.restartContainers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ protected void initDbForTest() throws SQLException {
}
}

public void restartContainers() {
sourceContainer.stop();
sourceContainer.start();
targetContainer.stop();
targetContainer.start();
}

private void cleanSchema(Connection conn) throws SQLException {
conn.createStatement().execute(format("DROP SCHEMA IF EXISTS `%s`", SOURCE_SCHEMA));
conn.createStatement().execute(format("CREATE SCHEMA `%s`", SOURCE_SCHEMA));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ch.ergon.adam.integrationtest.oracle;

import ch.ergon.adam.integrationtest.testcases.SqlExecutorTest;

import static org.jooq.SQLDialect.ORACLE;

public class OracleSqlExecutorTest extends SqlExecutorTest {
public OracleSqlExecutorTest() {
super(new OracleTestDbUrlProvider(), ORACLE);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ch.ergon.adam.integrationtest.oracle;

import ch.ergon.adam.integrationtest.postgresql.PostgreSqlTestDbUrlProvider;
import ch.ergon.adam.integrationtest.testcases.ViewTests;

import static org.jooq.SQLDialect.ORACLE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ch.ergon.adam.integrationtest.postgresql;

import ch.ergon.adam.integrationtest.testcases.SqlExecutorTest;

import static org.jooq.SQLDialect.POSTGRES;

public class PostgreSqlExecutorTest extends SqlExecutorTest {
public PostgreSqlExecutorTest() {
super(new PostgreSqlTestDbUrlProvider(), POSTGRES);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ch.ergon.adam.integrationtest.sqlite;

import ch.ergon.adam.core.db.SourceAndSinkFactory;
import ch.ergon.adam.core.db.interfaces.SqlExecutor;
import ch.ergon.adam.integrationtest.testcases.SqlExecutorTest;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static org.jooq.SQLDialect.SQLITE;

public class SqliteSqlExecutorTest extends SqlExecutorTest {

public SqliteSqlExecutorTest() throws IOException {
super(new SqliteTestFileDbUrlProvider(), SQLITE);
}

@Test
public void testDropSchema() throws Exception {
executeOnTargetDb(CREATE_TABLE_SQL);
executeOnTargetDb(INSERT_DATA_SQL);

String targetUrl = getTargetDbUrl();
try (
SqlExecutor sqlExecutor = SourceAndSinkFactory.getInstance().getSqlExecutor(targetUrl)
) {
Assert.assertThrows(UnsupportedOperationException.class, () -> sqlExecutor.dropSchema());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ch.ergon.adam.integrationtest.testcases;

import ch.ergon.adam.core.db.SourceAndSinkFactory;
import ch.ergon.adam.core.db.interfaces.SqlExecutor;
import ch.ergon.adam.integrationtest.AbstractDbTestBase;
import ch.ergon.adam.integrationtest.TestDbUrlProvider;
import org.jooq.SQLDialect;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

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

public abstract class SqlExecutorTest extends AbstractDbTestBase {

protected static final String CREATE_TABLE_SQL =
"create table \"test_table\" (" +
"col1 int" +
")";

protected static final String INSERT_DATA_SQL =
"insert into \"test_table\" values (1)";

public SqlExecutorTest(TestDbUrlProvider testDbUrlProvider, SQLDialect dialect) {
super(testDbUrlProvider, dialect);
}

@Test
public void testDropSchema() throws Exception {
executeOnTargetDb(CREATE_TABLE_SQL);
executeOnTargetDb(INSERT_DATA_SQL);
TestDbUrlProvider.close();

String targetUrl = getTargetDbUrl();
try (
SqlExecutor sqlExecutor = SourceAndSinkFactory.getInstance().getSqlExecutor(targetUrl)
) {
sqlExecutor.dropSchema();
}
verifyDroppedSchema();
}

protected void verifyDroppedSchema() throws SQLException {
ResultSet tempTable = getTargetDbConnection()
.getMetaData()
.getTables(null, getTargetDbConnection().getSchema(), "test_table", null);
Assertions.assertFalse(tempTable.next());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ch.ergon.adam.mariadb;

import ch.ergon.adam.jooq.JooqSqlExecutor;

import java.sql.Connection;

public class MariaDbSqlExecutor extends JooqSqlExecutor {

private final String schema;

public MariaDbSqlExecutor(String url, String schema) {
super(url, schema);
this.schema = schema;
}

public MariaDbSqlExecutor(Connection dbConnection, String schema) {
super(dbConnection, schema);
this.schema = schema;
}

@Override
public void dropSchema() {
context.dropSchema(schema).execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public MariaDbTransactionWrapper(String url, String schema, Runnable closeHandle
sqlSink = new MariaDbSink(dbConnection, schema);
sqlSource = new MariaDbSource(dbConnection, schema);
beginTransaction();
sqlExecutor = new JooqSqlExecutor(dbConnection, schema);
sqlExecutor = new MariaDbSqlExecutor(dbConnection, schema);
} catch (SQLException e) {
throw new RuntimeException(e);
}
Expand Down
49 changes: 49 additions & 0 deletions oracle/src/main/java/ch/ergon/adam/oracle/OracleSqlExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,53 @@ public void executeScript(String script) {
super.executeScript(statement.statement());
}
}

@Override
public void dropSchema() {
context.execute("""
BEGIN
FOR cur_rec IN (SELECT object_name, object_type
FROM user_objects
WHERE object_type IN
('TABLE',
'VIEW',
'PACKAGE',
'PROCEDURE',
'FUNCTION',
'SEQUENCE',
'TYPE',
'SYNONYM',
'MATERIALIZED VIEW',
'SCHEMA'
))
LOOP
BEGIN
IF cur_rec.object_type = 'TABLE'
THEN
EXECUTE IMMEDIATE 'DROP '
|| cur_rec.object_type
|| ' "'
|| cur_rec.object_name
|| '" CASCADE CONSTRAINTS';
ELSE
EXECUTE IMMEDIATE 'DROP '
|| cur_rec.object_type
|| ' "'
|| cur_rec.object_name
|| '"';
END IF;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ( 'FAILED: DROP '
|| cur_rec.object_type
|| ' "'
|| cur_rec.object_name
|| '"'
);
END;
END LOOP;
END;
""");
}
}
3 changes: 1 addition & 2 deletions sqlite/src/main/java/ch/ergon/adam/sqlite/SqliteFactory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ch.ergon.adam.sqlite;

import ch.ergon.adam.jooq.JooqSqlExecutor;
import ch.ergon.adam.core.db.interfaces.SchemaSink;
import ch.ergon.adam.core.db.interfaces.SchemaSource;
import ch.ergon.adam.core.db.interfaces.SourceAndSinkAdapter;
Expand Down Expand Up @@ -33,6 +32,6 @@ public SchemaSink createSink(String url) {

@Override
public SqlExecutor createSqlExecutor(String url) {
return new JooqSqlExecutor(url, null);
return new SqliteSqlExecutor(url, null);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ch.ergon.adam.sqlite;

import ch.ergon.adam.jooq.JooqSqlExecutor;
import ch.ergon.adam.core.db.interfaces.SchemaSink;
import ch.ergon.adam.core.db.interfaces.SchemaSource;
import ch.ergon.adam.core.db.interfaces.SourceAndSinkAdapter;
Expand Down Expand Up @@ -66,6 +65,6 @@ public SchemaSink createSink(String url) {

@Override
public SqlExecutor createSqlExecutor(String url) {
return new JooqSqlExecutor(getOrCreateConnection(url), null);
return new SqliteSqlExecutor(getOrCreateConnection(url), null);
}
}
21 changes: 21 additions & 0 deletions sqlite/src/main/java/ch/ergon/adam/sqlite/SqliteSqlExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ch.ergon.adam.sqlite;

import ch.ergon.adam.jooq.JooqSqlExecutor;

import java.sql.Connection;

public class SqliteSqlExecutor extends JooqSqlExecutor {

public SqliteSqlExecutor(String url, String schema) {
super(url, schema);
}

public SqliteSqlExecutor(Connection dbConnection, String schema) {
super(dbConnection, schema);
}

@Override
public void dropSchema() {
throw new UnsupportedOperationException("Drop schema not supported in Sqlite");
}
}