SlideShare ist ein Scribd-Unternehmen logo
1 von 37
Downloaden Sie, um offline zu lesen
Database Change Management
Roger Nilsson, Altran
Rikard Thulin, Squeed
Rikard Thulin
Has been working as a software engineer for 17 years,
mostly as an consultant with focus on the Java platform
e-mail: rikard.thulin@squeed.com, twitter: @rikard_thulin,
blog: http://squeed.com/blog
Roger Nilsson
Has been working as a software engineer and consultant
for 15 years, with focus on the Java platform
e-mail: roger.nilsson@altran.com
About the Presenters
Questions?
Questions during the presentation is
encouraged
There will be a Q&A if time permits at
the end of the presentation
The Question?
You never develop code without version
control,
why would you develop your
database without it?
Liquibase at a glance
Liquibase is an Apache 2.0 Licensed tool for
tracking, managing and applying database
changes
Database changes are stored in an XML (or
JSON, YAML, plain-ish SQL) file that is version
controlled and database independent
Liquibase managed and applies changes to the
database
Major Concepts - Changeset
A changeset is uniquely identified change that
should be applied to the database
When Liquibase runs, it queries the
DATABASECHANGELOG table for the
changesets that are marked as executed and
then executes all changesets that have not yet
been executed
Major Concepts - Changeset
Major Concepts - Changelog file
The changelog file contains Change Sets or
references to other Changelog files
Changelog files can be be arbitrarily nested for
better management
Major Concepts - Changelog file
<databaseChangeLog>
<include file="changelogs/jira-937.xml"/>
<include file="changelogs/jira-1023.xml"/>
<include file="changelogs/jira-1104.xml"/>
<!-- Stored Procedures -->
<include file="procedures/uspUpdateSearchIndex.sql"/>
</databaseChangeLog>
Major Concepts - Changes
Each changeset generally contains a change
which describes the change/refactoring to apply
to the database
Liquibase supports both descriptive changes
that generate SQL for supported databases
and raw SQL
Major Concepts - Changeset
Major Concepts - Preconditions
Preconditions can be applied to either the
changelog as a whole or individual change sets
columnExists, tableExists, viewExists, foreignKeyConstraintExists, sqlCheck, and more...
If a precondition fails, Liquibase will stop
execution
Major Concepts - Contexts
Contexts can be applied to changesets to
control which are ran in different environments
For example, some changesets can be tagged
as "production" and others as "test"
Use Contexts only when there is a good reason
● Update rollbacks
● Database ”diff“s
● Generating starting change logs from
existing databases
● Generating database change documentation
● Code branches and merging
Liquibase feature set
Database Change Management
Database deltas are part of the code change
The correlation between code revisions are
ensured
Allows the automatisation of database changes
Keeps all environments consistent
Allows reverting a previous version of a system
Why Liquibase?
Source Code
Why Liquibase?
8 developers with 8 local databases/schemas
Development server
Test / QA server
CI Server
Staging Server Node 1
Production Node 1
Staging Server Node 2
Production Node 2
Why Liquibase?
8 developers with 8 local databases/schemas
Development server
Test / QA server
CI Server
Staging Server Node 1
Production Node 1
Staging Server Node 2
Production Node 2
The four steps
1. Write your database change set
2. Run Liquibase locally to test the SQL
3. Commit your source code and change set
4. Deploy application and database changes
Liquibase plays well when merging and working
with branches
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
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
Referential Integrity Refactorings
● Add Foreign Key Constraint
● Drop Foreign Key Constraint
● Drop All Foreign Key Constraints
● Add Primary Key Constraint
● Drop Primary Key Constraint
Non-Refactoring Transformations
● Insert Data
● Load Data
● Load Update Data
● Update Data
● Delete Data
● Tag Database
● Stop
Architectural Refactorings
● Create Index
● Drop Index
Custom Refactorings
● Modifying Generated SQL
● Custom SQL
● Custom SQL File
● Custom Refactoring Class
● Execute Shell Command
Supported Refactorings
Running Liquibase
● Manual
○ Command Line
○ Ant
○ Maven
● Automatic
○ Spring
○ Servlet Listener
○ CDI Environment
Tips and Tricks - real life experience
You must have a manual way to run Liquibase
(command line, ant or maven) during
development of changesets
Bundle your database changes into your
deployment artifact (spring, servlet listener or
CDI)
It is (mentally) scary to do, but it works!
Demo
1. Update Database with pending changesets
2. Rollback previous changes
Tips and tricks - structure
Think about the structure of your changelogs
Based on experience, when the changelog xml file is
10.000 lines long it is gets ugly. It is much more difficult
when merging and branching.
Have one master changelog that includes smaller
changelogs in turn, divided by release or issue
changelog.xml changelog.xml
jira-1.xml changelog-1_0.xml
jira-4.xml changelog-1_1.xml
Tips and tricks - structure
Liquibase should foremost be used for structural changes
but it is also possible to use it for configuration data and for
test data
Keep in mind that the use case for these are very different!
● Structural changes should be applied together with the
corresponding code
● Configuration data has en dependency to the database
structure, but is not applied at any time
● Test data has a dependency to the database structure,
may have preconditions and is typically applied by a CI
server nightly
Tips and tricks - structure
● changelog.xml for structural changes, applied when
the system is deployed
● test-data.xml for test data, applied nightly be CI server
● configuration-data.xml for system configuration
changes, applied manually when needed
When executing update or rollback there is a
handy option which do not apply changes
against the database but instead save the
resulting SQL
This can be usable when*
● the resulting SQL needs to be tweeked
● having the SQL approved by a DBA
● SOX compliance.
*) Not a recommended best practices by the authors
Advanced Topics
SQL Output
Generating RFC to DBA automatically using Jenkins
REQUEST FOR CHANGE for sqlserver://sqlprod
Planning:
1. Changeset esc-1729.xml::1::rikard::(Checksum: 3:be601b760eaccc5d864da5b3639bb031)
Reviewed by: rikard
Tested in sqldev, 2013-06-17 23:45:46, OK | Insert Column, New attribute for customer
Tested in sqltest, 2013-06-18 12:10:01, OK | Insert Column, New attribute for customer
Tested in sqlprod , 2013-09-14 20:57:44, OK | Insert Column, New attribute for customer
Implementation:
Run script using Jenkins deploy server
Implementation & Review:
Executed by Jenkins/CI-Server, 2013-09-14 20:57 Scripts successfully applied
Advanced Topics
SQL Output - real life example
Tips and tricks - wrong is right
One of the most difficult challenges is to
overcome “wrong is right” principle
● When a changeset has been committed, it
can not be modified even if it is “wrong”
● You must write another changeset to correct
the previous one
● The “wrong” changeset will hit your
production database followed by the “right”
changeset
Tips and tricks - stored procedure
If you have stored procedures the should
obviously be versioned controlled
But the CRC does not fit well as that requires
the stored procedure to be repeated for every
modification
The “run always” attribute saves the day. This
will make sure that whenever there is a change
it is applied -- a better fit for stored procedures
Tips and tricks - stored procedures
--liquibase formatted sql
--changeset rikard:1 runOnChange:true
ALTER procedure javaforum
…
● Above we use a plain sql file with two special
comments that magically makes it a
changeset
Demo
Stored procedure
1. Update database with plain SQL file
2. Change procedure and update again..
Tips and Tricks - neutralizing data
● The “run always” attribute can also be used
to neutralize data in dev and test
environments
● Useful in cases when the database is copied
for production to other environments
● The changes that has been applied is stored
in the database itself, therefore it is safe to
copy clone a database
Tips and Tricks - neutralizing data
<changeSet id="1" author="joe" runAlways="true"
context="sqld,sqlt,localhost">
<comment>Change users psw on (localhost, dev, test) to
'Secret' and their email to 'test@jforum.se'</comment>
<update tableName="user">
<column name="email" value="test@jforum.se"/>
<column name="password" value="Secret"/>
<where>email != 'test@jforum.se' OR password !=
'Secret'</where>
</update>
</changeSet>
Real life experience
We have used Liquibase in projects for many years, more
than 50 man years of development with very little problems
Overall score 97%
by Roger and Rikard
Alternatives
● Flyway
● c5-db-migration
● dbdeploy
● mybatis
● MIGRATEdb
● migrate4j
● dbmaintain
● AutoPatch
Q & A

Weitere ähnliche Inhalte

Was ist angesagt?

Flyway: The agile database migration framework for Java
Flyway: The agile database migration framework for JavaFlyway: The agile database migration framework for Java
Flyway: The agile database migration framework for Java
Axel Fontaine
 
Liquidating database frustrations with liquibase
Liquidating database frustrations with liquibaseLiquidating database frustrations with liquibase
Liquidating database frustrations with liquibase
Paul Churchward
 

Was ist angesagt? (20)

Liquibase for java developers
Liquibase for java developersLiquibase for java developers
Liquibase for java developers
 
Database change management with Liquibase
Database change management with LiquibaseDatabase change management with Liquibase
Database change management with Liquibase
 
Liquibase case study
Liquibase case studyLiquibase case study
Liquibase case study
 
Liquibase migration for data bases
Liquibase migration for data basesLiquibase migration for data bases
Liquibase migration for data bases
 
Liquibase – a time machine for your data
Liquibase – a time machine for your dataLiquibase – a time machine for your data
Liquibase – a time machine for your data
 
Flyway: The agile database migration framework for Java
Flyway: The agile database migration framework for JavaFlyway: The agile database migration framework for Java
Flyway: The agile database migration framework for Java
 
Agile Database Development with Liquibase
Agile Database Development with LiquibaseAgile Database Development with Liquibase
Agile Database Development with Liquibase
 
Database migrations with Flyway and Liquibase
Database migrations with Flyway and LiquibaseDatabase migrations with Flyway and Liquibase
Database migrations with Flyway and Liquibase
 
Successful DB migrations with Liquibase
 Successful DB migrations with Liquibase Successful DB migrations with Liquibase
Successful DB migrations with Liquibase
 
Database migration with flyway
Database migration  with flywayDatabase migration  with flyway
Database migration with flyway
 
Handling Database Deployments
Handling Database DeploymentsHandling Database Deployments
Handling Database Deployments
 
Liquibase - Open Source version control for your database
Liquibase - Open Source version control for your databaseLiquibase - Open Source version control for your database
Liquibase - Open Source version control for your database
 
Liquidating database frustrations with liquibase
Liquidating database frustrations with liquibaseLiquidating database frustrations with liquibase
Liquidating database frustrations with liquibase
 
Oozie meetup - HA
Oozie meetup - HAOozie meetup - HA
Oozie meetup - HA
 
New awesome features in MySQL 5.7
New awesome features in MySQL 5.7New awesome features in MySQL 5.7
New awesome features in MySQL 5.7
 
Products.intro.forum version
Products.intro.forum versionProducts.intro.forum version
Products.intro.forum version
 
Oracle Unified Directory. Lessons learnt. Is it ready for a move from OID? (O...
Oracle Unified Directory. Lessons learnt. Is it ready for a move from OID? (O...Oracle Unified Directory. Lessons learnt. Is it ready for a move from OID? (O...
Oracle Unified Directory. Lessons learnt. Is it ready for a move from OID? (O...
 
MySQL 5.7 NEW FEATURES, BETTER PERFORMANCE, AND THINGS THAT WILL BREAK -- Mid...
MySQL 5.7 NEW FEATURES, BETTER PERFORMANCE, AND THINGS THAT WILL BREAK -- Mid...MySQL 5.7 NEW FEATURES, BETTER PERFORMANCE, AND THINGS THAT WILL BREAK -- Mid...
MySQL 5.7 NEW FEATURES, BETTER PERFORMANCE, AND THINGS THAT WILL BREAK -- Mid...
 
October 2014 HUG : Oozie HA
October 2014 HUG : Oozie HAOctober 2014 HUG : Oozie HA
October 2014 HUG : Oozie HA
 
MySQL 5.7 in a Nutshell
MySQL 5.7 in a NutshellMySQL 5.7 in a Nutshell
MySQL 5.7 in a Nutshell
 

Andere mochten auch

Andere mochten auch (8)

Database Refactoring With Liquibase
Database Refactoring With LiquibaseDatabase Refactoring With Liquibase
Database Refactoring With Liquibase
 
Database Refactoring With Liquibase
Database Refactoring With LiquibaseDatabase Refactoring With Liquibase
Database Refactoring With Liquibase
 
Database Change Management
Database Change ManagementDatabase Change Management
Database Change Management
 
Last Month in PHP - February 2017
Last Month in PHP - February 2017Last Month in PHP - February 2017
Last Month in PHP - February 2017
 
Mini Training Flyway
Mini Training FlywayMini Training Flyway
Mini Training Flyway
 
Getting started with agile database migrations for java flywaydb
Getting started with agile database migrations for java flywaydbGetting started with agile database migrations for java flywaydb
Getting started with agile database migrations for java flywaydb
 
Javaland 2016 - Flyway vs. LiquiBase - Battle der Datenbankmigrationstools
Javaland 2016 - Flyway vs. LiquiBase - Battle der DatenbankmigrationstoolsJavaland 2016 - Flyway vs. LiquiBase - Battle der Datenbankmigrationstools
Javaland 2016 - Flyway vs. LiquiBase - Battle der Datenbankmigrationstools
 
Database Migrations with Gradle and Liquibase
Database Migrations with Gradle and LiquibaseDatabase Migrations with Gradle and Liquibase
Database Migrations with Gradle and Liquibase
 

Ähnlich wie Liquibase få kontroll på dina databasförändringar

Database Build and Release - SQL In The City - Ernest Hwang
Database Build and Release - SQL In The City - Ernest HwangDatabase Build and Release - SQL In The City - Ernest Hwang
Database Build and Release - SQL In The City - Ernest Hwang
Red Gate Software
 

Ähnlich wie Liquibase få kontroll på dina databasförändringar (20)

KoprowskiT_Session2_SDNEvent_SourceControlForDBA
KoprowskiT_Session2_SDNEvent_SourceControlForDBAKoprowskiT_Session2_SDNEvent_SourceControlForDBA
KoprowskiT_Session2_SDNEvent_SourceControlForDBA
 
Liquibase Integration with MuleSoft
Liquibase Integration with MuleSoftLiquibase Integration with MuleSoft
Liquibase Integration with MuleSoft
 
Schema migration in agile environmnets
Schema migration in agile environmnetsSchema migration in agile environmnets
Schema migration in agile environmnets
 
SQL in Version Control using SQL Server Database Projects
SQL in Version Control using SQL Server Database ProjectsSQL in Version Control using SQL Server Database Projects
SQL in Version Control using SQL Server Database Projects
 
MuleSoft integration with Liquibase | Mysore MuleSoft Meetup #3
MuleSoft integration with Liquibase | Mysore MuleSoft Meetup #3MuleSoft integration with Liquibase | Mysore MuleSoft Meetup #3
MuleSoft integration with Liquibase | Mysore MuleSoft Meetup #3
 
Database CI/CD Pipeline
Database CI/CD PipelineDatabase CI/CD Pipeline
Database CI/CD Pipeline
 
Database Build and Release - SQL In The City - Ernest Hwang
Database Build and Release - SQL In The City - Ernest HwangDatabase Build and Release - SQL In The City - Ernest Hwang
Database Build and Release - SQL In The City - Ernest Hwang
 
Microsoft SQL Server Continuous Integration
Microsoft SQL Server Continuous IntegrationMicrosoft SQL Server Continuous Integration
Microsoft SQL Server Continuous Integration
 
Database Fundamental Concepts- Series 1 - Performance Analysis
Database Fundamental Concepts- Series 1 - Performance AnalysisDatabase Fundamental Concepts- Series 1 - Performance Analysis
Database Fundamental Concepts- Series 1 - Performance Analysis
 
MOUG17: SQLT Utility for Tuning - Practical Examples
MOUG17: SQLT Utility for Tuning - Practical ExamplesMOUG17: SQLT Utility for Tuning - Practical Examples
MOUG17: SQLT Utility for Tuning - Practical Examples
 
MOUG17: DB Security; Secure your Data
MOUG17: DB Security; Secure your DataMOUG17: DB Security; Secure your Data
MOUG17: DB Security; Secure your Data
 
Upgrading to my sql 8.0
Upgrading to my sql 8.0Upgrading to my sql 8.0
Upgrading to my sql 8.0
 
What's New in MySQL 5.7
What's New in MySQL 5.7What's New in MySQL 5.7
What's New in MySQL 5.7
 
MySQL 5.7: What's New, Nov. 2015
MySQL 5.7: What's New, Nov. 2015MySQL 5.7: What's New, Nov. 2015
MySQL 5.7: What's New, Nov. 2015
 
Remote DBA Experts 11g Features
Remote DBA Experts 11g FeaturesRemote DBA Experts 11g Features
Remote DBA Experts 11g Features
 
Performance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and UnderscoresPerformance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and Underscores
 
Database automated build and test - SQL In The City Cambridge
Database automated build and test - SQL In The City CambridgeDatabase automated build and test - SQL In The City Cambridge
Database automated build and test - SQL In The City Cambridge
 
Why retail companies can't afford database downtime
Why retail companies can't afford database downtimeWhy retail companies can't afford database downtime
Why retail companies can't afford database downtime
 
Evolutionary database design
Evolutionary database designEvolutionary database design
Evolutionary database design
 
SQL Server DevOps Jumpstart
SQL Server DevOps JumpstartSQL Server DevOps Jumpstart
SQL Server DevOps Jumpstart
 

Mehr von Squeed

Mehr von Squeed (9)

Efficient Code Organisation
Efficient Code OrganisationEfficient Code Organisation
Efficient Code Organisation
 
Groovy moppingjavaforum
Groovy moppingjavaforumGroovy moppingjavaforum
Groovy moppingjavaforum
 
Refactoring toward deeper insight java forum
Refactoring toward deeper insight   java forumRefactoring toward deeper insight   java forum
Refactoring toward deeper insight java forum
 
Javaforum looking into the memory
Javaforum   looking into the memoryJavaforum   looking into the memory
Javaforum looking into the memory
 
Personal kaizen
Personal kaizenPersonal kaizen
Personal kaizen
 
Java one 2011_v0.9
Java one 2011_v0.9Java one 2011_v0.9
Java one 2011_v0.9
 
Javaforum coin
Javaforum coinJavaforum coin
Javaforum coin
 
Javaforum indy
Javaforum indyJavaforum indy
Javaforum indy
 
Javaforum 20110915
Javaforum 20110915Javaforum 20110915
Javaforum 20110915
 

Kürzlich hochgeladen

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 

Liquibase få kontroll på dina databasförändringar

  • 1. Database Change Management Roger Nilsson, Altran Rikard Thulin, Squeed
  • 2. Rikard Thulin Has been working as a software engineer for 17 years, mostly as an consultant with focus on the Java platform e-mail: rikard.thulin@squeed.com, twitter: @rikard_thulin, blog: http://squeed.com/blog Roger Nilsson Has been working as a software engineer and consultant for 15 years, with focus on the Java platform e-mail: roger.nilsson@altran.com About the Presenters
  • 3. Questions? Questions during the presentation is encouraged There will be a Q&A if time permits at the end of the presentation
  • 4. The Question? You never develop code without version control, why would you develop your database without it?
  • 5. Liquibase at a glance Liquibase is an Apache 2.0 Licensed tool for tracking, managing and applying database changes Database changes are stored in an XML (or JSON, YAML, plain-ish SQL) file that is version controlled and database independent Liquibase managed and applies changes to the database
  • 6. Major Concepts - Changeset A changeset is uniquely identified change that should be applied to the database When Liquibase runs, it queries the DATABASECHANGELOG table for the changesets that are marked as executed and then executes all changesets that have not yet been executed
  • 7. Major Concepts - Changeset
  • 8. Major Concepts - Changelog file The changelog file contains Change Sets or references to other Changelog files Changelog files can be be arbitrarily nested for better management
  • 9. Major Concepts - Changelog file <databaseChangeLog> <include file="changelogs/jira-937.xml"/> <include file="changelogs/jira-1023.xml"/> <include file="changelogs/jira-1104.xml"/> <!-- Stored Procedures --> <include file="procedures/uspUpdateSearchIndex.sql"/> </databaseChangeLog>
  • 10. Major Concepts - Changes Each changeset generally contains a change which describes the change/refactoring to apply to the database Liquibase supports both descriptive changes that generate SQL for supported databases and raw SQL
  • 11. Major Concepts - Changeset
  • 12. Major Concepts - Preconditions Preconditions can be applied to either the changelog as a whole or individual change sets columnExists, tableExists, viewExists, foreignKeyConstraintExists, sqlCheck, and more... If a precondition fails, Liquibase will stop execution
  • 13. Major Concepts - Contexts Contexts can be applied to changesets to control which are ran in different environments For example, some changesets can be tagged as "production" and others as "test" Use Contexts only when there is a good reason
  • 14. ● Update rollbacks ● Database ”diff“s ● Generating starting change logs from existing databases ● Generating database change documentation ● Code branches and merging Liquibase feature set
  • 15. Database Change Management Database deltas are part of the code change The correlation between code revisions are ensured Allows the automatisation of database changes Keeps all environments consistent Allows reverting a previous version of a system
  • 17. Why Liquibase? 8 developers with 8 local databases/schemas Development server Test / QA server CI Server Staging Server Node 1 Production Node 1 Staging Server Node 2 Production Node 2
  • 18. Why Liquibase? 8 developers with 8 local databases/schemas Development server Test / QA server CI Server Staging Server Node 1 Production Node 1 Staging Server Node 2 Production Node 2
  • 19. The four steps 1. Write your database change set 2. Run Liquibase locally to test the SQL 3. Commit your source code and change set 4. Deploy application and database changes Liquibase plays well when merging and working with branches
  • 20. 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 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 Referential Integrity Refactorings ● Add Foreign Key Constraint ● Drop Foreign Key Constraint ● Drop All Foreign Key Constraints ● Add Primary Key Constraint ● Drop Primary Key Constraint Non-Refactoring Transformations ● Insert Data ● Load Data ● Load Update Data ● Update Data ● Delete Data ● Tag Database ● Stop Architectural Refactorings ● Create Index ● Drop Index Custom Refactorings ● Modifying Generated SQL ● Custom SQL ● Custom SQL File ● Custom Refactoring Class ● Execute Shell Command Supported Refactorings
  • 21. Running Liquibase ● Manual ○ Command Line ○ Ant ○ Maven ● Automatic ○ Spring ○ Servlet Listener ○ CDI Environment
  • 22. Tips and Tricks - real life experience You must have a manual way to run Liquibase (command line, ant or maven) during development of changesets Bundle your database changes into your deployment artifact (spring, servlet listener or CDI) It is (mentally) scary to do, but it works!
  • 23. Demo 1. Update Database with pending changesets 2. Rollback previous changes
  • 24. Tips and tricks - structure Think about the structure of your changelogs Based on experience, when the changelog xml file is 10.000 lines long it is gets ugly. It is much more difficult when merging and branching. Have one master changelog that includes smaller changelogs in turn, divided by release or issue changelog.xml changelog.xml jira-1.xml changelog-1_0.xml jira-4.xml changelog-1_1.xml
  • 25. Tips and tricks - structure Liquibase should foremost be used for structural changes but it is also possible to use it for configuration data and for test data Keep in mind that the use case for these are very different! ● Structural changes should be applied together with the corresponding code ● Configuration data has en dependency to the database structure, but is not applied at any time ● Test data has a dependency to the database structure, may have preconditions and is typically applied by a CI server nightly
  • 26. Tips and tricks - structure ● changelog.xml for structural changes, applied when the system is deployed ● test-data.xml for test data, applied nightly be CI server ● configuration-data.xml for system configuration changes, applied manually when needed
  • 27. When executing update or rollback there is a handy option which do not apply changes against the database but instead save the resulting SQL This can be usable when* ● the resulting SQL needs to be tweeked ● having the SQL approved by a DBA ● SOX compliance. *) Not a recommended best practices by the authors Advanced Topics SQL Output
  • 28. Generating RFC to DBA automatically using Jenkins REQUEST FOR CHANGE for sqlserver://sqlprod Planning: 1. Changeset esc-1729.xml::1::rikard::(Checksum: 3:be601b760eaccc5d864da5b3639bb031) Reviewed by: rikard Tested in sqldev, 2013-06-17 23:45:46, OK | Insert Column, New attribute for customer Tested in sqltest, 2013-06-18 12:10:01, OK | Insert Column, New attribute for customer Tested in sqlprod , 2013-09-14 20:57:44, OK | Insert Column, New attribute for customer Implementation: Run script using Jenkins deploy server Implementation & Review: Executed by Jenkins/CI-Server, 2013-09-14 20:57 Scripts successfully applied Advanced Topics SQL Output - real life example
  • 29. Tips and tricks - wrong is right One of the most difficult challenges is to overcome “wrong is right” principle ● When a changeset has been committed, it can not be modified even if it is “wrong” ● You must write another changeset to correct the previous one ● The “wrong” changeset will hit your production database followed by the “right” changeset
  • 30. Tips and tricks - stored procedure If you have stored procedures the should obviously be versioned controlled But the CRC does not fit well as that requires the stored procedure to be repeated for every modification The “run always” attribute saves the day. This will make sure that whenever there is a change it is applied -- a better fit for stored procedures
  • 31. Tips and tricks - stored procedures --liquibase formatted sql --changeset rikard:1 runOnChange:true ALTER procedure javaforum … ● Above we use a plain sql file with two special comments that magically makes it a changeset
  • 32. Demo Stored procedure 1. Update database with plain SQL file 2. Change procedure and update again..
  • 33. Tips and Tricks - neutralizing data ● The “run always” attribute can also be used to neutralize data in dev and test environments ● Useful in cases when the database is copied for production to other environments ● The changes that has been applied is stored in the database itself, therefore it is safe to copy clone a database
  • 34. Tips and Tricks - neutralizing data <changeSet id="1" author="joe" runAlways="true" context="sqld,sqlt,localhost"> <comment>Change users psw on (localhost, dev, test) to 'Secret' and their email to 'test@jforum.se'</comment> <update tableName="user"> <column name="email" value="test@jforum.se"/> <column name="password" value="Secret"/> <where>email != 'test@jforum.se' OR password != 'Secret'</where> </update> </changeSet>
  • 35. Real life experience We have used Liquibase in projects for many years, more than 50 man years of development with very little problems Overall score 97% by Roger and Rikard
  • 36. Alternatives ● Flyway ● c5-db-migration ● dbdeploy ● mybatis ● MIGRATEdb ● migrate4j ● dbmaintain ● AutoPatch
  • 37. Q & A