Database migration tool that enables versioned database schema evolution with simple SQL scripts and Java migrations
—
Comprehensive configuration system providing fluent API for customizing all aspects of Flyway's migration behavior, from database connections to migration discovery and execution patterns.
Builder pattern interface for constructing Flyway configurations with method chaining and type safety.
/**
* Fluent configuration that can be customized and used to create a Flyway instance
*/
public class FluentConfiguration {
/** Create Flyway instance from this configuration */
public Flyway load();
/** Configure the ClassLoader to use for loading migrations and JDBC drivers */
public FluentConfiguration classLoader(ClassLoader classLoader);
}Configure database connections and schema management settings.
public class FluentConfiguration {
/** Configure database connection using URL, username and password */
public FluentConfiguration dataSource(String url, String user, String password);
/** Configure database connection using existing DataSource */
public FluentConfiguration dataSource(DataSource dataSource);
/** Configure database connection using JDBC driver and properties */
public FluentConfiguration driver(String driver);
public FluentConfiguration url(String url);
public FluentConfiguration user(String user);
public FluentConfiguration password(String password);
/** Set the schemas managed by Flyway */
public FluentConfiguration schemas(String... schemas);
/** Set the default schema containing the schema history table */
public FluentConfiguration defaultSchema(String defaultSchema);
/** Set the name of the schema history table */
public FluentConfiguration table(String table);
}Usage Examples:
// Basic database configuration
Flyway flyway = Flyway.configure()
.dataSource("jdbc:postgresql://localhost:5432/mydb", "user", "password")
.schemas("public", "migration_schema")
.load();
// Using existing DataSource
DataSource dataSource = getApplicationDataSource();
Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.defaultSchema("public")
.table("flyway_schema_history")
.load();
// Multiple database support
Flyway flyway = Flyway.configure()
.driver("org.postgresql.Driver")
.url("jdbc:postgresql://localhost:5432/mydb")
.user("flyway_user")
.password("flyway_pass")
.schemas("schema1", "schema2", "schema3")
.load();Configure how Flyway discovers and processes migration files and Java migrations.
public class FluentConfiguration {
/** Set the locations to scan for migrations */
public FluentConfiguration locations(String... locations);
public FluentConfiguration locations(Location... locations);
/** Set the character encoding of SQL migration files */
public FluentConfiguration encoding(Charset encoding);
public FluentConfiguration encoding(String encoding);
/** Set the file name prefix for SQL migrations */
public FluentConfiguration sqlMigrationPrefix(String sqlMigrationPrefix);
/** Set the file name suffix for SQL migrations */
public FluentConfiguration sqlMigrationSuffix(String sqlMigrationSuffix);
/** Set the file name suffixes for SQL migrations */
public FluentConfiguration sqlMigrationSuffixes(String... sqlMigrationSuffixes);
/** Set the file name separator for SQL migrations */
public FluentConfiguration sqlMigrationSeparator(String sqlMigrationSeparator);
/** Set the file name prefix for repeatable SQL migrations */
public FluentConfiguration repeatableSqlMigrationPrefix(String repeatableSqlMigrationPrefix);
}Usage Examples:
// Migration file discovery
Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.locations("classpath:db/migration", "filesystem:/path/to/migrations")
.sqlMigrationPrefix("V")
.sqlMigrationSeparator("__")
.sqlMigrationSuffixes(".sql", ".SQL")
.encoding("UTF-8")
.load();
// Custom migration patterns
Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.locations("classpath:db/migration", "classpath:db/special")
.sqlMigrationPrefix("Migration_")
.repeatableSqlMigrationPrefix("R__")
.encoding(StandardCharsets.UTF_8)
.load();Configure version management, baseline settings, and migration targeting.
public class FluentConfiguration {
/** Set the target version up to which Flyway should consider migrations */
public FluentConfiguration target(MigrationVersion target);
public FluentConfiguration target(String target);
/** Set the version to tag an existing schema with when executing baseline */
public FluentConfiguration baselineVersion(MigrationVersion baselineVersion);
public FluentConfiguration baselineVersion(String baselineVersion);
/** Set the description to tag an existing schema with when executing baseline */
public FluentConfiguration baselineDescription(String baselineDescription);
/** Whether to automatically call baseline when migrate is executed against a non-empty schema */
public FluentConfiguration baselineOnMigrate(boolean baselineOnMigrate);
/** Whether to allow mixing transactional and non-transactional statements within the same migration */
public FluentConfiguration mixed(boolean mixed);
/** Whether to group all pending migrations together in the same transaction when possible */
public FluentConfiguration group(boolean group);
}Usage Examples:
// Version targeting
Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.target(MigrationVersion.fromVersion("2.5"))
.baselineVersion("1.0")
.baselineDescription("Initial baseline")
.baselineOnMigrate(true)
.load();
// Transaction management
Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.mixed(true) // Allow mixed DDL/DML
.group(false) // Execute each migration in separate transaction
.load();Configure migration validation behavior and error handling strategies.
public class FluentConfiguration {
/** Whether to automatically call validate when running migrate */
public FluentConfiguration validateOnMigrate(boolean validateOnMigrate);
/** Whether to allow out of order migrations */
public FluentConfiguration outOfOrder(boolean outOfOrder);
/** Whether to skip executing migrations */
public FluentConfiguration skipExecutingMigrations(boolean skipExecutingMigrations);
/** Whether to ignore missing migrations when validating */
public FluentConfiguration ignoreMissingMigrations(boolean ignoreMissingMigrations);
/** Whether to ignore ignored migrations when validating */
public FluentConfiguration ignoreIgnoredMigrations(boolean ignoreIgnoredMigrations);
/** Whether to ignore pending migrations when validating */
public FluentConfiguration ignorePendingMigrations(boolean ignorePendingMigrations);
/** Whether to ignore future migrations when validating */
public FluentConfiguration ignoreFutureMigrations(boolean ignoreFutureMigrations);
/** Ignore migrations that match these patterns during validation */
public FluentConfiguration ignoreMigrationPatterns(ValidatePattern... ignoreMigrationPatterns);
public FluentConfiguration ignoreMigrationPatterns(String... ignoreMigrationPatterns);
}Usage Examples:
// Validation configuration
Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.validateOnMigrate(true)
.outOfOrder(false)
.ignoreMissingMigrations(false)
.ignoreFutureMigrations(true)
.ignoreMigrationPatterns("*:missing", "*:future")
.load();
// Development mode settings
Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.outOfOrder(true)
.skipExecutingMigrations(false)
.ignorePendingMigrations(true)
.load();Configure schema creation and management behavior.
public class FluentConfiguration {
/** Whether Flyway should attempt to create the schemas specified in the schemas property */
public FluentConfiguration createSchemas(boolean createSchemas);
/** Whether to disable clean */
public FluentConfiguration cleanDisabled(boolean cleanDisabled);
/** Whether to disable the removal of empty schema during clean */
public FluentConfiguration cleanOnValidationError(boolean cleanOnValidationError);
}Usage Examples:
// Schema management
Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.schemas("app_schema", "audit_schema")
.createSchemas(true)
.cleanDisabled(false) // Enable clean for testing
.load();Configure lifecycle callbacks and plugin extensions.
public class FluentConfiguration {
/** Set the callbacks for lifecycle notifications */
public FluentConfiguration callbacks(Callback... callbacks);
public FluentConfiguration callbacks(String... callbacks);
/** Set the custom MigrationResolvers to be used in addition to the built-in ones */
public FluentConfiguration resolvers(MigrationResolver... resolvers);
public FluentConfiguration resolvers(String... resolvers);
/** Set the custom ResourceProviders to be used */
public FluentConfiguration resourceProvider(ResourceProvider resourceProvider);
/** Set the custom ClassProviders to be used */
public FluentConfiguration classProvider(ClassProvider classProvider);
}Usage Examples:
// Callback configuration
Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.callbacks(new MyCustomCallback(), new AuditCallback())
.resolvers(new CustomMigrationResolver())
.resourceProvider(new S3ResourceProvider())
.load();
// String-based configuration
Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.callbacks("com.example.MyCallback", "com.example.AuditCallback")
.resolvers("com.example.CustomResolver")
.load();Read-only access to configuration values for use in migrations and callbacks.
public interface Configuration {
/** Get the ClassLoader to use for loading migrations and JDBC drivers */
ClassLoader getClassLoader();
/** Get the JDBC DataSource */
DataSource getDataSource();
/** Get the maximum number of retries when attempting to connect to the database */
int getConnectRetries();
/** Get the maximum time between retries when attempting to connect to the database in seconds */
int getConnectRetriesInterval();
/** Get the SQL statements to run to initialize a new database connection immediately after opening it */
String getInitSql();
/** Get the schemas managed by Flyway */
String[] getSchemas();
/** Get the name of the schema history table */
String getTable();
/** Get the target version up to which Flyway should consider migrations */
MigrationVersion getTarget();
/** Get the locations to scan recursively for migrations */
Location[] getLocations();
/** Get the encoding of SQL migration files */
Charset getEncoding();
/** Get the callbacks for lifecycle notifications */
Callback[] getCallbacks();
/** Get whether Flyway should attempt to create the schemas specified in the schemas property */
boolean isCreateSchemas();
/** Get whether to automatically call baseline when migrate is executed against a non-empty schema */
boolean isBaselineOnMigrate();
/** Get whether to automatically call validate when running migrate */
boolean isValidateOnMigrate();
/** Get whether clean is disabled */
boolean isCleanDisabled();
}Usage Examples:
// In a callback or migration
public class MyCallback extends BaseCallback {
@Override
public void handle(Event event, Context context) {
Configuration config = context.getConfiguration();
String[] schemas = config.getSchemas();
boolean createSchemas = config.isCreateSchemas();
System.out.println("Managing schemas: " + Arrays.toString(schemas));
System.out.println("Auto-create enabled: " + createSchemas);
}
}
// In a Java migration
public class V2__CustomMigration extends BaseJavaMigration {
@Override
public void migrate(Context context) throws Exception {
Configuration config = context.getConfiguration();
Location[] locations = config.getLocations();
// Use configuration information in migration logic
if (locations.length > 1) {
// Handle multiple location scenario
}
}
}Plugin system for extending configuration with custom properties.
public interface ConfigurationExtension {
/** The namespace for this configuration extension */
String getNamespace();
/** Configuration properties specific to this extension */
Map<String, String> getConfiguration();
}
// Access extensions via Configuration
public interface Configuration {
/** Get plugin register for accessing extensions */
PluginRegister getPluginRegister();
}Usage Examples:
// Custom configuration extension
public class MyConfigExtension implements ConfigurationExtension {
@Override
public String getNamespace() {
return "mycompany";
}
@Override
public Map<String, String> getConfiguration() {
Map<String, String> config = new HashMap<>();
config.put("customProperty", "customValue");
return config;
}
}
// Access in Flyway
Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.load();
MyConfigExtension ext = flyway.getConfigurationExtension(MyConfigExtension.class);
String customValue = ext.getConfiguration().get("customProperty");Install with Tessl CLI
npx tessl i tessl/maven-org-flywaydb--flyway-core