Flyway with MAVEN

Prerequisites

  • Have a project setup using MAVEN
  • The example will be connecting to a MYSQL database but FLYWAY can connect to several different types of databases

What is Quick Search?

Flyway is a tool used to manage a scheme across several developers. Using GIT or SVN flyway allows you to commit SQL that other developers will pull down and using the flyway:migrate command the needed SQL queries will be performed to their database. The process for getting FLYWAY stood up is fairy straight forward and for any further documentation can be viewed HERE.

Steps

First and for most lets add the FLYWAY plugin to our pom.xml file. So open up your pom.xml file and add the following snippet:

<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>3.2.1</version>
    <configuration>
        <url>jdbc:mysql://{IP_ADDRESS}:{PORT_NUMBER}/{DATABASE_NAME}</url>
        <user>{USERNAME}</user>
        <password>{PASSWORD}</password>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>
    </dependencies>
</plugin>

The above snippet is to connect to a MYSQL database however many different types of databases can be connected to. To connect to different databases you would need the change the dependency to use the correct connector for your db.

 

Replace:

  • {IP_ADDRESS} with the ip address of your database instance.
  • {PORT_NUMBER} with the port number to connect to your database instance.
  • {DATABASE_NAME} with your database name.
  • {USERNAME} with a user to connect to the database with.
  • {PASSWORD} with the password for the user to use to connect to the database.

Run

mvn clean package

To pull down any FLYWAY artifacts that aren’t in your local repository yet.

Next you need to create the directory:

db/migration

under your “resources” directory. This is the default location of where FLYWAY is looking for new scripts to be executed.

Next up run:

mvn flyway:baseline

This command will create a new table called “scheme_versions” that FLYWAY uses to know which scripts have and have not been executed.

 

Now lets test it out by adding a dummy file under the “db/migrations” ┬ácalled “V1_1__Create_dummy_table.sql” with the following content in it:

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

Notice the same of the script. All scripts in this directory must be called “V{VERSION_NUMBER}__{DESCRIPTION_OF_SQL_SCRIPT}.sql“. FLYWAY parses the version part of the file name to keep an index of which scripts have/havent been executed yet.

To run any pending migrations (any migrations in this directory that have yet to get applied to your database) run the following command:

mvn compile flyway:migrate

And there you have it. Your project is now integrated in with FLYWAY!

Go you!

 

 

 

 

Advertisements