Skip to content
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

Feature/oracle support #10

Merged
merged 2 commits into from
Sep 26, 2024
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
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
6 changes: 6 additions & 0 deletions 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 @@ -151,6 +153,10 @@ adam {
allowUnknownDBVersion
// Execute migration even if source (DB) version is not an ancestor of the target version.
allowNonForwardMigration
// Exclude list of objects during migration
excludes
// Consider list of objects during migration
includes
}
}
```
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
39 changes: 32 additions & 7 deletions core/src/main/java/ch/ergon/adam/core/Adam.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
import ch.ergon.adam.core.prepost.MigrationScriptProvider;
import ch.ergon.adam.core.prepost.MigrationStep;
import ch.ergon.adam.core.prepost.MigrationStepExecutor;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import static ch.ergon.adam.core.prepost.MigrationStep.POSTMIGRATION_ALWAYS;
import static ch.ergon.adam.core.prepost.MigrationStep.POSTMIGRATION_INIT;
Expand All @@ -27,6 +30,7 @@
import static ch.ergon.adam.core.prepost.MigrationStep.PREMIGRATION_ONCE;
import static ch.ergon.adam.core.prepost.db_schema_version.DbSchemaVersionSource.SCHEMA_VERSION_TABLE_NAME;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newHashSet;
import static java.lang.ClassLoader.getSystemResourceAsStream;
import static java.lang.String.format;

Expand All @@ -49,6 +53,8 @@ public class Adam {
private boolean allowUnknownDBVersion = false;
private boolean allowNonForwardMigration = false;
private boolean migrateSameVersion = false;
private Collection<String> includes;
private Collection<String> excludes;


public static Adam usingGitRepo(String referenceSchemaUrl, String targetUrl, String targetVersion, File migrationScriptPath, File gitRepo) throws IOException {
Expand Down Expand Up @@ -163,7 +169,7 @@ public void execute() throws IOException {
MigrationStepExecutor executor = new MigrationStepExecutor(migrationScriptProvider, targetExecutor);
executor.executeStep(PREMIGRATION_ALWAYS);
executor.executeStep(PREMIGRATION_ONCE);
SchemaMigrator.migrate(referenceUrl, targetUrl);
SchemaMigrator.migrate(referenceUrl, targetUrl, getMigrationConfig());
executor.executeStep(POSTMIGRATION_ONCE);
executor.executeStep(POSTMIGRATION_ALWAYS);
} else {
Expand All @@ -172,7 +178,7 @@ public void execute() throws IOException {
if (isDbInit) {
executor.executeStep(PREMIGRATION_INIT);
}
SchemaMigrator.migrate(referenceUrl, targetUrl);
SchemaMigrator.migrate(referenceUrl, targetUrl, getMigrationConfig());
if (isDbInit) {
executor.executeStep(POSTMIGRATION_INIT);
}
Expand All @@ -190,6 +196,17 @@ public void execute() throws IOException {

}

private MigrationConfiguration getMigrationConfig() {
MigrationConfiguration migrationConfiguration = new MigrationConfiguration();
Set<String> excludeList = newHashSet(SCHEMA_VERSION_TABLE_NAME);
if (excludes != null) {
excludeList.addAll(excludes);
}
migrationConfiguration.setObjectNameExcludeList(excludeList);
migrationConfiguration.setObjectNameIncludeList(includes);
return migrationConfiguration;
}

private void logExecutionOrder(MigrationScriptProvider migrationScriptProvider) {
logger.info("The following scripts will be executed in given order:");
logExecutionOrderForStep(migrationScriptProvider, PREMIGRATION_ALWAYS);
Expand Down Expand Up @@ -219,23 +236,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 All @@ -261,4 +278,12 @@ public boolean isAllowNonForwardMigration() {
public void setAllowNonForwardMigration(boolean allowNonForwardMigration) {
this.allowNonForwardMigration = allowNonForwardMigration;
}

public void setIncludes(Collection<String> includes) {
this.includes = includes;
}

public void setExcludes(Collection<String> excludes) {
this.excludes = excludes;
}
}
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);
}
10 changes: 10 additions & 0 deletions gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ gradlePlugin {
}
}
}

publishing {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ist das noch Test-Code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ja, aber ich glaube das kann bleiben

repositories {
maven {
name = "localPluginRepository"
url = uri("/tmp/adam/local-gradle-plugin-repository")
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Strings.isNullOrEmpty;
Expand All @@ -25,6 +26,8 @@ public class MigrateDBTask extends DefaultTask {
private boolean migrateSameVersion;
private boolean allowNonForwardMigration;
private AdamExtension extension;
private Collection<String> excludes;
private Collection<String> includes;


public MigrateDBTask() {
Expand All @@ -48,6 +51,8 @@ void migrateDb() throws IOException {
adam.setAllowUnknownDBVersion(allowUnknownDBVersion);
adam.setMigrateSameVersion(migrateSameVersion);
adam.setAllowNonForwardMigration(allowNonForwardMigration);
adam.setIncludes(includes);
adam.setExcludes(excludes);
adam.execute();
}

Expand Down Expand Up @@ -114,4 +119,24 @@ public boolean getAllowNonForwardMigration() {
public void setAllowNonForwardMigration(boolean allowNonForwardMigration) {
this.allowNonForwardMigration = allowNonForwardMigration;
}

@Input
@Optional
public Collection<String> getExcludes() {
return excludes;
}

public void setExcludes(Collection<String> excludes) {
this.excludes = excludes;
}

@Input
@Optional
public Collection<String> getIncludes() {
return includes;
}

public void setIncludes(Collection<String> includes) {
this.includes = includes;
}
}
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
Loading
Loading