Command-line
Published 16 November 2022
Command-line
The Flyway command-line tool is a standalone Flyway distribution. It runs on Windows, macOS and Linux and it is primarily meant for users who wish to migrate their database from the command-line without having to integrate Flyway into their applications nor having to install a build tool.
Download and installation
Teams/Enterprise Edition
Windows
flyway-commandline-9.19.1-windows-x64.zip
md5
sha1
Extract the archive and simply add the new `flyway-9.19.1` directory to the `PATH` to make the `flyway` command available from anywhere on your system.
macOS
flyway-commandline-9.19.1-macosx-x64.tar.gz md5 sha1
Linux
Download, extract and install by adding to PATH
(requires sudo
permissions):
$ wget -qO- https://download.red-gate.com/maven/release/org/flywaydb/enterprise/flyway-commandline/9.19.1/flyway-commandline-9.19.1-linux-x64.tar.gz | tar -xvz && sudo ln -s `pwd`/flyway-9.19.1/flyway /usr/local/bin
Or simply download the archive:
flyway-commandline-9.19.1-linux-x64.tar.gz
md5
sha1
Docker
(Linux only) Download, extract and install by adding to PATH
(requires sudo
permissions):
$ sudo sh -c 'echo "docker run --rm redgate/flyway:9.19.1 $*" > /usr/local/bin/flyway && chmod +x /usr/local/bin/flyway'
(All platforms) Or simply download the image:
> docker pull redgate/flyway:9.19.1
Go to Docker Hub for detailed usage instructions.
Community Edition
Windows
flyway-commandline-9.19.1-windows-x64.zip
md5
sha1
Extract the archive and simply add the new `flyway-9.19.1` directory to the `PATH` to make the `flyway` command available from anywhere on your system.
macOS
flyway-commandline-9.19.1-macosx-x64.tar.gz
md5
sha1
Linux
Download, extract and install by adding to PATH
(requires sudo
permissions):
$ wget -qO- https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/9.19.1/flyway-commandline-9.19.1-linux-x64.tar.gz | tar -xvz && sudo ln -s `pwd`/flyway-9.19.1/flyway /usr/local/bin
Or simply download the archive:
flyway-commandline-9.19.1-linux-x64.tar.gz
md5
sha1
Docker
(Linux only) Download, extract and install by adding to PATH
(requires sudo
permissions):
$ sudo sh -c 'echo "docker run --rm flyway/flyway:9.19.1 $*" > /usr/local/bin/flyway && chmod +x /usr/local/bin/flyway'
(All platforms) Or simply download the image:
> docker pull flyway/flyway:9.19.1
Go to Docker Hub for detailed usage instructions.
Older versions, packages without JRE and sources are available from Maven Central
Older Docker images are available from boxfuse/flyway
Directory structure
The Flyway download, once extracted, now becomes a directory with the following structure:
flyway-9.19.1 conf flyway.conf configuration file drivers JDBC drivers jars Java-based migrations (as jars) jre lib licenses sql SQL migrations flyway macOS/Linux executable flyway.cmd Windows executable
Usage
> flyway [options] command
Flyway editions
The Flyway Command-line tool distribution ships with all editions of Flyway. It auto-detects which edition to run based on any license keys you configure, however it can easily be configured to run the [paid editions]https://flywaydb.org/download specifically.
Environment variable
One way to switch between the various Flyway editions is to set the FLYWAY_EDITION
environment variable prior to
executing Flyway to any of the following values:
community | Select the Flyway Community Edition |
teams | Select the Flyway Teams Edition |
Help flags
The following flags provide helpful information without carrying out any other operations:
--help -h -? | Print the list of available commands and options |
--version -v | Print the Flyway version |
Commands
Name | Description |
---|---|
migrate | Migrates the database |
clean | Drops all objects in the configured schemas |
info | Prints the details and status information about all the migrations |
validate | Validates the applied migrations against the ones available on the classpath |
undo Flyway Teams | Undoes the most recently applied versioned migrations |
baseline | Baselines an existing database, excluding all migrations up to and including baselineVersion |
repair | Repairs the schema history table |
JDBC drivers
In order to connect with your database, Flyway needs the appropriate JDBC driver to be available in its drivers
directory.
To see if Flyway ships with the JDBC driver for your database, visit the Driver section of the documentation page for your database. For example, here is the Oracle Drivers section.
If Flyway does not ship with the JDBC driver, you will need to download the driver and place it in the drivers
directory yourself. Instructions on where to download drivers from are also in the Driver section of the documentation page for each database, under Maven Central coordinates
.
Configuration
The Flyway Command-line tool can be configured in a wide variety of ways. You can use config files, environment
variables and command-line parameters (except for FLYWAY_EDITION
, which is not available in config files and
should be used as specified above). These different means of configuration can be combined at will.
Config files
Config files are supported by the Flyway command-line tool. If you are not familiar with them, check out the Flyway config file structure and settings reference first.
Flyway will search for and automatically load the following config files if present:
<install-dir>/conf/flyway.conf
<user-home>/flyway.conf
<current-dir>/flyway.conf
It is also possible to point Flyway at one or more additional config files. This is achieved by
supplying the command line parameter -configFiles=
as follows:
> flyway -configFiles=path/to/myAlternativeConfig.conf migrate
To pass in multiple files, separate their names with commas:
> flyway -configFiles=path/to/myAlternativeConfig.conf,other.conf migrate
Relative paths are relative to the current working directory. The special option -configFiles=-
reads from
standard input.
Alternatively you can also use the FLYWAY_CONFIG_FILES
environment variable for this.
When set it will take preference over the command-line parameter.
> export FLYWAY_CONFIG_FILES=path/to/myAlternativeConfig.conf,other.conf > flyway migrate
By default Flyway loads configuration files using UTF-8. To use an alternative encoding, use the command line parameter -configFileEncoding=
as follows:
> flyway -configFileEncoding=ISO-8859-1 migrate
Alternatively you can also use the FLYWAY_CONFIG_FILE_ENCODING
environment variable for this.
When set it will take preference over the command-line parameter.
> export FLYWAY_CONFIG_FILE_ENCODING=ISO-8859-1
Environment Variables
To make it easier to work with cloud and containerized environments, Flyway also supports configuration via environment variables. Check out the Flyway environment variable reference for details.
Command-line Arguments
Finally, Flyway can also be configured by passing arguments directly from the command-line:
> flyway -user=myuser -schemas=schema1,schema2 -placeholders.keyABC=valueXYZ migrate
A note on escaping command-line arguments
Some command-line arguments will need care as specific characters may be interpreted differently depending on the
shell you are working in. The url
parameter is particularly affected when it contains extra parameters with
equals =
and ampersands &
. For example:
bash, macOS terminal and Windows cmd: use double-quotes:
> flyway info -url="jdbc:snowflake://ab12345.snowflakecomputing.com/?db=demo_db&user=foo"
Powershell: use double-quotes inside single-quotes:
> ./flyway info -url='"jdbc:snowflake://ab12345.snowflakecomputing.com/?db=demo_db&user=foo"'
Configuration from standard input
You can provide configuration options to the standard input of the Flyway command line, using the ` -configFiles=-` option. Flyway will expect such configuration to be in the same format as a configuration file.
This allows you to compose Flyway with other operations. For instance, you can decrypt a config file containing login credentials and pipe it straight into Flyway.
Examples
Read a single option from echo
:
> echo $'flyway.url=jdbc:h2:mem:mydb' | flyway info -configFiles=-
Read multiple options from echo
, delimited by newlines:
> echo $'flyway.url=jdbc:h2:mem:mydb\nflyway.user=sa' | flyway info -configFiles=-
Use cat
to read a config file and pipe it directly into Flyway:
> cat flyway.conf | flyway migrate -configFiles=-
Use gpg
to encrypt a config file, then pipe it into Flyway.
Encrypt the config file:
> gpg -e -r "Your Name" flyway.conf
Decrypt the file and pipe it to Flyway:
> gpg -d -q flyway.conf.gpg | flyway info -configFiles=-
Overriding order
The Flyway command-line tool has been carefully designed to load and override configuration in a sensible order.
Settings are loaded in the following order (higher items in the list take precedence over lower ones):
- Command-line arguments
- Environment variables
- Standard input
- Custom config files
<current-dir>/flyway.conf
<user-home>/flyway.conf
<install-dir>/conf/flyway.conf
- Flyway command-line defaults
The means that if for example flyway.url
is both present in a config file and passed as -url=
from the command-line,
the command-line argument will take precedence and be used.
Credentials
If you do not supply a database user
or password
via any of the means above, you will be
prompted to enter them:
Database user: myuser Database password:
If you want Flyway to connect to your database without a user or password, you can suppress prompting by adding
the -n
flag.
There are exceptions, where the credentials are passed in the JDBC URL or where a password-less method of authentication is being used.
Java Arguments
If you need to to pass custom arguments to Flyway's JVM, you can do so by setting the JAVA_ARGS
environment variable.
They will then automatically be taken into account when launching Flyway. This is particularly useful when needing to set JVM system properties.
Output
By default, all debug, info and warning output is sent to stdout
. All errors are sent to stderr
.
Flyway will automatically detect and use any logger class that it finds on its classpath that derives from any of the following:
- the Apache Commons Logging framework
org.apache.commons.logging.Log
(including Log4j v1) - SLF4J
org.slf4j.Logger
- Log4J v2
org.apache.logging.log4j.Logger
Alternatively, you can use the loggers configuration parameter to specify an exact desired logging framework to use.
The simplest way to make use of Flyway's auto-detection is to put all the necessary JAR files in Flyway's lib
folder and any configuration in the Flyway root folder.
For example, if you wished to use log4j
v2 with the Flyway command line, you would achieve this by placing the log4j JAR files and the corresponding configuration file log4j2.xml
like this:
flyway-9.19.1 conf drivers jars jre lib log4j-api-2.17.1.jar log4j v2 jar log4j-core-2.17.1.jar log4j v2 jar licenses sql log4j2.xml log4j configuration
Similarly, to use Logback
add the relevant files like this:
flyway-9.19.1 conf drivers jars jre lib logback-classic.1.1.7.jar Logback jar logback-core-1.1.7.jar Logback jar slf4j-api-1.7.21.jar Logback dependency licenses sql logback.xml Logback configuration
If you are building Flyway into a larger application, this means you do not need to explicitly wire up any logging as it will auto-detect one of these frameworks.
P6Spy
P6Spy is another approach to logging which operates at the driver or datasource level, and Flyway has integration with this. You can read about setting it up here and configuring it here.
Colors
By default the output is automatically colorized if stdout
is associated with a terminal.
You can override this behavior with the -color
option. Possible values:
auto
(default) : Colorize output, unlessstdout
is not associated with a terminalalways
: Always colorize outputnever
: Never colorize output
Debug output
Add -X
to the argument list to also print debug output. If this gives you too much information, you can filter it
with normal command-line tools, for example:
bash, macOS terminal
> flyway migrate -X | grep -v 'term-to-filter-out'
Powershell
> flyway migrate -X | sls -Pattern 'term-to-filter-out' -NoMatch
Windows cmd
> flyway migrate -X | findstr /v /c:"term-to-filter-out"
Quiet mode
Add -q
to the argument list to suppress all output, except for errors and warnings.
Machine-readable output
Add -outputType=json
to the argument list to print JSON instead of human-readable output. Errors are included in the JSON payload instead of being sent to stderr
.
Writing to a file
Add -outputFile=/my/output.txt
to the argument list to also write output to the specified file.