Skip to content

Commit

Permalink
Allow setting includes/excludes through gradle plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuehlma committed Sep 25, 2024
1 parent b64de52 commit 23de7b5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
29 changes: 27 additions & 2 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 @@ -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 @@ -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,22 @@ public boolean getAllowNonForwardMigration() {
public void setAllowNonForwardMigration(boolean allowNonForwardMigration) {
this.allowNonForwardMigration = allowNonForwardMigration;
}

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

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

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

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

0 comments on commit 23de7b5

Please sign in to comment.