My favorites | Sign in
Project Logo
                
Search
for
Updated Mar 28, 2007 by talios
IntroductionToDatabaseNG  
A short introduction to database migrations with DatabaseNG.

Introduction

DatabaseNG is a light weight database migration framework for Java 5+. The project provides a simple framework for declaring controlled, repeatable database migrations for use in both testing and production environments.

Sample Migration Processor

public static void main(String[] args) throws SQLException {
    DatabaseInitializationManagerFactory
            .getInstance("pgsql")
            .createDatabase("my_integration_db", "localhost", "amrk", "password")
            .processMigrations("com.theoryinpractise.dbng", "dbng-examples", "com.theoryinpractise.*");
}

This simple main class uses the DatabaseInitializationManagerFactory to get an PostgreSQL manager which creates a new database called "my_integration_db" against the localhost server.

The call to processMigrations() takes three parameters: groupId, artifactId, and packages. The groupId and artifactId parameters serve as simple organizational groupings similar to those in Maven, the packages parameter defines the package (or base wildcarded package) in which to look for migrations.

Sample Migration Class

public class DatabaseCreation {
    @DataMigration(groupId = "com.theoryinpractise.dbng", artifactId = "dbng-examples", version = 1)
    public void createTheEmptyDatabase(MigrationManager manager) throws IOException {
        manager.executeSqlFile(
                DatabaseCreation.class.getResourceAsStream("/schema-3.2.4.sql"));
        manager.update("CREATE TABLE test (id serial, name varchar(256))");
    }
}

Here we have a simple POJO with a method annotated with @DataMigration, the annotation defines the groupId and artifactId pair as seen in the call to processMigrations() and a version number to assign to this migration. The method takes a MigrationManager parameter which provides transaction bound access to the newly created/opened database.

The API redesign of the framework is in development and is currently likely to change until it looks 'good'.


Comment by Marcus.Ilgner, Oct 12, 2007

Interesting project. Is it still under development? Annotation-based migrations seem like a good idea. I often had to migrate between two different schemas, so annotations to the model-POJOs would be a nice way to map the properties tables and columns in the source and destination databases.

Comment by talios2008, Dec 05, 2007

Hi marcus - I'm not touched this for awhile actually as I got sidetracked with some other work. Although I've not overly needed to touch/modify it much more than there is there.

Although I have been considering adding support for Sybase as I really need to sort out some form of automatically creating/dropping our sybase based projects for getting testing working.

Comment by talios2008, Dec 05, 2007

Hrm that being said - this wiki page is a bit out of date compared to the current annotations used. Will update shortly.


Sign in to add a comment
Hosted by Google Code