Quickstart - Gradle
Published 16 November 2022
Prerequisites
- Java 17
- Gradle 3.0 or newer
Setting up the build file
Let's start by creating build.gradle
that integrates and configures Flyway to connect to H2:
buildscript { dependencies { classpath 'com.h2database:h2:2.2.224' } } plugins { id "org.flywaydb.flyway" version "10.0.0" } flyway { url = 'jdbc:h2:file:./target/foobar' user = 'sa' }
Creating the first migration
We create a first migration called src/main/resources/db/migration/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 our database:
> gradle flywayMigrate -i
If all went well, you should see the following output:
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.062s).
Adding a second migration
If we now add a second migration called src/main/resources/db/migration/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:
> gradle flywayMigrate -i
We now get:
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.090s).
Summary
In this brief tutorial we saw how to
- integrate the Flyway Gradle plugin into a project
- configure it so it can talk to our database
- write our first couple of migrations
These migrations were then successfully found and executed.