public class Flyway extends Object implements FlywayConfiguration
It is THE public API from which all important Flyway functions such as clean, validate and migrate can be called.
Constructor and Description |
---|
Flyway()
Creates a new instance of Flyway.
|
Modifier and Type | Method and Description |
---|---|
void |
baseline()
Baselines an existing database, excluding all migrations up to and including baselineVersion.
|
void |
clean()
Drops all objects (tables, views, procedures, triggers, ...) in the configured schemas.
|
void |
configure(Map<String,String> props)
Configures Flyway with these properties.
|
void |
configure(Properties properties)
Configures Flyway with these properties.
|
String |
getBaselineDescription()
Retrieves the description to tag an existing schema with when executing baseline.
|
MigrationVersion |
getBaselineVersion()
Retrieves the version to tag an existing schema with when executing baseline.
|
FlywayCallback[] |
getCallbacks()
Gets the callbacks for lifecycle notifications.
|
ClassLoader |
getClassLoader()
Retrieves the ClassLoader to use for resolving migrations on the classpath.
|
DataSource |
getDataSource()
Retrieves the dataSource to use to access the database.
|
String |
getEncoding()
Retrieves the encoding of Sql migrations.
|
String |
getInstalledBy()
The username that will be recorded in the metadata table as having applied the migration.
|
String[] |
getLocations()
Retrieves the locations to scan recursively for migrations.
|
String |
getPlaceholderPrefix()
Retrieves the prefix of every placeholder.
|
Map<String,String> |
getPlaceholders()
Retrieves the map of <placeholder, replacementValue> to apply to sql migration scripts.
|
String |
getPlaceholderSuffix()
Retrieves the suffix of every placeholder.
|
String |
getRepeatableSqlMigrationPrefix()
Retrieves the file name prefix for repeatable sql migrations.
|
MigrationResolver[] |
getResolvers()
Retrieves the The custom MigrationResolvers to be used in addition to the built-in ones for resolving Migrations to apply.
|
String[] |
getSchemas()
Retrieves the schemas managed by Flyway.
|
String |
getSqlMigrationPrefix()
Retrieves the file name prefix for sql migrations.
|
String |
getSqlMigrationSeparator()
Retrieves the file name separator for sql migrations.
|
String |
getSqlMigrationSuffix()
Retrieves the file name suffix for sql migrations.
|
String |
getTable()
Retrieves the name of the schema metadata table that will be used by Flyway.
|
MigrationVersion |
getTarget()
Retrieves the target version up to which Flyway should consider migrations.
|
MigrationInfoService |
info()
Retrieves the complete information about all the migrations including applied, pending and current migrations with
details and status.
|
boolean |
isAllowMixedMigrations()
Deprecated.
Use
isMixed() instead. Will be removed in Flyway 5.0. |
boolean |
isBaselineOnMigrate()
Whether to automatically call baseline when migrate is executed against a non-empty schema with no metadata table.
|
boolean |
isCleanDisabled()
Whether to disable clean.
|
boolean |
isCleanOnValidationError()
Whether to automatically call clean or not when a validation error occurs.
|
boolean |
isGroup()
Whether to group all pending migrations together in the same transaction when applying them (only recommended for databases with support for DDL transactions).
|
boolean |
isIgnoreFailedFutureMigration()
Deprecated.
Use the more generic
isIgnoreFutureMigration() instead. Will be removed in Flyway 5.0. |
boolean |
isIgnoreFutureMigrations()
Ignore future migrations when reading the metadata table.
|
boolean |
isIgnoreMissingMigrations()
Ignore missing migrations when reading the metadata table.
|
boolean |
isMixed()
Whether to allow mixing transactional and non-transactional statements within the same migration.
|
boolean |
isOutOfOrder()
Allows migrations to be run "out of order".
|
boolean |
isPlaceholderReplacement()
Checks whether placeholders should be replaced.
|
boolean |
isSkipDefaultCallbacks()
Whether Flyway should skip the default callbacks.
|
boolean |
isSkipDefaultResolvers()
Whether Flyway should skip the default resolvers.
|
boolean |
isValidateOnMigrate()
Whether to automatically call validate or not when running migrate.
|
int |
migrate()
Starts the database migration.
|
void |
repair()
Repairs the Flyway metadata table.
|
void |
setAllowMixedMigrations(boolean allowMixedMigrations)
Deprecated.
Use
setMixed() instead. Will be removed in Flyway 5.0. |
void |
setBaselineDescription(String baselineDescription)
Sets the description to tag an existing schema with when executing baseline.
|
void |
setBaselineOnMigrate(boolean baselineOnMigrate)
Whether to automatically call baseline when migrate is executed against a non-empty schema with no metadata table.
|
void |
setBaselineVersion(MigrationVersion baselineVersion)
Sets the version to tag an existing schema with when executing baseline.
|
void |
setBaselineVersionAsString(String baselineVersion)
Sets the version to tag an existing schema with when executing baseline.
|
void |
setCallbacks(FlywayCallback... callbacks)
Set the callbacks for lifecycle notifications.
|
void |
setCallbacksAsClassNames(String... callbacks)
Set the callbacks for lifecycle notifications.
|
void |
setClassLoader(ClassLoader classLoader)
Sets the ClassLoader to use for resolving migrations on the classpath.
|
void |
setCleanDisabled(boolean cleanDisabled)
Whether to disable clean.
|
void |
setCleanOnValidationError(boolean cleanOnValidationError)
Whether to automatically call clean or not when a validation error occurs.
|
void |
setDataSource(DataSource dataSource)
Sets the datasource to use.
|
void |
setDataSource(String url,
String user,
String password,
String... initSqls)
Sets the datasource to use.
|
void |
setEncoding(String encoding)
Sets the encoding of Sql migrations.
|
void |
setGroup(boolean group)
Whether to group all pending migrations together in the same transaction when applying them (only recommended for databases with support for DDL transactions).
|
void |
setIgnoreFailedFutureMigration(boolean ignoreFailedFutureMigration)
Deprecated.
Use the more generic
setIgnoreFutureMigrations() instead. Will be removed in Flyway 5.0. |
void |
setIgnoreFutureMigrations(boolean ignoreFutureMigrations)
Whether to ignore future migrations when reading the metadata table.
|
void |
setIgnoreMissingMigrations(boolean ignoreMissingMigrations)
Ignore missing migrations when reading the metadata table.
|
void |
setInstalledBy(String installedBy)
The username that will be recorded in the metadata table as having applied the migration.
|
void |
setLocations(String... locations)
Sets the locations to scan recursively for migrations.
|
void |
setMixed(boolean mixed)
Whether to allow mixing transactional and non-transactional statements within the same migration.
|
void |
setOutOfOrder(boolean outOfOrder)
Allows migrations to be run "out of order".
|
void |
setPlaceholderPrefix(String placeholderPrefix)
Sets the prefix of every placeholder.
|
void |
setPlaceholderReplacement(boolean placeholderReplacement)
Sets whether placeholders should be replaced.
|
void |
setPlaceholders(Map<String,String> placeholders)
Sets the placeholders to replace in sql migration scripts.
|
void |
setPlaceholderSuffix(String placeholderSuffix)
Sets the suffix of every placeholder.
|
void |
setRepeatableSqlMigrationPrefix(String repeatableSqlMigrationPrefix)
Sets the file name prefix for repeatable sql migrations.
|
void |
setResolvers(MigrationResolver... resolvers)
Sets custom MigrationResolvers to be used in addition to the built-in ones for resolving Migrations to apply.
|
void |
setResolversAsClassNames(String... resolvers)
Sets custom MigrationResolvers to be used in addition to the built-in ones for resolving Migrations to apply.
|
void |
setSchemas(String... schemas)
Sets the schemas managed by Flyway.
|
void |
setSkipDefaultCallbacks(boolean skipDefaultCallbacks)
Whether Flyway should skip the default callbacks.
|
void |
setSkipDefaultResolvers(boolean skipDefaultResolvers)
Whether Flyway should skip the default resolvers.
|
void |
setSqlMigrationPrefix(String sqlMigrationPrefix)
Sets the file name prefix for sql migrations.
|
void |
setSqlMigrationSeparator(String sqlMigrationSeparator)
Sets the file name separator for sql migrations.
|
void |
setSqlMigrationSuffix(String sqlMigrationSuffix)
Sets the file name suffix for sql migrations.
|
void |
setTable(String table)
Sets the name of the schema metadata table that will be used by Flyway.
|
void |
setTarget(MigrationVersion target)
Sets the target version up to which Flyway should consider migrations.
|
void |
setTargetAsString(String target)
Sets the target version up to which Flyway should consider migrations.
|
void |
setValidateOnMigrate(boolean validateOnMigrate)
Whether to automatically call validate or not when running migrate.
|
void |
validate()
Validate applied migrations against resolved ones (on the filesystem or classpath)
to detect accidental changes that may prevent the schema(s) from being recreated exactly.
|
public Flyway()
public String[] getLocations()
FlywayConfiguration
The location type is determined by its prefix.
Unprefixed locations or locations starting with classpath:
point to a package on the classpath and may
contain both sql and java-based migrations.
Locations starting with filesystem:
point to a directory on the filesystem and may only contain sql
migrations.
getLocations
in interface FlywayConfiguration
public String getEncoding()
FlywayConfiguration
getEncoding
in interface FlywayConfiguration
public String[] getSchemas()
FlywayConfiguration
Consequences:
getSchemas
in interface FlywayConfiguration
public String getTable()
FlywayConfiguration
Retrieves the name of the schema metadata table that will be used by Flyway.
By default (single-schema mode) the metadata table is placed in the default schema for the connection provided by the datasource.
When the flyway.schemas property is set (multi-schema mode), the metadata table is placed in the first schema of the list.
getTable
in interface FlywayConfiguration
public MigrationVersion getTarget()
FlywayConfiguration
current
designates the current version of the schema.getTarget
in interface FlywayConfiguration
public boolean isPlaceholderReplacement()
FlywayConfiguration
isPlaceholderReplacement
in interface FlywayConfiguration
public Map<String,String> getPlaceholders()
FlywayConfiguration
getPlaceholders
in interface FlywayConfiguration
public String getPlaceholderPrefix()
FlywayConfiguration
getPlaceholderPrefix
in interface FlywayConfiguration
public String getPlaceholderSuffix()
FlywayConfiguration
getPlaceholderSuffix
in interface FlywayConfiguration
public String getSqlMigrationPrefix()
FlywayConfiguration
Sql migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix , which using the defaults translates to V1_1__My_description.sql
getSqlMigrationPrefix
in interface FlywayConfiguration
public String getRepeatableSqlMigrationPrefix()
FlywayConfiguration
Repeatable sql migrations have the following file name structure: prefixSeparatorDESCRIPTIONsuffix , which using the defaults translates to R__My_description.sql
getRepeatableSqlMigrationPrefix
in interface FlywayConfiguration
public String getSqlMigrationSeparator()
FlywayConfiguration
Sql migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix , which using the defaults translates to V1_1__My_description.sql
getSqlMigrationSeparator
in interface FlywayConfiguration
public String getSqlMigrationSuffix()
FlywayConfiguration
Sql migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix , which using the defaults translates to V1_1__My_description.sql
getSqlMigrationSuffix
in interface FlywayConfiguration
public boolean isIgnoreMissingMigrations()
FlywayConfiguration
isIgnoreMissingMigrations
in interface FlywayConfiguration
true
to continue normally and log a warning, false
to fail fast with an exception.
(default: false
)public boolean isIgnoreFutureMigrations()
FlywayConfiguration
isIgnoreFutureMigrations
in interface FlywayConfiguration
true
to continue normally and log a warning, false
to fail fast with an exception.
(default: true
)@Deprecated public boolean isIgnoreFailedFutureMigration()
isIgnoreFutureMigration()
instead. Will be removed in Flyway 5.0.true
to terminate normally and log a warning, false
to fail fast with an exception.
(default: false
)public boolean isValidateOnMigrate()
FlywayConfiguration
isValidateOnMigrate
in interface FlywayConfiguration
true
if validate should be called. false
if not. (default: true
)public boolean isCleanOnValidationError()
FlywayConfiguration
This is exclusively intended as a convenience for development. Even tough we strongly recommend not to change migration scripts once they have been checked into SCM and run, this provides a way of dealing with this case in a smooth manner. The database will be wiped clean automatically, ensuring that the next migration will bring you back to the state checked into SCM.
Warning ! Do not enable in production !
isCleanOnValidationError
in interface FlywayConfiguration
true
if clean should be called. false
if not. (default: false
)public boolean isCleanDisabled()
FlywayConfiguration
This is especially useful for production environments where running clean can be quite a career limiting move.
isCleanDisabled
in interface FlywayConfiguration
true
to disabled clean. false
to leave it enabled. (default: false
)public MigrationVersion getBaselineVersion()
FlywayConfiguration
getBaselineVersion
in interface FlywayConfiguration
public String getBaselineDescription()
FlywayConfiguration
getBaselineDescription
in interface FlywayConfiguration
public boolean isBaselineOnMigrate()
FlywayConfiguration
Whether to automatically call baseline when migrate is executed against a non-empty schema with no metadata table.
This schema will then be initialized with the baselineVersion
before executing the migrations.
Only migrations above baselineVersion
will then be applied.
This is useful for initial Flyway production deployments on projects with an existing DB.
Be careful when enabling this as it removes the safety net that ensures Flyway does not migrate the wrong database in case of a configuration mistake!
isBaselineOnMigrate
in interface FlywayConfiguration
true
if baseline should be called on migrate for non-empty schemas, false
if not. (default: false
)public boolean isOutOfOrder()
FlywayConfiguration
If you already have versions 1 and 3 applied, and now a version 2 is found, it will be applied too instead of being ignored.
isOutOfOrder
in interface FlywayConfiguration
true
if outOfOrder migrations should be applied, false
if not. (default: false
)public MigrationResolver[] getResolvers()
FlywayConfiguration
getResolvers
in interface FlywayConfiguration
public boolean isSkipDefaultResolvers()
FlywayConfiguration
isSkipDefaultResolvers
in interface FlywayConfiguration
public DataSource getDataSource()
FlywayConfiguration
getDataSource
in interface FlywayConfiguration
public ClassLoader getClassLoader()
FlywayConfiguration
getClassLoader
in interface FlywayConfiguration
public boolean isMixed()
FlywayConfiguration
isMixed
in interface FlywayConfiguration
true
if mixed migrations should be allowed. false
if an error should be thrown instead. (default: false
)@Deprecated public boolean isAllowMixedMigrations()
isMixed()
instead. Will be removed in Flyway 5.0.FlywayConfiguration
isAllowMixedMigrations
in interface FlywayConfiguration
true
if mixed migrations should be allowed. false
if an error should be thrown instead. (default: false
)public String getInstalledBy()
FlywayConfiguration
getInstalledBy
in interface FlywayConfiguration
null
for the current database user of the connection. (default: null
).public boolean isGroup()
FlywayConfiguration
isGroup
in interface FlywayConfiguration
true
if migrations should be grouped. false
if they should be applied individually instead. (default: false
)public void setGroup(boolean group)
group
- true
if migrations should be grouped. false
if they should be applied individually instead. (default: false
)public void setInstalledBy(String installedBy)
installedBy
- The username or null
for the current database user of the connection. (default: null
).public void setMixed(boolean mixed)
mixed
- true
if mixed migrations should be allowed. false
if an error should be thrown instead. (default: false
)@Deprecated public void setAllowMixedMigrations(boolean allowMixedMigrations)
setMixed()
instead. Will be removed in Flyway 5.0.allowMixedMigrations
- true
if mixed migrations should be allowed. false
if an error should be thrown instead. (default: false
)public void setIgnoreMissingMigrations(boolean ignoreMissingMigrations)
ignoreMissingMigrations
- true
to continue normally and log a warning, false
to fail fast with an exception.
(default: false
)public void setIgnoreFutureMigrations(boolean ignoreFutureMigrations)
ignoreFutureMigrations
- true
to continue normally and log a warning, false
to fail
fast with an exception. (default: true
)@Deprecated public void setIgnoreFailedFutureMigration(boolean ignoreFailedFutureMigration)
setIgnoreFutureMigrations()
instead. Will be removed in Flyway 5.0.ignoreFailedFutureMigration
- true
to terminate normally and log a warning, false
to fail
fast with an exception. (default: false
)public void setValidateOnMigrate(boolean validateOnMigrate)
validateOnMigrate
- true
if validate should be called. false
if not. (default: true
)public void setCleanOnValidationError(boolean cleanOnValidationError)
This is exclusively intended as a convenience for development. Even tough we strongly recommend not to change migration scripts once they have been checked into SCM and run, this provides a way of dealing with this case in a smooth manner. The database will be wiped clean automatically, ensuring that the next migration will bring you back to the state checked into SCM.
Warning ! Do not enable in production !
cleanOnValidationError
- true
if clean should be called. false
if not. (default: false
)public void setCleanDisabled(boolean cleanDisabled)
This is especially useful for production environments where running clean can be quite a career limiting move.
cleanDisabled
- true
to disabled clean. false
to leave it enabled. (default: false
)public void setLocations(String... locations)
The location type is determined by its prefix.
Unprefixed locations or locations starting with classpath:
point to a package on the classpath and may
contain both sql and java-based migrations.
Locations starting with filesystem:
point to a directory on the filesystem and may only contain sql
migrations.
locations
- Locations to scan recursively for migrations. (default: db/migration)public void setEncoding(String encoding)
encoding
- The encoding of Sql migrations. (default: UTF-8)public void setSchemas(String... schemas)
Consequences:
schemas
- The schemas managed by Flyway. May not be null
. Must contain at least one element.public void setTable(String table)
Sets the name of the schema metadata table that will be used by Flyway.
By default (single-schema mode) the metadata table is placed in the default schema for the connection provided by the datasource.
When the flyway.schemas property is set (multi-schema mode), the metadata table is placed in the first schema of the list.
table
- The name of the schema metadata table that will be used by flyway. (default: schema_version)public void setTarget(MigrationVersion target)
target
- The target version up to which Flyway should consider migrations. (default: the latest version)public void setTargetAsString(String target)
target
- The target version up to which Flyway should consider migrations.
The special value current
designates the current version of the schema. (default: the latest
version)public void setPlaceholderReplacement(boolean placeholderReplacement)
placeholderReplacement
- Whether placeholders should be replaced. (default: true)public void setPlaceholders(Map<String,String> placeholders)
placeholders
- The map of <placeholder, replacementValue> to apply to sql migration scripts.public void setPlaceholderPrefix(String placeholderPrefix)
placeholderPrefix
- The prefix of every placeholder. (default: ${ )public void setPlaceholderSuffix(String placeholderSuffix)
placeholderSuffix
- The suffix of every placeholder. (default: } )public void setSqlMigrationPrefix(String sqlMigrationPrefix)
Sql migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix , which using the defaults translates to V1_1__My_description.sql
sqlMigrationPrefix
- The file name prefix for sql migrations (default: V)public void setRepeatableSqlMigrationPrefix(String repeatableSqlMigrationPrefix)
Repeatable sql migrations have the following file name structure: prefixSeparatorDESCRIPTIONsuffix , which using the defaults translates to R__My_description.sql
repeatableSqlMigrationPrefix
- The file name prefix for repeatable sql migrations (default: R)public void setSqlMigrationSeparator(String sqlMigrationSeparator)
Sql migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix , which using the defaults translates to V1_1__My_description.sql
sqlMigrationSeparator
- The file name separator for sql migrations (default: __)public void setSqlMigrationSuffix(String sqlMigrationSuffix)
Sql migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix , which using the defaults translates to V1_1__My_description.sql
sqlMigrationSuffix
- The file name suffix for sql migrations (default: .sql)public void setDataSource(DataSource dataSource)
dataSource
- The datasource to use. Must have the necessary privileges to execute ddl.public void setDataSource(String url, String user, String password, String... initSqls)
To use a custom ClassLoader, setClassLoader() must be called prior to calling this method.
url
- The JDBC URL of the database.user
- The user of the database.password
- The password of the database.initSqls
- The (optional) sql statements to execute to initialize a connection immediately after obtaining it.public void setClassLoader(ClassLoader classLoader)
classLoader
- The ClassLoader to use for resolving migrations on the classpath. (default: Thread.currentThread().getContextClassLoader() )public void setBaselineVersion(MigrationVersion baselineVersion)
baselineVersion
- The version to tag an existing schema with when executing baseline. (default: 1)public void setBaselineVersionAsString(String baselineVersion)
baselineVersion
- The version to tag an existing schema with when executing baseline. (default: 1)public void setBaselineDescription(String baselineDescription)
baselineDescription
- The description to tag an existing schema with when executing baseline. (default: << Flyway Baseline >>)public void setBaselineOnMigrate(boolean baselineOnMigrate)
Whether to automatically call baseline when migrate is executed against a non-empty schema with no metadata table.
This schema will then be baselined with the baselineVersion
before executing the migrations.
Only migrations above baselineVersion
will then be applied.
This is useful for initial Flyway production deployments on projects with an existing DB.
Be careful when enabling this as it removes the safety net that ensures Flyway does not migrate the wrong database in case of a configuration mistake!
baselineOnMigrate
- true
if baseline should be called on migrate for non-empty schemas, false
if not. (default: false
)public void setOutOfOrder(boolean outOfOrder)
If you already have versions 1 and 3 applied, and now a version 2 is found, it will be applied too instead of being ignored.
outOfOrder
- true
if outOfOrder migrations should be applied, false
if not. (default: false
)public FlywayCallback[] getCallbacks()
getCallbacks
in interface FlywayConfiguration
public boolean isSkipDefaultCallbacks()
FlywayConfiguration
isSkipDefaultCallbacks
in interface FlywayConfiguration
public void setCallbacks(FlywayCallback... callbacks)
callbacks
- The callbacks for lifecycle notifications. (default: none)public void setCallbacksAsClassNames(String... callbacks)
callbacks
- The fully qualified class names of the callbacks for lifecycle notifications. (default: none)public void setSkipDefaultCallbacks(boolean skipDefaultCallbacks)
skipDefaultCallbacks
- Whether default built-in callbacks should be skipped. (default: false)
public void setResolvers(MigrationResolver... resolvers)
resolvers
- The custom MigrationResolvers to be used in addition to the built-in ones for resolving Migrations to apply. (default: empty list)public void setResolversAsClassNames(String... resolvers)
resolvers
- The fully qualified class names of the custom MigrationResolvers to be used in addition to the built-in ones for resolving Migrations to apply. (default: empty list)public void setSkipDefaultResolvers(boolean skipDefaultResolvers)
skipDefaultResolvers
- Whether default built-in resolvers should be skipped. (default: false)
public int migrate() throws FlywayException
Starts the database migration. All pending migrations will be applied in order. Calling migrate on an up-to-date database has no effect.
FlywayException
- when the migration failed.public void validate() throws FlywayException
Validate applied migrations against resolved ones (on the filesystem or classpath) to detect accidental changes that may prevent the schema(s) from being recreated exactly.
Validation fails if
FlywayException
- when the validation failed.public void clean()
Drops all objects (tables, views, procedures, triggers, ...) in the configured schemas.
The schemas are cleaned in the order specified by the schemas
property.
FlywayException
- when the clean fails.public MigrationInfoService info()
Retrieves the complete information about all the migrations including applied, pending and current migrations with details and status.
FlywayException
- when the info retrieval failed.public void baseline() throws FlywayException
Baselines an existing database, excluding all migrations up to and including baselineVersion.
FlywayException
- when the schema baselining failed.public void repair() throws FlywayException
FlywayException
- when the metadata table repair failed.public void configure(Properties properties)
To use a custom ClassLoader, setClassLoader() must be called prior to calling this method.
properties
- Properties used for configuration.FlywayException
- when the configuration failed.public void configure(Map<String,String> props)
To use a custom ClassLoader, setClassLoader() must be called prior to calling this method.
props
- Properties used for configuration.FlywayException
- when the configuration failed.Copyright © 2017. All rights reserved.