|
IntroductionToDatabaseNG
A short introduction to database migrations with DatabaseNG.
IntroductionDatabaseNG 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 Processorpublic 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 Classpublic 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'. |
Sign in to add a comment
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.
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.
Hrm that being said - this wiki page is a bit out of date compared to the current annotations used. Will update shortly.