Skip to content

Commit

Permalink
Support oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuehlma committed Sep 24, 2024
1 parent c97e31a commit 372dca2
Show file tree
Hide file tree
Showing 94 changed files with 1,748 additions and 292 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ jobs:
with:
fetch-depth: 0
- name: Build
run: ./gradlew -S -i assemble
run: ./gradlew -S -i assemble -PjooqProUser=${{ secrets.JOOQ_PRO_USER }} -PjooqProPassword=${{ secrets.JOOQ_PRO_PASSWORD }}
- name: Test
run: ./gradlew -S -i test
run: ./gradlew -S -i test -PjooqProUser=${{ secrets.JOOQ_PRO_USER }} -PjooqProPassword=${{ secrets.JOOQ_PRO_PASSWORD }}
- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish_to_central.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: mkdir build && echo '${{secrets.SIGNING_KEY_FILE_BASE64}}' | base64 -d > build/adam_signing_key.gpg

- name: Upload
run: ./gradlew -S -i publishReleasePublicationToSonatypeRepository -Psigning.keyId=${{ secrets.SIGNING_KEY_ID }} -Psigning.password='${{ secrets.SIGNING_PASSWORD }}' -Psigning.secretKeyRingFile=../build/adam_signing_key.gpg -PossrhUsername='${{ secrets.OSSRH_USERNAME }}' -PossrhPassword='${{ secrets.OSSRH_PASSWORD }}' -PossrhStagingProfileId='${{ secrets.OSSRH_STAGING_PROFILE_ID }}'
run: ./gradlew -S -i publishReleasePublicationToSonatypeRepository -PjooqProUser=${{ secrets.JOOQ_PRO_USER }} -PjooqProPassword=${{ secrets.JOOQ_PRO_PASSWORD }} -Psigning.keyId=${{ secrets.SIGNING_KEY_ID }} -Psigning.password='${{ secrets.SIGNING_PASSWORD }}' -Psigning.secretKeyRingFile=../build/adam_signing_key.gpg -PossrhUsername='${{ secrets.OSSRH_USERNAME }}' -PossrhPassword='${{ secrets.OSSRH_PASSWORD }}' -PossrhStagingProfileId='${{ secrets.OSSRH_STAGING_PROFILE_ID }}'

- name: Cleanup
if: always()
Expand Down
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Supported:
- YML-Files
- PostgreSQL
- SQLite
- Oracle

### Schema sink

Expand All @@ -31,6 +32,7 @@ Supported:
- YML-Files
- PostgreSQL
- SQLite
- Oracle

### Automated schema migrator

Expand Down Expand Up @@ -69,7 +71,7 @@ In case of the **Once**-scripts the script executor needs to decide which script
artificial version of the software. On every migration, the current version is stored in a separate table `db_schema_version` on the database itself. To check
which scripts need to be executed, the script executor checks which scripts have been added since the current version of the database. The order of execution is
then given by the artificial version.

za
---
**NOTE**

Expand Down
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
10 changes: 5 additions & 5 deletions core/src/main/java/ch/ergon/adam/core/Adam.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,23 +219,23 @@ private void ensureSchemaVersionTable(String targetUrl) {
}

private void ensureNoInProgressMigrations(SqlExecutor sqlExecutor) {
Object result = sqlExecutor.queryResult(format("SELECT COUNT(1) FROM %s WHERE execution_completed_at IS NULL", SCHEMA_VERSION_TABLE_NAME));
if (!(result.equals(0L) || result.equals(0))) {
Object result = sqlExecutor.queryResult(format("SELECT COUNT(1) FROM \"%s\" WHERE \"execution_completed_at\" IS NULL", SCHEMA_VERSION_TABLE_NAME));
if (Integer.parseInt(result.toString()) != 0) {
throw new RuntimeException("There is an unfinished migration in [" + SCHEMA_VERSION_TABLE_NAME + "]");
}
}

private String getDbSchemaVersion(SqlExecutor sqlExecutor) {
Object result = sqlExecutor.queryResult(format("SELECT target_version FROM %s ORDER BY execution_started_at DESC", SCHEMA_VERSION_TABLE_NAME));
Object result = sqlExecutor.queryResult(format("SELECT \"target_version\" FROM \"%s\" ORDER BY \"execution_started_at\" DESC", SCHEMA_VERSION_TABLE_NAME));
return result == null ? null : result.toString();
}

private void createSchemaVersionEntry(SqlExecutor sqlExecutor, String fromVersion, String toVersion) {
sqlExecutor.queryResult(format("INSERT INTO %s (execution_started_at, source_version, target_version) VALUES (CURRENT_TIMESTAMP, ?, ?)", SCHEMA_VERSION_TABLE_NAME), fromVersion, toVersion);
sqlExecutor.queryResult(format("INSERT INTO \"%s\" (\"execution_started_at\", \"source_version\", \"target_version\") VALUES (CURRENT_TIMESTAMP, ?, ?)", SCHEMA_VERSION_TABLE_NAME), fromVersion, toVersion);
}

private void completeSchemaVersionEntry(SqlExecutor sqlExecutor) {
sqlExecutor.queryResult(format("UPDATE %s SET execution_completed_at = CURRENT_TIMESTAMP WHERE execution_completed_at IS NULL", SCHEMA_VERSION_TABLE_NAME));
sqlExecutor.queryResult(format("UPDATE \"%s\" SET \"execution_completed_at\" = CURRENT_TIMESTAMP WHERE \"execution_completed_at\" IS NULL", SCHEMA_VERSION_TABLE_NAME));
}

public void setAllowUnknownDBVersion(boolean allowUnknownDBVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ private void applyTableRecreate(Table sourceTable, Table targetTable, SchemaSink

sink.copyData(sourceTable, targetTable, insertSourceTableName);

sink.adjustSequences(targetTable);

sink.dropTable(new Table(insertSourceTableName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ public void dropSequencesAndDefaults(Table table) {
wrappedSink.dropSequencesAndDefaults(table);
}

@Override
public void adjustSequences(Table table) {
logger.info("Adjust sequences for table [{}]", table.getName());
wrappedSink.adjustSequences(table);
}

@Override
public void close() throws Exception {
wrappedSink.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ public interface SchemaSink extends AutoCloseable {
default boolean supportAlterAndDropField() {
return true;
}

void adjustSequences(Table table);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1
2 1
3 1
4 2 3
5 4
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
name: "test_table"
fields:
- name: "id"
dataType: "BIGINT"
sequence: true
- name: "col1"
dataType: "DECIMAL_INTEGER"
- name: "col2"
dataType: "NUMERIC"
defaultValue: "10"
nullable: true
precision: 10
scale: 2
- name: "col3"
dataType: "VARCHAR"
nullable: true
- name: "col4"
dataType: "VARCHAR"
length: 10
- name: "col5"
dataType: "VARCHAR"
nullable: true
foreignKeys: []
indexes:
- name: "test_table_col1_key"
fields:
- "col1"
unique: true
- name: "test_table_pkey"
fields:
- "id"
primary: true
unique: true
ruleConstraints: []
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a faulty script and should cause an error if executed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE a_table
(
id integer
)
;
CREATE TABLE another_table
(
id integer
)
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
INSERT INTO another_table
VALUES (2)
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE test_table
(
col1 CLOB
)
;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
CREATE TABLE a_table
(
id integer
);
)
;
CREATE TABLE another_table
(
id integer
)
;
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
INSERT INTO another_table
VALUES (2)
;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
CREATE TABLE test_table
(
col1 TEXT
);
)
;
2 changes: 1 addition & 1 deletion integration-test-db/src/main/resources/adam/target_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4
5
21 changes: 21 additions & 0 deletions integration-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,44 @@ 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'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.1'
testImplementation group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
testImplementation("org.testcontainers:testcontainers:1.20.1")
testImplementation("com.fasterxml.jackson.core:jackson-annotations") {
version {
strictly("2.12.2")
}
}
testImplementation("org.testcontainers:junit-jupiter:1.20.1")
testImplementation("org.testcontainers:postgresql:1.20.1")
testImplementation("org.postgresql:postgresql:42.7.1")
testImplementation("org.testcontainers:oracle-free:1.20.1")
testImplementation('com.oracle.database.jdbc:ojdbc11:23.5.0.24.07')
testImplementation('org.jooq.pro:jooq:3.19.11')
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,40 @@
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;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class DbMigrationWithSchemaTest extends AbstractPostgresqlTestBase {
public abstract class AbstractDbMigrationWithSchemaTest extends AbstractDbTestBase {

public AbstractDbMigrationWithSchemaTest(TestDbUrlProvider testDbUrlProvider) {
super(testDbUrlProvider);
}

private static final String DB_VERSION_5 = "5";
private static final String DB_VERSION_4 = "4";

private Adam getDbMigration(String targetVersion) throws IOException {
Path exportPath = Paths.get("../integration-test-db/" + DEFAULT_MAIN_RESOURCE_PATH + DEFAULT_ADAM_PACKAGE);
Path exportPath = Paths.get("../integration-test-db/" + DEFAULT_MAIN_RESOURCE_PATH + DEFAULT_ADAM_PACKAGE + getExportFolder());
Path targetVersionFile = exportPath.resolve(TARGET_VERSION_FILE_NAME);
Files.write(targetVersionFile, targetVersion.getBytes(UTF_8));
return Adam.usingExportDirectory(getTargetDbUrl(), "yml", exportPath.resolve("schema"), exportPath);
}

protected String getExportFolder() {
return "";
}

private void doMigrate(String targetVersion) throws IOException {
getDbMigration(targetVersion).execute();
}

private String getCurrentSchemaVersion() throws SQLException {
ResultSet result = getTargetDbConnection().createStatement().executeQuery(format("SELECT target_version FROM %s ORDER BY execution_started_at DESC", SCHEMA_VERSION_TABLE_NAME));
ResultSet result = getTargetDbConnection().createStatement().executeQuery(format("SELECT \"target_version\" FROM \"%s\" ORDER BY \"execution_started_at\" DESC", SCHEMA_VERSION_TABLE_NAME));
assertTrue(result.next());
return result.getString(1);
}
Expand All @@ -47,7 +53,7 @@ private String getCurrentSchemaVersion() throws SQLException {
public void testPreMigrationCreatingTable() throws Exception {
doMigrate(DB_VERSION_4);
assertThat(getCurrentSchemaVersion(), is(DB_VERSION_4));
getTargetDbConnection().createStatement().execute(format("DROP TABLE test_table"));
getTargetDbConnection().createStatement().execute(format("DROP TABLE \"test_table\""));
doMigrate(DB_VERSION_5);
assertThat(getCurrentSchemaVersion(), is(DB_VERSION_5));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ch.ergon.adam.integrationtest;

import ch.ergon.adam.core.Adam;
import ch.ergon.adam.integrationtest.postgresql.AbstractPostgresqlTestBase;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -12,17 +11,20 @@
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;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class DbMigrationWithoutSchemaTest extends AbstractPostgresqlTestBase {
public abstract class AbstractDbMigrationWithoutSchemaTest extends AbstractDbTestBase {

public AbstractDbMigrationWithoutSchemaTest(TestDbUrlProvider testDbUrlProvider) {
super(testDbUrlProvider);
}


private static final String DB_VERSION_UNKNOWN = "6";
private static final String DB_VERSION_5 = "5";
Expand All @@ -42,20 +44,20 @@ private void doMigrate(String targetVersion) throws IOException {
}

private String getCurrentSchemaVersion() throws SQLException {
ResultSet result = getTargetDbConnection().createStatement().executeQuery(format("SELECT target_version FROM %s ORDER BY execution_started_at DESC", SCHEMA_VERSION_TABLE_NAME));
ResultSet result = getTargetDbConnection().createStatement().executeQuery(format("SELECT \"target_version\" FROM \"%s\" ORDER BY \"execution_started_at\" DESC", SCHEMA_VERSION_TABLE_NAME));
assertTrue(result.next());
return result.getString(1);
}

private int countMigrations() throws SQLException {
ResultSet result = getTargetDbConnection().createStatement().executeQuery(format("SELECT count(*) FROM %s", SCHEMA_VERSION_TABLE_NAME));
ResultSet result = getTargetDbConnection().createStatement().executeQuery(format("SELECT count(*) FROM \"%s\"", SCHEMA_VERSION_TABLE_NAME));
assertTrue(result.next());
return result.getInt(1);
}


private void setCurrentSchemaVersion(String version) throws SQLException {
getTargetDbConnection().createStatement().execute(format("UPDATE %s SET target_version = '%s'", SCHEMA_VERSION_TABLE_NAME, version));
getTargetDbConnection().createStatement().execute(format("UPDATE \"%s\" SET \"target_version\" = '%s'", SCHEMA_VERSION_TABLE_NAME, version));
}

@Test
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 @@ -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
Loading

0 comments on commit 372dca2

Please sign in to comment.