Database change management with 
Liquibase 
Introduction 
Prepared by: Jarosław Szczepankiewicz 
Submitted on: 2014-02-11 
Version: 1.0 
© Rule Financial 2014 1
Why should I care about db change management? 
Can I quickly setup new instance on DEV / PROD? 
What is the state of database on machine X in relation to 
application version 
Who did last upgrade of db? When? 
How can I easily upgrade db to version X 
How can i easily downgrade db to version Y 
© Rule Financial 2014 2
Liquibase promises 
Keep changes in special format as portable as possible 
between DB versions 
Let’s the tool check which changes should be applied to 
upgrade the db to specified version 
Allow me to rollback changes 
Give audit information 
Initialize the whole db from the ground 
© Rule Financial 2014 3
How to start? 
Write changes (changesets) in format you like: 
XML (with XSD support), YAML, JSON, SQL 
Invoke tool 
Commandline 
© Rule Financial 2014 4 
Ant 
Maven 
Spring / CDI (as part of application lifecycle)
Let’s code! 
© Rule Financial 2014 5
Rollbacks 
Most DDL have rollbacks 
Insert, drop X not autorollbacked 
Can be declared empty (<rollback/>) but watch out! 
© Rule Financial 2014 6
Parameters 
From environment or declared in source 
© Rule Financial 2014 7
Preconditions (for changesets) 
Conditionally execute changeset 
AND / OR / NOT available 
Custom preconditions available (through interface 
implementation) 
Dbms, runningAs, changeSetExecuted, columnExists, 
tableExists, viewExists, foreignKeyConstraintExists, indexExists, 
sequenceExists 
On changelog (applied to all files) or changeset level 
© Rule Financial 2014 8
Custom SQL 
Can specify „normal” instruction + rollback 
Can be conditionally executed based on dbms type 
© Rule Financial 2014 9
Not enough refactorings? 
Create your own refactoring class and invoke from changeset 
(liquibase.change.custom.CustomTaskChange, 
liquibase.change.custom.CustomSqlRollback) 
© Rule Financial 2014 10
Your project has already DB? 
Generate changelog / sets from existing db 
Not all features supported (triggers, procedures) 
© Rule Financial 2014 11
© Rule Financial 2014 12 
DIFF 
Check the difference between databases using content 
analysing rather than changelog table history 
Not recommended (limited abilities) but sometime usefull 
Report mode (list) or changeset mode (*.xml)
DBDoc 
Document state + history of changelog / db in javalike style 
© Rule Financial 2014 13
Extensions 
Hibernate – analyze db + mapping and generate diff 
MS SQL / Oracle / Postgresql extensions 
© Rule Financial 2014 14 
…
Thank you 
© Rule Financial 2014 15

Database change management with Liquibase

  • 1.
    Database change managementwith Liquibase Introduction Prepared by: Jarosław Szczepankiewicz Submitted on: 2014-02-11 Version: 1.0 © Rule Financial 2014 1
  • 2.
    Why should Icare about db change management? Can I quickly setup new instance on DEV / PROD? What is the state of database on machine X in relation to application version Who did last upgrade of db? When? How can I easily upgrade db to version X How can i easily downgrade db to version Y © Rule Financial 2014 2
  • 3.
    Liquibase promises Keepchanges in special format as portable as possible between DB versions Let’s the tool check which changes should be applied to upgrade the db to specified version Allow me to rollback changes Give audit information Initialize the whole db from the ground © Rule Financial 2014 3
  • 4.
    How to start? Write changes (changesets) in format you like: XML (with XSD support), YAML, JSON, SQL Invoke tool Commandline © Rule Financial 2014 4 Ant Maven Spring / CDI (as part of application lifecycle)
  • 5.
    Let’s code! ©Rule Financial 2014 5
  • 6.
    Rollbacks Most DDLhave rollbacks Insert, drop X not autorollbacked Can be declared empty (<rollback/>) but watch out! © Rule Financial 2014 6
  • 7.
    Parameters From environmentor declared in source © Rule Financial 2014 7
  • 8.
    Preconditions (for changesets) Conditionally execute changeset AND / OR / NOT available Custom preconditions available (through interface implementation) Dbms, runningAs, changeSetExecuted, columnExists, tableExists, viewExists, foreignKeyConstraintExists, indexExists, sequenceExists On changelog (applied to all files) or changeset level © Rule Financial 2014 8
  • 9.
    Custom SQL Canspecify „normal” instruction + rollback Can be conditionally executed based on dbms type © Rule Financial 2014 9
  • 10.
    Not enough refactorings? Create your own refactoring class and invoke from changeset (liquibase.change.custom.CustomTaskChange, liquibase.change.custom.CustomSqlRollback) © Rule Financial 2014 10
  • 11.
    Your project hasalready DB? Generate changelog / sets from existing db Not all features supported (triggers, procedures) © Rule Financial 2014 11
  • 12.
    © Rule Financial2014 12 DIFF Check the difference between databases using content analysing rather than changelog table history Not recommended (limited abilities) but sometime usefull Report mode (list) or changeset mode (*.xml)
  • 13.
    DBDoc Document state+ history of changelog / db in javalike style © Rule Financial 2014 13
  • 14.
    Extensions Hibernate –analyze db + mapping and generate diff MS SQL / Oracle / Postgresql extensions © Rule Financial 2014 14 …
  • 15.
    Thank you ©Rule Financial 2014 15