Flyway

Quickstart - Command-line

This brief tutorial will teach how to get up and running with the Flyway Command-line tool. It will take you through the steps on how to configure it and how to write and execute your first few database migrations.

This tutorial should take you about 5 minutes to complete.

Note:  This Quickstart is using the Flyway Command-line.  Everything below can also be accomplished in the Flyway Desktop GUI.  If you prefer a GUI, please see this Quickstart Guide for Flyway Desktop.  

Prerequisites

Install Flyway.

Configuring Flyway

If you have downloaded the command line separately, unzip it to a flyway folder.

If you have installed Flyway using Flyway Desktop, copy the flyway CLI folder from the installation folder (eg "C:\Program Files\Red Gate\Flyway Desktop\flyway") to a location you have read/write access to. 


From a command prompt, jump into your flyway folder:

> cd flyway

Configure Flyway by editing /conf/flyway.toml, like this:


[flyway]
locations = ["filesystem:migrations"]


[environments.default]
locations = ["filesystem:migrations"]
url = "jdbc:h2:mem:db"
user = ""
password = ""

 

Creating the first migration

Now create your first migration in the /migrations directory called V1__Create_person_table.sql:

create table PERSON (
    ID int not null,
    NAME varchar(100) not null
);

Migrating the database

It's now time to execute Flyway to migrate your database:

flyway> flyway migrate

If all went well, you should see the following output:

Database: jdbc:sqlite:FlywayQuickStartCLI.db (SQLite 3.41)
Successfully validated 1 migration (execution time 00:00.008s) Creating Schema History table: "PUBLIC"."flyway_schema_history" Current version of schema "PUBLIC": << Empty Schema >> Migrating schema "PUBLIC" to version 1 - Create person table Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.033s)

Adding a second migration

If you now add a second migration to the /sql directory called V2__Add_people.sql:

insert into PERSON (ID, NAME) values (1, 'Axel');
insert into PERSON (ID, NAME) values (2, 'Mr. Foo');
insert into PERSON (ID, NAME) values (3, 'Ms. Bar');

and execute it by issuing:

flyway> flyway migrate

You now get:

Database: jdbc:sqlite:FlywayQuickStartCLI.db (SQLite 3.41)
Successfully validated 2 migrations (execution time 00:00.018s) Current version of schema "PUBLIC": 1 Migrating schema "PUBLIC" to version 2 - Add people Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.016s)

Summary

In this brief tutorial we saw how to:

  • install the Flyway Command-line tool
  • configure it so it can talk to our database
  • write our first couple of migrations

These migrations were then successfully found and executed.


Didn't find what you were looking for?