Database Migrations with Maven

Published: 10:10 PM GMT+12, Tuesday, 21 October 2008 under: technology
java  maven  sql 

In order to learn more about using Apache Maven beyond a more 'simple' build, I decided to build a maven plugin, or mojo as they're called to wrap my simple database migration library.

Now it's as easy as a few lines of XML and SQL to have your database automatically regenerated whenever you run your tests:

<build>
  <plugins>
    <plugin>
      <groupId>com.theoryinpractise.dbng</groupId>
      <artifactId>dbng-maven-plugin</artifactId>
      <version>1.1-SNAPSHOT</version>
      <configuration>
        <createDatabase>true</createDatabase>
        <username>postgres</username>
        <password>****</password>

        <files>
          <file>src/main/resources/test.sql</file>
        </files>

      </configuration>
  			  	
      <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>8.3-603.jdbc4</version>
      </dependency>

    </plugin>
  		  
  </plugins>
</build>

By default the plugin is (currently) bound to the 'process-classes' phase as I wanted things to run before the 'test' phase and Maven doesn't currently have before/after meta-phases. Migrations themselves are not yet fully supported in the plugin yet, but for most cases I'm finding simply recreating the database and applying a set of SQL files (schema, default data) does 99% of what I need in testing.

Comments (0)

Add Comment