Database migration with Flyway

Flyway in an extremely convenient database migraton tool.
First and foremost it integrates wonderfully with maven.
But one of its biggest assets is the ability to run both sql migration scripts and java migration scripts.

Let us start with a simple maven project

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>migration</groupId>
    <artifactId>com.gkatzioura</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <flyway.version>3.1</flyway.version>
        <mysql.driver.version>5.1.33</mysql.driver.version>
        <database.url>{your jdbc url}</database.url>
        <database.user>{your database user}</database.user>
        <databese.password>{your database password}</databese.password>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
            <version>${flyway.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-maven-plugin</artifactId>
                <version>${flyway.version}</version>
                <configuration>
                    <baselineOnMigrate>true</baselineOnMigrate>
                    <url>${database.url}</url>
                    <user>${database.user}</user>
                    <password>${databese.password}</password>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.driver.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

When you issue through maven

mvn flyway:migrate

Then flyway will will lookup on the db/migration folder of your target to find any migration files. This folder can be changed by altering the content of the locations inside the configuration of the flyway plugin.

For my first migration file I will use sql

The sql file will be located on the db/migration folder which will reside on the maven resources folder.
The name would be V1_1__Create_Persons.sql
Therefore the full path would be src/main/resources/db/migration/V1_1__Create_Persons.sql

CREATE TABLE Persons
(
PersonID bigint(20) NOT NULL AUTO_INCREMENT,
LastName varchar(255),
FirstName varchar(255),
); 

The second file would be a java file.
It will be located on the package db.migration folder which will reside on the maven src folder.
The name would be V1_2__Insert_Persons.java
Therefore the full path would be src/main/java/db/migration/V1_2__Insert_Persons.java

package db.migration;

import org.flywaydb.core.api.migration.jdbc.JdbcMigration;

import java.sql.Connection;
import java.sql.Statement;

public class V1_2__Insert_Persons implements JdbcMigration
{

	@Override
	public void migrate(Connection connection) throws Exception
	{
		Statement stmt = connection.createStatement();

		stmt.addBatch("INSERT INTO Persons (FirstName,LastName) VALUES ('Emmanouil','Gkatziouras')");

		stmt.addBatch("INSERT INTO Persons (FirstName,LastName) VALUES ('Do not know','this guy')");

		try {
			stmt.executeBatch();
		}
		finally {
			stmt.close();
		}

	}

}

Since we provide a java file it is wise to call flyway by

mvn clean compile flyway:migrate

Aspect Oriented Programming: Qi4j

Qi4j is an emerging framework for composition, injection and aspect oriented programming.

Its main asset is composition however it provides us with tools such as concerns and sideofs in order to apply aspect oriented programming.

I will use the same example with the employees as I did on the previous posts by using Spring and Java EE.
Each employee whether he is a supervisor or a worker will have a different behavior before entering the working area.

First let us start with the interface

 
package com.gkatzioura;

public interface Employee {

    public void enterWorkArea();

}

Also I will create the default implementation for the Employee interface

 
package com.gkatzioura;

import java.util.logging.Logger;

public class EmployeeMixin implements Employee {

    private static final Logger LOGGER = Logger.getLogger(EmployeeMixin.class.getName());

    @Override
    public void enterWorkArea() {

        LOGGER.info("I will enter the work area");

    }

}

Now I will create two transient composites a worker and an employee.

 
package com.gkatzioura;

import org.qi4j.api.composite.TransientComposite;

public interface Worker extends Employee,TransientComposite {
}

 
package com.gkatzioura;

import org.qi4j.api.composite.TransientComposite;

public interface Supervisor extends Employee,TransientComposite {
}

And for each composite I will create a concern. Concern is the equivalent for MethodBeforeAdvice on spring.

 
package com.gkatzioura;

import org.qi4j.api.concern.ConcernOf;
import java.util.logging.Logger;

public class WorkerBeforeConcern extends ConcernOf<Employee> implements Employee{

    private static final Logger LOGGER = Logger.getLogger(WorkerBeforeConcern.class.getName());

    @Override
    public void enterWorkArea() {

        LOGGER.info("Informing the supervisor that I have arrived");

        next.enterWorkArea();
    }
}
 
package com.gkatzioura;

import org.qi4j.api.concern.ConcernOf;
import java.util.logging.Logger;

public class SupervisorBeforeConcern extends ConcernOf<Employee> implements Employee {

    private static final Logger LOGGER = Logger.getLogger(SupervisorBeforeConcern.class.getName());

    @Override
    public void enterWorkArea() {

        LOGGER.info("Check if everything is ok");

        next.enterWorkArea();
    }
}

The last class that remains is to assemble our environment.

 
package com.gkatzioura;

import org.qi4j.api.activation.ActivationException;
import org.qi4j.api.structure.Application;
import org.qi4j.api.structure.Module;
import org.qi4j.bootstrap.*;
import java.util.logging.Logger;

public class Main {

    private static Energy4Java qi4j;
    private static Application application;

    private static final Logger LOGGER = Logger.getLogger(Main.class.getName());

    public static void main(String[] args) throws AssemblyException, ActivationException {

        qi4j = new Energy4Java();
        Application application = qi4j.newApplication(new ApplicationAssembler() {

            @Override
            public ApplicationAssembly assemble(ApplicationAssemblyFactory factory)
                    throws AssemblyException {

                ApplicationAssembly assembly = factory.newApplicationAssembly();

                LayerAssembly rootLayer = assembly.layer("layer");
                ModuleAssembly rootModule = rootLayer.module("module");

                rootModule.transients(Supervisor.class)
                        .withMixins(EmployeeMixin.class)
                        .withConcerns(SupervisorBeforeConcern.class);
                rootModule.transients(Worker.class)
                        .withMixins(EmployeeMixin.class).
                        withConcerns(WorkerBeforeConcern.class);

                return assembly;
            }
        });

        application.activate();

        Module module = application.findModule("layer", "module");

        Employee supervisor = module.newTransient(Supervisor.class);
        Worker worker = module.newTransient(Worker.class);

        supervisor.enterWorkArea();

        worker.enterWorkArea();

    }    
}