The agile database migration framework for Java




                                                                        @axelfontaine
Axel Fontaine                                                         axelfontaine.com
Software Development Expert                                  business@axelfontaine.com
THE PLAN
WARNING!

Disruptive thinking
ahead!
The Problem
addAgeToAccount.sql
?
?   DB
DEV                                                                                              PROD




                                               ?


      http://digitalcitizen.ca/2009/06/22/simpsons-profile-pics-for-facebook-and-other-applications/
VS
DML        DDL
Problems for the DBA


  • Little understanding of context
  • Coordination with app deployer
  • Monkey work
  • Poor use of expertise
  • Poor working hours
Problems for DEV


  • Late review of changes
  • Manual step with friction
  • Less likely to change DB structure
  • Code and DB diverge
  • Incompatible with Continuous Deployment
Goals
                                Goals
• Cooperation between DEV and DBA
• Use full power of the DB
• (Almost) no lock-in
• Integrate in app so you can forget about it
• Descent working hours for everyone
flywaydb.org
6
               commands

clean   info    migrate   validate   init   repair
Empty DB   Version 1   Version 2
Flyway flyway = new Flyway();
flyway.setDataSource(myDataSource);
flyway.migrate();
Demo
DEV
      Migrate   PROD
        on
      Startup
Existing Production Database



                                align



             DEV         TEST           PROD




         V1__Dump.sql
Existing Production Database



                                align



             DEV         TEST           PROD




         V1__Dump.sql
Existing Production Database



                                align



                 DEV     TEST           PROD



       migrate




         V1__Dump.sql
Existing Production Database



                                align



                 DEV     TEST           PROD



       migrate




         V1__Dump.sql
Java Migrations

public class V1_2__Another_user implements JdbcMigration {


    public void migrate(Connection conn) throws Exception {
      PreparedStatement stmt = conn.prepareStatement(“…");

        try {
           stmt.execute();
        } finally {
           stmt.close();
        }
    }
}
Multiple Locations




               http://www.flickr.com/photos/nrivera/3158808122/
Backwards Compatible
Migrations
DB   Backup
DB   Backup
DB
DB
DB
Example

Rename column
 Surname -> Last_Name
Surname             Last_Name



          Surname




                ALTER TABLE … RENAME COLUMN
Last_Name                       Last_Name



Surname                       Last_Name




          ALTER TABLE …                  DROP TRIGGER …
            ADD COLUMN Last_Name         ALTER TABLE …
          UPDATE … SET Last_Name = Surname DROP COLUMN Surname
          CREATE TRIGGER …
Last_Name



Last_Name




        DROP TRIGGER …
        ALTER TABLE …
          DROP COLUMN Surname
Roadmap




   2.1    2.2   3.0
1 day course


 Effective Database
Migrations with Flyway




flywaydb.org/support/training.html
flywaydb.org
@axelfontaine
Axel Fontaine                          axelfontaine.com
Software Development Expert   business@axelfontaine.com

Flyway: The agile database migration framework for Java

Editor's Notes

  • #16 Plain Old SQLConvention Over ConfigurationFail fastZero-required dependenciesJava 5+ and JDBC driverDeceivingly simple, yet surprisingly powerful