Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Liquibase

Gary Hale
January 09, 2013

Introduction to Liquibase

JAXJUG presentation on Liquibase

Gary Hale

January 09, 2013
Tweet

More Decks by Gary Hale

Other Decks in Technology

Transcript

  1. Agenda ⬜ Overview ⬜ Liquibase XML basics ⬜ Liquibase Integrations

    ⬜ Liquibase + Gradle ⬜ Liquibase Functionality ⬜ Demo
  2. 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
  3. 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
  4. Overview Test Prod Add table Column Width = 4 Column

    Width = 8 Run dml Add table Column Width = 4 Column Width = 8 Run dml
  5. 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
  6. 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
  7. XML Basics ⬜ Liquibase can be called from: ⬜ Command

    Line ⬜ Ant ⬜ Maven ⬜ Spring ⬜ Grails ⬜ Servlet Listener ⬜ Gradle
  8. XML Basics ⬜ Sample Change Log: <?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog

    xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd"> <preConditions> <dbms type="mysql"/> </preConditions> <changeSet id="123-1" author="ghale"> <createTable tableName="person"> <column name="id" type="int" autoIncrement="true"> <constraints primaryKey="true" nullable="false"/> </column> <column name="firstname" type="varchar(50)"/> <column name="lastname" type="varchar(50)"> <constraints nullable="false"/> </column> </createTable> </changeSet> <include file=“/some/other/changelog.xml” /> </databaseChangeLog>
  9. XML Basics ⬜ Sample Change Log: <?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog

    xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd"> <preConditions> <dbms type="mysql"/> </preConditions> <changeSet id="123-1" author="ghale"> <sql> CREATE TABLE `person` (`id` INT AUTO_INCREMENT NOT NULL, `firstname` VARCHAR(50), `lastname` VARCHAR(50) NOT NULL, CONSTRAINT `PK_PERSON` PRIMARY KEY (`id`)) </sql> </changeSet> <include file=“/some/other/changelog.xml” /> </databaseChangeLog>
  10. 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
  11. Ant Plugin <target name="update-database" depends="prepare"> <fail unless="db.changelog.file">db.changelog.file not set</fail> <fail

    unless="database.url">database.url not set</fail> <fail unless="database.username">database.username not set</fail> <fail unless="database.password">database.password not set</fail> <taskdef resource="liquibasetasks.properties"> <classpath refid="classpath"/> </taskdef> <updateDatabase changeLogFile="${db.changelog.file}" driver="${database.driver}" url="${database.url}" username="${database.username}" password="${database.password}" promptOnNonLocalDatabase="${prompt.user.if.not.local.database}" dropFirst="false" classpathref="classpath" /> </target>
  12. Servlet Listener <context-param> <param-name>liquibase.changelog</param-name> <param-value>com/example/db.changelog.xml</param-value> </context-param> <context-param> <param-name>liquibase.datasource</param-name> <param-value>java:comp/env/jdbc/default</param-value> </context-param>

    <context-param> <param-name>liquibase.host.includes</param-name> <param-value>production1.example.com, production2.example.com</param-value> </context-param> <context-param> <param-name>liquibase.onerror.fail</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>liquibase.contexts</param-name> <param-value>production</param-value> </context-param> <listener> <listener-class>liquibase.integration.servlet.LiquibaseServletListener</listener-class> </listener>
  13. 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
  14. 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
  15. 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
  16. 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)
  17. 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.