Skip to content

Commit

Permalink
Support oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuehlma committed Sep 22, 2024
1 parent c97e31a commit c67500b
Show file tree
Hide file tree
Showing 63 changed files with 1,381 additions and 253 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ build/
/integration-test-db/src/main/resources/adamd/target_version
gradle-plugin-test/.gradle/
gradle.properties
local.properties
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ext.getLocalProperty = { key, file = "local.properties" ->
def properties = new Properties()
def localProperties = new File(rootProject.rootDir, file)
if (localProperties.isFile()) {
new InputStreamReader(new FileInputStream(localProperties), UTF_8).with {properties.load}
new InputStreamReader(new FileInputStream(localProperties), UTF_8).with {properties.load(it)}
return properties.getProperty(key, "")
} else {
return null
Expand Down
15 changes: 15 additions & 0 deletions integration-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,27 @@ group 'ch.ergon.adam'

sourceCompatibility = 21

ext.jooqProUser = hasProperty("jooqProUser") ? property("jooqProUser").toString() : rootProject.getLocalProperty("JOOQ_PRO_USER")
ext.jooqProPassword = hasProperty("jooqProPassword") ? property("jooqProPassword").toString() : rootProject.getLocalProperty("JOOQ_PRO_PASSWORD")

repositories {
mavenCentral()
maven {
url = uri("https://repo.jooq.org/repo")
credentials {
username = jooqProUser
password = jooqProPassword
}
}
}

dependencies {
testImplementation group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.14.1'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.7.1'
testImplementation project(':core')
testImplementation project(':yml')
testImplementation project(':postgresql')
testImplementation project(':oracle')
testImplementation project(':sqlite')
testImplementation project(':integration-test-db')
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package ch.ergon.adam.integrationtest;

import ch.ergon.adam.yml.YmlSink;
import ch.ergon.adam.core.db.SchemaMigrator;
import ch.ergon.adam.core.db.SourceAndSinkFactory;
import ch.ergon.adam.core.db.interfaces.SchemaSink;
import ch.ergon.adam.core.db.interfaces.SchemaSource;
import ch.ergon.adam.core.db.schema.Schema;
import ch.ergon.adam.yml.YmlSink;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;

import static ch.ergon.adam.core.Adam.DEFAULT_ADAM_PACKAGE;
import static ch.ergon.adam.core.Adam.DEFAULT_MAIN_RESOURCE_PATH;
import static ch.ergon.adam.core.Adam.TARGET_VERSION_FILE_NAME;
import static ch.ergon.adam.core.Adam.*;
import static ch.ergon.adam.core.prepost.db_schema_version.DbSchemaVersionSource.SCHEMA_VERSION_TABLE_NAME;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;

import static ch.ergon.adam.core.Adam.DEFAULT_ADAM_PACKAGE;
import static ch.ergon.adam.core.Adam.DEFAULT_MAIN_RESOURCE_PATH;
import static ch.ergon.adam.core.Adam.TARGET_VERSION_FILE_NAME;
import static ch.ergon.adam.core.Adam.*;
import static ch.ergon.adam.core.prepost.db_schema_version.DbSchemaVersionSource.SCHEMA_VERSION_TABLE_NAME;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;

import static ch.ergon.adam.core.Adam.DEFAULT_ADAM_PACKAGE;
import static ch.ergon.adam.core.Adam.DEFAULT_MAIN_RESOURCE_PATH;
import static ch.ergon.adam.core.Adam.TARGET_VERSION_FILE_NAME;
import static ch.ergon.adam.core.Adam.*;
import static ch.ergon.adam.core.prepost.db_schema_version.DbSchemaVersionSource.SCHEMA_VERSION_TABLE_NAME;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package ch.ergon.adam.integrationtest.oracle;

import ch.ergon.adam.core.db.SchemaDiffExtractor;
import ch.ergon.adam.core.db.interfaces.SchemaSource;
import ch.ergon.adam.core.db.schema.Schema;
import ch.ergon.adam.core.db.schema.View;
import ch.ergon.adam.core.helper.FileHelper;
import ch.ergon.adam.integrationtest.AssertAnyChangeStrategy;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static ch.ergon.adam.core.helper.CollectorsHelper.createSchemaItemNameArray;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;

public abstract class AbstractOracleSchemaCoverageTest extends AbstractOracleTestBase {

private static Path tempFolder;

@BeforeAll
public static void setupTempFolder() throws IOException {
tempFolder = Files.createTempDirectory("ADAMTableTest");
tempFolder.toFile().deleteOnExit();
}

@AfterAll
public static void cleanupTempFolder() throws IOException {
FileHelper.deleteFolderRecursively(tempFolder);
}

private static final String CREATE_TABLE_SQL =
"CREATE TABLE test_table (" +
"id INTEGER GENERATED BY DEFAULT ON NULL AS IDENTITY, " +
"col1 INTEGER NOT NULL, " +
"col2 NUMBER(10,2) DEFAULT 10 NULL, " +
"col3 CLOB NULL, " +
"col4 VARCHAR2(10) NOT NULL, " +
"CONSTRAINT test_table_pkey PRIMARY KEY (id), " +
"CONSTRAINT test_table_col1_key UNIQUE (col1)" +
")";

private static final String CREATE_TABLE_WITH_FK_SQL =
"CREATE TABLE test_fktable (" +
"id INTEGER REFERENCES test_table(id), " +
"CONSTRAINT test_fktable_pkey PRIMARY KEY (id) " +
")";

private static final String CREATE_VIEW =
"CREATE VIEW test_view AS (" +
"SELECT * FROM test_table " +
")";

private static final String CREATE_SEQUENCE =
"CREATE SEQUENCE test_sequence";

private static final String CREATE_VIEW2 =
"CREATE VIEW depending_view AS (" +
"SELECT * FROM test_view " +
")";


@Test
public void testSchemaCoverage() throws Exception {
getSourceDbConnection().createStatement().execute(CREATE_TABLE_SQL);
getSourceDbConnection().createStatement().execute(CREATE_TABLE_WITH_FK_SQL);
getSourceDbConnection().createStatement().execute(CREATE_VIEW);
getSourceDbConnection().createStatement().execute(CREATE_VIEW2);
getSourceDbConnection().createStatement().execute(CREATE_SEQUENCE);
SchemaSource source = getSourceDbSource();
Schema finalSchema = executeTransformation(source);

assertSchemaEquals(source.getSchema(), finalSchema);
}

protected abstract Schema executeTransformation(SchemaSource source) throws Exception;

private void assertSchemaEquals(Schema sourceSchema, Schema targetSchema) {
SchemaDiffExtractor diffExtractor = new SchemaDiffExtractor(sourceSchema, targetSchema);
diffExtractor.process(new AssertAnyChangeStrategy());

sourceSchema.getViews().forEach(sourceView -> {
View targetView = targetSchema.getView(sourceView.getName());
String[] sourceDependencies = createSchemaItemNameArray(sourceView.getBaseRelations());
String[] targetDependencies = createSchemaItemNameArray(targetView.getBaseRelations());
assertArrayEquals(targetDependencies, sourceDependencies);
});
}
}
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.AbstractDbTestBase;

public class AbstractOracleTestBase extends AbstractDbTestBase {


public AbstractOracleTestBase() {
super(new OracleTestDbUrlProvider());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ch.ergon.adam.integrationtest.oracle;

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

public class OracleAddFieldTests extends AddFieldTests {
public OracleAddFieldTests() {
super(new OracleTestDbUrlProvider());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ch.ergon.adam.integrationtest.oracle;

import ch.ergon.adam.core.db.schema.Field;
import ch.ergon.adam.core.db.schema.Schema;
import ch.ergon.adam.core.db.schema.Table;
import ch.ergon.adam.integrationtest.DummySink;
import ch.ergon.adam.integrationtest.testcases.CastFieldTypeTest;
import org.junit.jupiter.api.Test;

import java.sql.ResultSet;

import static ch.ergon.adam.core.db.schema.DataType.INTEGER;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class OracleCastFieldTypeTests extends CastFieldTypeTest {
public OracleCastFieldTypeTests() {
super(new OracleTestDbUrlProvider());
}

@Test
public void testCastVarcharToSerial() throws Exception {

// Setup db
getTargetDbConnection().createStatement().execute(getCreateTableStatement());
getTargetDbConnection().createStatement().execute(INSERT_DATA_SQL);
DummySink dummySink = targetToDummy();
Schema schema = dummySink.getTargetSchema();

// Apply change
Table table = schema.getTable("test_table");
Field field = table.getField("col1");
field.setDataType(INTEGER);
field.setSequence(true);
migrateTargetWithSchema(schema);
dummySink = targetToDummy();
schema = dummySink.getTargetSchema();

// Verify
table = schema.getTable("test_table");
assertNotNull(table);

// Data still present?
ResultSet result = getTargetDbConnection().createStatement().executeQuery("select sum(\"col1\") from \"test_table\"");
assertTrue(result.next());
assertThat(result.getInt(1), is(2));
}

@Override
protected String getCreateTableStatement() {
return "create table \"test_table\" (" +
"\"col1\" varchar2(100), " +
"\"col2\" int " +
")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ch.ergon.adam.integrationtest.oracle;

import ch.ergon.adam.integrationtest.testcases.ChangeFieldTypeTest;
import org.junit.jupiter.api.Disabled;

public class OracleChangeFieldTypeTest extends ChangeFieldTypeTest {
public OracleChangeFieldTypeTest() {
super(new OracleTestDbUrlProvider());
}

protected String getCreateTableNotNullSql() {
return "create table \"test_table\" (" +
"\"test_field\" varchar(10) not null " +
")";
}

protected String getCreateTableNullSql() {
return "create table \"test_table\" (" +
"\"test_field\" numeric(10,2) null " +
")";
}

protected String getCreateTableTwoFieldsSql() {
return "create table \"test_table\" (" +
"\"col1\" clob null, " +
"\"col2\" clob null " +
")";
}

@Override
@Disabled
public void changeFromSerialToClob() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ch.ergon.adam.integrationtest.oracle;

import ch.ergon.adam.core.db.schema.Schema;
import ch.ergon.adam.integrationtest.DummySink;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;

public class OracleConstraintTests extends AbstractOracleTestBase {

private static final String CREATE_TABLE_SQL =
"create table test_table (" +
"id integer null CHECK (id > 0) " +
")";

@Test
public void testCreateConstraint() throws Exception {

// Setup db
getSourceDbConnection().createStatement().execute(CREATE_TABLE_SQL);
sourceToTarget();
DummySink dummySink = targetToDummy();

// Verify
Schema schema = dummySink.getTargetSchema();
assertThat(schema.getTable("TEST_TABLE").getConstraints().size(), is(1));
}

@Test
public void testRecreateConstraintAfterTableChange() throws Exception {

// Setup db
getSourceDbConnection().createStatement().execute(CREATE_TABLE_SQL);
sourceToTarget();
DummySink dummySink = targetToDummy();
Schema schema = dummySink.getTargetSchema();

// Apply change
schema.getTable("TEST_TABLE").getField("ID").setNullable(false);
migrateTargetWithSchema(schema);
dummySink = targetToDummy();
schema = dummySink.getTargetSchema();

// Verify
assertFalse(schema.getTable("TEST_TABLE").getField("ID").isNullable());
assertThat(schema.getTable("TEST_TABLE").getConstraints().size(), is(1));
}
}
Loading

0 comments on commit c67500b

Please sign in to comment.