viernes, 17 de mayo de 2013

Carbon 5 Database Migration: a Maven plugin for keeping your development and production databases up-to-date

So, you just added a new column to the database schema to giving support to a new feature, but the code crashes in production because someone forgot to update the database schema?
The Carbon 5 Database Migration Plugin aims to solve this. Each time you update your code and need to update your database you just ask Maven to do it.
It's done quite easily: the DB schema syncronization are stored as incremental modifications in the schema into separated .sql files with a timestamp in its filename and c5-database-migration takes care of them.
To set up Carbon 5 db migration you need to:
Add the following plugin repository in your pom.xml
<pluginRepositories>
    <pluginRepository>
        <id>c5-public-repository</id>
        <url>http://mvn.carbonfive.com/public</url>
    </pluginRepository>

</pluginRepositories>

Also, add the folloing plugin in the pom.xml
<plugin>
    <groupId>com.carbonfive.db-support</groupId>
    <artifactId>db-migration-maven-plugin</artifactId>
    <configuration>
        <url>jdbc:mysql://localhost:3306/databasename</url>
        <username>root</username>
        <databaseType>MYSQL</databaseType>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
    </dependencies>
 </plugin>
Pay attention that this one is a set-up for MySQL.
Each time you need to modify your DB schema you just run the following maven command within your project's directory:
mvn db-migration:new
and then you wirte down your SQL statements to update the schema in there. To make the plugin to update your schema you need to run the following command:
mvn db-migration:migrate
If it finds that the DB is outdated it will run the necesary updates. If the DB is updated it will do nothing.
There's another goals for creating and dropping databases, but I will not cover them since they are quite tricky. The main issue with database creation and dropping is that quite often we rely on DB servers which we don't have a sufficiently privileged accounts to create new databases. So, if you need to reset your database you'll have some issues. You can work it around droping manually your database and then creating a blank schema again, or maybe modifying your local pom.xml to have the root access (or a sufficiently privileged one) to your local database.
More info in the following link:
http://code.google.com/p/c5-db-migration/wiki/MavenPlugin

No hay comentarios:

Publicar un comentario