2.18k likes | 3.08k Views
Liquibase. Database change management. What is Liquibase?. Liquibase is an open-source solution It is for managing revisions of database schema scripts It works across various types of databases S upports various file formats for defining the DB structure. Why Liquibase?.
E N D
Liquibase Database change management
What is Liquibase? • Liquibase is an open-source solution • It is for managing revisions of database schema scripts • It works across various types of databases • Supports various file formats for defining the DB structure
Why Liquibase? • We used to maintain DB scripts(text based) and add them manually to DB • But there are more issues • easy to lost sync between code and DB state • hard to recover from error during development (ex: in case of applying wrong statement to DB) • often require to re-create DB from scratch during development • hard to find out state of particular DB environment For these issues, Liquibase is a good solution
What are the other similar tools? • Flyway • Liquibase • c5-db-migration • dbdeploy • mybatis • MIGRATEdb • migrate4j • dbmaintain • AutoPatch
How liquibase is different from others? • Java based and easy to manage with java project • Supports for many database types • Easy setup • Database independent migration support • Rollback database changes feature • Database diff report • Extensibility • Support for different changelogformat • build-in: XML, YAML, JSON and SQL
How to setup? • Liquibase can be downloaded from the official site. As of this writing, the stable version is 3.6.2 ( released on July 05, 2018) • https://download.liquibase.org/download/?frm=n • Download Zip and extract • Set the path variable
Major concepts • Changelogfile • Changeset • Changes • Preconditions • Contexts
Change log file • The root of all Liquibase changes is the databaseChangeLogfile. <databaseChangeLog> <changeSet id="1" ...> ... </changeSet> <changeSet id="2" ...> ... </changeSet> </databaseChangeLog>
Change log file • It is possible to break up changelogsinto multiple manageable pieces <databaseChangeLog> <include file="src/api/changelog-api-2.1.0.xml"/> <include file="src/api/changelog-api-2.2.0.xml"/> <include file="src/api/changelog-api-2.3.0.xml"/> ... </databaseChangeLog>
Change SET • The unit Liquibase tracks execution of • Database operation • Liquibase attempts to execute each changeSetin a transaction that is committed at the end, or rolled back if there is an error. • Uniquely identified by the "author" and "id" attribute • Runs only change sets which are not in executed state yet in DATABASECHANGELOG table
Change SET <changeSet id="2.1.0-update-display-order-admin-user-role-event-mgr" author="Vindya" dbms="mysql" context="prod, test"> <comment>Sample of changeset</comment> <!-- comment is optional --> <change .. /> <!-- will be explained later --</changeSet>
Change • Each changeset contains a change which describes the change/refactoring to apply to the database. • One change per changeset
Change • Structural Refactorings • Add Column • Rename Column • Modify Column • Drop Column • Alter Sequence • Create Table • Rename Table • Drop Table • Create View • Rename View • Drop View • Merge Columns • Create Stored Procedure
Change • Data Quality Refactorings • Add Lookup Table • Add Not-Null Constraint • Remove Not-Null Constraint • Add Unique Constraint • Drop Unique Constraint • Create Sequence • Drop Sequence • Add Auto-Increment • Add Default Value • Drop Default Value
Change • Referential Integrity Refactorings • Add Foreign Key Constraint • Drop Foreign Key Constraint • Drop All Foreign Key Constraints • Add Primary Key Constraint • Drop Primary Key Constraint
Change • Non-Refactoring Transformations • Insert Data • Load Data • Load Update Data • Update Data • Delete Data
Change • Architectural Refactorings • Create Index • Drop Index • Custom Refactorings
Change • Modifying Generated SQL • Custom SQL • Custom SQL File • Custom Refactoring Class • Execute Shell Command
preconditions • Preconditions can be applied to either the changelog as a whole or individual change sets • precondition - assertion, that will be evaluated before execution of changeset. • If a precondition fails, Liquibase will stop execution • check for dbms type • check for current user name • check if changeset has been executed • check if table exists • check if table has column • check if view exists • check if FK constraint exists
Preconditions <?xml version="1.0" encoding="UTF-8"?> <changeSet id="1" author=”sfesenko” > <preConditionsonFail="MARK_RAN"> <tableExiststableName="TEST" /> </preConditions> <dropTablecascadeConstraints="true" tableName=”TEST” /> </changeset>
Changeset context • It’s possible to specify for each changeset in what context it should be run. • Context value can be specified on liquibase run. • Contexts can be applied to changesets to control which are ran in different environments. • Example – prod for production and test for test
Christie Best practice • https://christiecowork.atlassian.net/wiki/spaces/CC/pages/1072005286/Database+Version+Control+with+Liquibase
How to run? • On Demand • Command Line • Ant • Maven • Automated • Servlet Listener • Spring Listener • JEE CDI Listener • Java APIs • Liquibase can easily be embedded and executed through its Java APIs.
Command Line? • Create property file liquibase.property with connection • parameters: • driver=com.mysql.jdbc.Driver • classpath=db_changelogs/api • changeLogFile=changelog-api-master.xml • username=<username> • password=<password> • url=jdbc:mysql://localhost:3306/christie • logLevel=DEBUG • Run liquibase as liquibase <command>
Liquibase Commands? • update • updateSQL • validate • status • dropAll • rollbackCount • generateChangeLog • diff • changelogSync • changelogSyncSQL • clearCheckSums
Tips and tricks • Use one file per changeset (greatly simplified merges) • Use convention for file names • Use “run on change” attribute for stored procedures, views, triggers, etc • Decide early if rollback feature will be used • Test both incremental and full update • Do not change “wrong” changeset - just add new one with fix • Consider possibility of “compressing” changesets when full update became slow