Slide 1

Slide 1 text

Introduction to Liquibase Gary Hale

Slide 2

Slide 2 text

Agenda ⬜ Overview ⬜ Liquibase XML basics ⬜ Liquibase Integrations ⬜ Liquibase + Gradle ⬜ Liquibase Functionality ⬜ Demo

Slide 3

Slide 3 text

Overview ⬜ Liquibase premise: All database changes are stored in a human readable yet trackable form and checked into source control. ⬜ Liquibase allows you to: ⬜ Apply database changes systematically ⬜ Manage changes across environments/platforms ⬜ Rollback changes ⬜ Compare databases ⬜ Generate change documentation

Slide 4

Slide 4 text

Overview ⬜ Incremental changes to code are (in general) simple and atomic – remove the previous version, install the new version ⬜ Database deployments are more tricky: ⬜ Can’t remove the schema and recreate it without wiping out data ⬜ Involves getting the database state from point in time A to point in time B ⬜ Order is important ⬜ Easy for environments to drift apart ⬜ Need to be able to determine the state of the database at any point in time

Slide 5

Slide 5 text

Overview Test Prod Add table Column Width = 4 Column Width = 8 Run dml Add table Column Width = 4 Column Width = 8 Run dml

Slide 6

Slide 6 text

Overview ⬜ Liquibase uses a “changelog” which captures all database changes ⬜ The changelog can be stored in source control (and versioned) to bind a certain database version to a version of code ⬜ Changelogs are composed of “changesets” which are atomic groups of operations that applied and rolled back in increments

Slide 7

Slide 7 text

Overview ⬜ Liquibase supports: ⬜ Multiple databases (e.g. dev, test, prod) ⬜ Multiple database platforms ⬜ Multiple contexts ⬜ DDL, DML, and DCL ⬜ Abstract DSL or plain SQL ⬜ Stored Procedures ⬜ Database Tags

Slide 8

Slide 8 text

XML Basics ⬜ Liquibase can be called from: ⬜ Command Line ⬜ Ant ⬜ Maven ⬜ Spring ⬜ Grails ⬜ Servlet Listener ⬜ Gradle

Slide 9

Slide 9 text

XML Basics ⬜ Sample Change Log:

Slide 10

Slide 10 text

XML Basics ⬜ Sample Change Log: CREATE TABLE `person` (`id` INT AUTO_INCREMENT NOT NULL, `firstname` VARCHAR(50), `lastname` VARCHAR(50) NOT NULL, CONSTRAINT `PK_PERSON` PRIMARY KEY (`id`))

Slide 11

Slide 11 text

Command Line ⬜ Sample Command Line Invocation: liquibase \ --url=jdbc:mysql://jxlprdbld02:3306/liquibase_demo?auto_reconnect=true \ --classpath=${HOME}/workspaces/developer_config/lib/mysql/mysql-connector-java/5.1.6 /mysql-connector-java-5.1.6.jar \ --changeLogFile=changelog.xml \ --username=liquibase \ --password=lqdem0 \ update

Slide 12

Slide 12 text

Ant Plugin db.changelog.file not set database.url not set database.username not set database.password not set

Slide 13

Slide 13 text

Servlet Listener liquibase.changelog com/example/db.changelog.xml liquibase.datasource java:comp/env/jdbc/default liquibase.host.includes production1.example.com, production2.example.com liquibase.onerror.fail true liquibase.contexts production liquibase.integration.servlet.LiquibaseServletListener

Slide 14

Slide 14 text

Spring

Slide 15

Slide 15 text

Gradle Plugin ⬜ Allows Liquibase XML to be written as Groovy DSL ⬜ Allows a single build file with multiple database definitions as well as multiple changelog definitions ⬜ Allows mixing of Groovy and XML changelogs if desired

Slide 16

Slide 16 text

Liquibase Functionality ⬜ Status – Prints the current status of the target database and what changesets (if any) need to be applied ⬜ Validate – Validates the changelog checking for errors, etc ⬜ Update – Migrates the database to the current state of the changelog ⬜ UpdateSQL – Prints out the SQL that needs to be executed to migrate the database to the current state of the changelog

Slide 17

Slide 17 text

Liquibase Functionality ⬜ Tag – Tags the current state of the database as a milestone for rollback ⬜ Rollback – Rolls back a database to a specified tag ⬜ RollbackCount – Rolls back a database by specified number of changesets ⬜ RollbackToDate – Rolls back a database to its state on a specified date

Slide 18

Slide 18 text

Liquibase Functionality ⬜ RollbackSQL – Prints the SQL that needs to be executed to rollback to the specified tag ⬜ UpdateTestingRollback – For each changeset, migrates the database to include the changeset, rolls it back and migrates again (tests rollback as part of the database migration) ⬜ GenerateChangeLog – Generates a change log from an existing database (does not include stored procedures)

Slide 19

Slide 19 text

Liquibase Functionality ⬜ Diff – Diffs two databases to determine what’s different based on the changelog. ⬜ Database contexts allow you to specify certain changesets which should only be applied when a given context is in effect.

Slide 20

Slide 20 text

Demo

Slide 21

Slide 21 text

Questions ?