Skip to content

Commit

Permalink
Fixed flyway#1062: API: Make it possible to use pre-instantiated Java…
Browse files Browse the repository at this point in the history
…-based migrations
  • Loading branch information
Axel Fontaine committed Dec 11, 2018
1 parent 03bbf67 commit 62f729b
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 53 deletions.
4 changes: 3 additions & 1 deletion flyway-commandline/src/main/assembly/flyway.conf
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ flyway.url=
# Flyway Pro and Flyway Enterprise only
# flyway.oracle.sqlplus=

# Flyway's license key.
# Your Flyway license key (FL01...). Not yet a Flyway Pro or Enterprise Edition customer?
# Request your Flyway trial license key st https://flywaydb.org/download/
# to try out Flyway Pro and Enterprise Edition features free for 30 days.
# Flyway Pro and Flyway Enterprise only
# flyway.licenseKey=
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.logging.Log;
import org.flywaydb.core.api.logging.LogFactory;
import org.flywaydb.core.api.migration.JavaMigration;
import org.flywaydb.core.api.resolver.MigrationResolver;
import org.flywaydb.core.internal.configuration.ConfigUtils;
import org.flywaydb.core.internal.jdbc.DriverDataSource;
Expand Down Expand Up @@ -190,6 +191,14 @@ public class ClassicConfiguration implements Configuration {
*/
private String[] sqlMigrationSuffixes = {".sql"};

/**
* The manually added Java-based migrations. These are not Java-based migrations discovered through classpath
* scanning and instantiated by Flyway. Instead these are manually added instances of JavaMigration.
* This is particularly useful when working with a dependency injection container, where you may want the DI
* container to instantiate the class and wire up its dependencies for you. (default: none)
*/
private JavaMigration[] javaMigrations = {};

/**
* Ignore missing migrations when reading the schema history table. These are migrations that were performed by an
* older deployment of the application that are no longer available in this version. For example: we have migrations
Expand Down Expand Up @@ -400,6 +409,8 @@ public class ClassicConfiguration implements Configuration {








Expand Down Expand Up @@ -496,6 +507,11 @@ public String[] getSqlMigrationSuffixes() {
return sqlMigrationSuffixes;
}

@Override
public JavaMigration[] getJavaMigrations() {
return javaMigrations;
}

@Override
public boolean isIgnoreMissingMigrations() {
return ignoreMissingMigrations;
Expand Down Expand Up @@ -1050,6 +1066,21 @@ public void setUndoSqlMigrationPrefix(String undoSqlMigrationPrefix) {



}

/**
* The manually added Java-based migrations. These are not Java-based migrations discovered through classpath
* scanning and instantiated by Flyway. Instead these are manually added instances of JavaMigration.
* This is particularly useful when working with a dependency injection container, where you may want the DI
* container to instantiate the class and wire up its dependencies for you.
*
* @param javaMigrations The manually added Java-based migrations. An empty array if none. (default: none)
*/
public void setJavaMigrations(JavaMigration... javaMigrations) {
if (javaMigrations == null) {
throw new FlywayException("javaMigrations cannot be null");
}
this.javaMigrations = javaMigrations;
}

@Override
Expand Down Expand Up @@ -1366,11 +1397,13 @@ public void setOracleSqlplus(boolean oracleSqlplus) {
}

/**
* Flyway's license key.
* Your Flyway license key (FL01...). Not yet a Flyway Pro or Enterprise Edition customer?
* Request your <a href="https://flywaydb.org/download/">Flyway trial license key</a>
* to try out Flyway Pro and Enterprise Edition features free for 30 days.
*
* <p><i>Flyway Pro and Flyway Enterprise only</i></p>
*
* @param licenseKey The license key.
* @param licenseKey Your Flyway license key.
*/
public void setLicenseKey(String licenseKey) {

Expand Down Expand Up @@ -1412,6 +1445,7 @@ public void configure(Configuration configuration) {
setIgnoreIgnoredMigrations(configuration.isIgnoreIgnoredMigrations());
setIgnorePendingMigrations(configuration.isIgnorePendingMigrations());
setInstalledBy(configuration.getInstalledBy());
setJavaMigrations(configuration.getJavaMigrations());
setLocations(configuration.getLocations());
setMixed(configuration.isMixed());
setOutOfOrder(configuration.isOutOfOrder());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.flywaydb.core.api.Location;
import org.flywaydb.core.api.MigrationVersion;
import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.migration.JavaMigration;
import org.flywaydb.core.api.resolver.MigrationResolver;

import javax.sql.DataSource;
Expand Down Expand Up @@ -151,6 +152,16 @@ public interface Configuration {
*/
String[] getSqlMigrationSuffixes();

/**
* The manually added Java-based migrations. These are not Java-based migrations discovered through classpath
* scanning and instantiated by Flyway. Instead these are manually added instances of JavaMigration.
* This is particularly useful when working with a dependency injection container, where you may want the DI
* container to instantiate the class and wire up its dependencies for you.
*
* @return The manually added Java-based migrations. An empty array if none. (default: none)
*/
JavaMigration[] getJavaMigrations();

/**
* Checks whether placeholders should be replaced.
*
Expand Down Expand Up @@ -429,11 +440,13 @@ public interface Configuration {
boolean isOracleSqlplus();

/**
* Flyway's license key.
* Your Flyway license key (FL01...). Not yet a Flyway Pro or Enterprise Edition customer?
* Request your <a href="https://flywaydb.org/download/">Flyway trial license key</a>
* to try out Flyway Pro and Enterprise Edition features free for 30 days.
*
* <p><i>Flyway Pro and Flyway Enterprise only</i></p>
*
* @return The license key.
* @return Your Flyway license key.
*/
String getLicenseKey();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.flywaydb.core.api.Location;
import org.flywaydb.core.api.MigrationVersion;
import org.flywaydb.core.api.callback.Callback;
import org.flywaydb.core.api.migration.JavaMigration;
import org.flywaydb.core.api.resolver.MigrationResolver;

import javax.sql.DataSource;
Expand Down Expand Up @@ -138,6 +139,11 @@ public String[] getSqlMigrationSuffixes() {
return config.getSqlMigrationSuffixes();
}

@Override
public JavaMigration[] getJavaMigrations() {
return config.getJavaMigrations();
}

@Override
public boolean isIgnoreMissingMigrations() {
return config.isIgnoreMissingMigrations();
Expand Down Expand Up @@ -683,6 +689,19 @@ public FluentConfiguration sqlMigrationSuffixes(String... sqlMigrationSuffixes)
return this;
}

/**
* The manually added Java-based migrations. These are not Java-based migrations discovered through classpath
* scanning and instantiated by Flyway. Instead these are manually added instances of JavaMigration.
* This is particularly useful when working with a dependency injection container, where you may want the DI
* container to instantiate the class and wire up its dependencies for you.
*
* @param javaMigrations The manually added Java-based migrations. An empty array if none. (default: none)
*/
public FluentConfiguration javaMigrations(JavaMigration... javaMigrations) {
config.setJavaMigrations(javaMigrations);
return this;
}

/**
* Sets the datasource to use. Must have the necessary privileges to execute ddl.
*
Expand Down Expand Up @@ -904,11 +923,13 @@ public FluentConfiguration oracleSqlplus(boolean oracleSqlplus) {
}

/**
* Flyway's license key.
* Your Flyway license key (FL01...). Not yet a Flyway Pro or Enterprise Edition customer?
* Request your <a href="https://flywaydb.org/download/">Flyway trial license key</a>
* to try out Flyway Pro and Enterprise Edition features free for 30 days.
*
* <p><i>Flyway Pro and Flyway Enterprise only</i></p>
*
* @param licenseKey The license key.
* @param licenseKey Your Flyway license key.
*/
public FluentConfiguration licenseKey(String licenseKey) {
config.setLicenseKey(licenseKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
import org.flywaydb.core.internal.callback.CallbackExecutor;
import org.flywaydb.core.internal.clazz.ClassProvider;
import org.flywaydb.core.internal.database.base.Database;
import org.flywaydb.core.internal.resolver.java.JavaMigrationResolver;
import org.flywaydb.core.internal.resolver.java.FixedJavaMigrationResolver;
import org.flywaydb.core.internal.resolver.java.ScanningJavaMigrationResolver;
import org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver;
import org.flywaydb.core.internal.resource.ResourceProvider;
import org.flywaydb.core.internal.sqlscript.SqlStatementBuilderFactory;
Expand Down Expand Up @@ -78,8 +79,9 @@ public CompositeMigrationResolver(Database database,


, configuration));
migrationResolvers.add(new JavaMigrationResolver(classProvider, configuration));
migrationResolvers.add(new ScanningJavaMigrationResolver(classProvider, configuration));
}
migrationResolvers.add(new FixedJavaMigrationResolver(configuration.getJavaMigrations()));

migrationResolvers.addAll(Arrays.asList(customMigrationResolvers));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2010-2018 Boxfuse GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flywaydb.core.internal.resolver.java;

import org.flywaydb.core.api.migration.JavaMigration;
import org.flywaydb.core.api.resolver.Context;
import org.flywaydb.core.api.resolver.MigrationResolver;
import org.flywaydb.core.api.resolver.ResolvedMigration;
import org.flywaydb.core.internal.resolver.ResolvedMigrationComparator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Migration resolver for a fixed set of pre-instantiated Java-based migrations.
*/
public class FixedJavaMigrationResolver implements MigrationResolver {
/**
* The JavaMigration instances to use.
*/
private final JavaMigration[] javaMigrations;

/**
* Creates a new instance.
*
* @param javaMigrations The JavaMigration instances to use.
*/
public FixedJavaMigrationResolver(JavaMigration... javaMigrations) {
this.javaMigrations = javaMigrations;
}

@Override
public List<ResolvedMigration> resolveMigrations(Context context) {
List<ResolvedMigration> migrations = new ArrayList<>();

for (JavaMigration javaMigration : javaMigrations) {
migrations.add(new ResolvedJavaMigration(javaMigration));
}

Collections.sort(migrations, new ResolvedMigrationComparator());
return migrations;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2018 Boxfuse GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flywaydb.core.internal.resolver.java;

import org.flywaydb.core.api.MigrationType;
import org.flywaydb.core.api.migration.JavaMigration;
import org.flywaydb.core.internal.resolver.ResolvedMigrationImpl;
import org.flywaydb.core.internal.util.ClassUtils;

/**
* A resolved Java migration.
*/
public class ResolvedJavaMigration extends ResolvedMigrationImpl {
/**
* Creates a new ResolvedJavaMigration based on this JavaMigration.
*
* @param javaMigration The JavaMigration to use.
*/
public ResolvedJavaMigration(JavaMigration javaMigration) {
setVersion(javaMigration.getVersion());
setDescription(javaMigration.getDescription());
setScript(javaMigration.getClass().getName());
setChecksum(javaMigration.getChecksum());
setType(



MigrationType.JDBC
);
setPhysicalLocation(ClassUtils.getLocationOnDisk(javaMigration.getClass()));
setExecutor(new JavaMigrationExecutor(javaMigration));
}
}
Loading

0 comments on commit 62f729b

Please sign in to comment.