Skip to content

Commit 91b1211

Browse files
authored
Merge pull request #10 from ergon/feature/oracle_support
Feature/oracle support
2 parents 14fa2af + 911b2ba commit 91b1211

File tree

96 files changed

+1822
-294
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+1822
-294
lines changed

.github/workflows/build_and_test.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ jobs:
1414
with:
1515
fetch-depth: 0
1616
- name: Build
17-
run: ./gradlew -S -i assemble
17+
run: ./gradlew -S -i assemble -PjooqProUser=${{ secrets.JOOQ_PRO_USER }} -PjooqProPassword=${{ secrets.JOOQ_PRO_PASSWORD }}
1818
- name: Test
19-
run: ./gradlew -S -i test
19+
run: ./gradlew -S -i test -PjooqProUser=${{ secrets.JOOQ_PRO_USER }} -PjooqProPassword=${{ secrets.JOOQ_PRO_PASSWORD }}
2020
- name: Publish Unit Test Results
2121
uses: EnricoMi/publish-unit-test-result-action@v2
2222
if: always()

.github/workflows/publish_to_central.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
run: mkdir build && echo '${{secrets.SIGNING_KEY_FILE_BASE64}}' | base64 -d > build/adam_signing_key.gpg
2525

2626
- name: Upload
27-
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 }}'
27+
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 }}'
2828

2929
- name: Cleanup
3030
if: always()

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ build/
55
/integration-test-db/src/main/resources/adamd/target_version
66
gradle-plugin-test/.gradle/
77
gradle.properties
8+
local.properties

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Supported:
2121
- YML-Files
2222
- PostgreSQL
2323
- SQLite
24+
- Oracle
2425

2526
### Schema sink
2627

@@ -31,6 +32,7 @@ Supported:
3132
- YML-Files
3233
- PostgreSQL
3334
- SQLite
35+
- Oracle
3436

3537
### Automated schema migrator
3638

@@ -151,6 +153,10 @@ adam {
151153
allowUnknownDBVersion
152154
// Execute migration even if source (DB) version is not an ancestor of the target version.
153155
allowNonForwardMigration
156+
// Exclude list of objects during migration
157+
excludes
158+
// Consider list of objects during migration
159+
includes
154160
}
155161
}
156162
```

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ ext.getLocalProperty = { key, file = "local.properties" ->
4545
def properties = new Properties()
4646
def localProperties = new File(rootProject.rootDir, file)
4747
if (localProperties.isFile()) {
48-
new InputStreamReader(new FileInputStream(localProperties), UTF_8).with {properties.load}
48+
new InputStreamReader(new FileInputStream(localProperties), UTF_8).with {properties.load(it)}
4949
return properties.getProperty(key, "")
5050
} else {
5151
return null

core/src/main/java/ch/ergon/adam/core/Adam.java

+32-7
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
import ch.ergon.adam.core.prepost.MigrationScriptProvider;
1212
import ch.ergon.adam.core.prepost.MigrationStep;
1313
import ch.ergon.adam.core.prepost.MigrationStepExecutor;
14+
import com.google.common.collect.Sets;
1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
1617

1718
import java.io.*;
1819
import java.nio.file.Files;
1920
import java.nio.file.Path;
21+
import java.util.Collection;
2022
import java.util.List;
23+
import java.util.Set;
2124

2225
import static ch.ergon.adam.core.prepost.MigrationStep.POSTMIGRATION_ALWAYS;
2326
import static ch.ergon.adam.core.prepost.MigrationStep.POSTMIGRATION_INIT;
@@ -27,6 +30,7 @@
2730
import static ch.ergon.adam.core.prepost.MigrationStep.PREMIGRATION_ONCE;
2831
import static ch.ergon.adam.core.prepost.db_schema_version.DbSchemaVersionSource.SCHEMA_VERSION_TABLE_NAME;
2932
import static com.google.common.collect.Lists.newArrayList;
33+
import static com.google.common.collect.Sets.newHashSet;
3034
import static java.lang.ClassLoader.getSystemResourceAsStream;
3135
import static java.lang.String.format;
3236

@@ -49,6 +53,8 @@ public class Adam {
4953
private boolean allowUnknownDBVersion = false;
5054
private boolean allowNonForwardMigration = false;
5155
private boolean migrateSameVersion = false;
56+
private Collection<String> includes;
57+
private Collection<String> excludes;
5258

5359

5460
public static Adam usingGitRepo(String referenceSchemaUrl, String targetUrl, String targetVersion, File migrationScriptPath, File gitRepo) throws IOException {
@@ -163,7 +169,7 @@ public void execute() throws IOException {
163169
MigrationStepExecutor executor = new MigrationStepExecutor(migrationScriptProvider, targetExecutor);
164170
executor.executeStep(PREMIGRATION_ALWAYS);
165171
executor.executeStep(PREMIGRATION_ONCE);
166-
SchemaMigrator.migrate(referenceUrl, targetUrl);
172+
SchemaMigrator.migrate(referenceUrl, targetUrl, getMigrationConfig());
167173
executor.executeStep(POSTMIGRATION_ONCE);
168174
executor.executeStep(POSTMIGRATION_ALWAYS);
169175
} else {
@@ -172,7 +178,7 @@ public void execute() throws IOException {
172178
if (isDbInit) {
173179
executor.executeStep(PREMIGRATION_INIT);
174180
}
175-
SchemaMigrator.migrate(referenceUrl, targetUrl);
181+
SchemaMigrator.migrate(referenceUrl, targetUrl, getMigrationConfig());
176182
if (isDbInit) {
177183
executor.executeStep(POSTMIGRATION_INIT);
178184
}
@@ -190,6 +196,17 @@ public void execute() throws IOException {
190196

191197
}
192198

199+
private MigrationConfiguration getMigrationConfig() {
200+
MigrationConfiguration migrationConfiguration = new MigrationConfiguration();
201+
Set<String> excludeList = newHashSet(SCHEMA_VERSION_TABLE_NAME);
202+
if (excludes != null) {
203+
excludeList.addAll(excludes);
204+
}
205+
migrationConfiguration.setObjectNameExcludeList(excludeList);
206+
migrationConfiguration.setObjectNameIncludeList(includes);
207+
return migrationConfiguration;
208+
}
209+
193210
private void logExecutionOrder(MigrationScriptProvider migrationScriptProvider) {
194211
logger.info("The following scripts will be executed in given order:");
195212
logExecutionOrderForStep(migrationScriptProvider, PREMIGRATION_ALWAYS);
@@ -219,23 +236,23 @@ private void ensureSchemaVersionTable(String targetUrl) {
219236
}
220237

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

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

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

237254
private void completeSchemaVersionEntry(SqlExecutor sqlExecutor) {
238-
sqlExecutor.queryResult(format("UPDATE %s SET execution_completed_at = CURRENT_TIMESTAMP WHERE execution_completed_at IS NULL", SCHEMA_VERSION_TABLE_NAME));
255+
sqlExecutor.queryResult(format("UPDATE \"%s\" SET \"execution_completed_at\" = CURRENT_TIMESTAMP WHERE \"execution_completed_at\" IS NULL", SCHEMA_VERSION_TABLE_NAME));
239256
}
240257

241258
public void setAllowUnknownDBVersion(boolean allowUnknownDBVersion) {
@@ -261,4 +278,12 @@ public boolean isAllowNonForwardMigration() {
261278
public void setAllowNonForwardMigration(boolean allowNonForwardMigration) {
262279
this.allowNonForwardMigration = allowNonForwardMigration;
263280
}
281+
282+
public void setIncludes(Collection<String> includes) {
283+
this.includes = includes;
284+
}
285+
286+
public void setExcludes(Collection<String> excludes) {
287+
this.excludes = excludes;
288+
}
264289
}

core/src/main/java/ch/ergon/adam/core/db/DefaultMigrationStrategy.java

+2
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ private void applyTableRecreate(Table sourceTable, Table targetTable, SchemaSink
402402

403403
sink.copyData(sourceTable, targetTable, insertSourceTableName);
404404

405+
sink.adjustSequences(targetTable);
406+
405407
sink.dropTable(new Table(insertSourceTableName));
406408
}
407409
}

core/src/main/java/ch/ergon/adam/core/db/LoggingSinkWrapper.java

+6
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ public void dropSequencesAndDefaults(Table table) {
164164
wrappedSink.dropSequencesAndDefaults(table);
165165
}
166166

167+
@Override
168+
public void adjustSequences(Table table) {
169+
logger.info("Adjust sequences for table [{}]", table.getName());
170+
wrappedSink.adjustSequences(table);
171+
}
172+
167173
@Override
168174
public void close() throws Exception {
169175
wrappedSink.close();

core/src/main/java/ch/ergon/adam/core/db/interfaces/SchemaSink.java

+2
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,6 @@ public interface SchemaSink extends AutoCloseable {
5757
default boolean supportAlterAndDropField() {
5858
return true;
5959
}
60+
61+
void adjustSequences(Table table);
6062
}

gradle-plugin/build.gradle

+10
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,13 @@ gradlePlugin {
3434
}
3535
}
3636
}
37+
38+
publishing {
39+
repositories {
40+
maven {
41+
name = "localPluginRepository"
42+
url = uri("/tmp/adam/local-gradle-plugin-repository")
43+
}
44+
}
45+
}
46+

gradle-plugin/src/main/java/ch/ergon/adam/gradleplugin/tasks/MigrateDBTask.java

+25
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.io.IOException;
1313
import java.nio.file.Path;
14+
import java.util.Collection;
1415

1516
import static com.google.common.base.MoreObjects.firstNonNull;
1617
import static com.google.common.base.Strings.isNullOrEmpty;
@@ -25,6 +26,8 @@ public class MigrateDBTask extends DefaultTask {
2526
private boolean migrateSameVersion;
2627
private boolean allowNonForwardMigration;
2728
private AdamExtension extension;
29+
private Collection<String> excludes;
30+
private Collection<String> includes;
2831

2932

3033
public MigrateDBTask() {
@@ -48,6 +51,8 @@ void migrateDb() throws IOException {
4851
adam.setAllowUnknownDBVersion(allowUnknownDBVersion);
4952
adam.setMigrateSameVersion(migrateSameVersion);
5053
adam.setAllowNonForwardMigration(allowNonForwardMigration);
54+
adam.setIncludes(includes);
55+
adam.setExcludes(excludes);
5156
adam.execute();
5257
}
5358

@@ -114,4 +119,24 @@ public boolean getAllowNonForwardMigration() {
114119
public void setAllowNonForwardMigration(boolean allowNonForwardMigration) {
115120
this.allowNonForwardMigration = allowNonForwardMigration;
116121
}
122+
123+
@Input
124+
@Optional
125+
public Collection<String> getExcludes() {
126+
return excludes;
127+
}
128+
129+
public void setExcludes(Collection<String> excludes) {
130+
this.excludes = excludes;
131+
}
132+
133+
@Input
134+
@Optional
135+
public Collection<String> getIncludes() {
136+
return includes;
137+
}
138+
139+
public void setIncludes(Collection<String> includes) {
140+
this.includes = includes;
141+
}
117142
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1
2+
2 1
3+
3 1
4+
4 2 3
5+
5 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
name: "test_table"
3+
fields:
4+
- name: "id"
5+
dataType: "BIGINT"
6+
sequence: true
7+
- name: "col1"
8+
dataType: "DECIMAL_INTEGER"
9+
- name: "col2"
10+
dataType: "NUMERIC"
11+
defaultValue: "10"
12+
nullable: true
13+
precision: 10
14+
scale: 2
15+
- name: "col3"
16+
dataType: "VARCHAR"
17+
nullable: true
18+
- name: "col4"
19+
dataType: "VARCHAR"
20+
length: 10
21+
- name: "col5"
22+
dataType: "VARCHAR"
23+
nullable: true
24+
foreignKeys: []
25+
indexes:
26+
- name: "test_table_col1_key"
27+
fields:
28+
- "col1"
29+
unique: true
30+
- name: "test_table_pkey"
31+
fields:
32+
- "id"
33+
primary: true
34+
unique: true
35+
ruleConstraints: []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a faulty script and should cause an error if executed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE a_table
2+
(
3+
id integer
4+
)
5+
;
6+
CREATE TABLE another_table
7+
(
8+
id integer
9+
)
10+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
INSERT INTO another_table
2+
VALUES (2)
3+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE test_table
2+
(
3+
col1 CLOB
4+
)
5+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
CREATE TABLE a_table
22
(
33
id integer
4-
);
4+
)
5+
;
56
CREATE TABLE another_table
67
(
78
id integer
89
)
10+
;
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
INSERT INTO another_table
22
VALUES (2)
3+
;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
CREATE TABLE test_table
22
(
33
col1 TEXT
4-
);
4+
)
5+
;
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4
1+
5

0 commit comments

Comments
 (0)