Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Setup

Create a new java project

...

Code Block
languagexml
titlepom.xml
 <project  xmlns="http://maven.apache.org/POM/4.0.0"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
   <modelVersion>4.0.0</modelVersion>  
 
   <groupId>com.liquibase.mariadb.app</groupId>  
   <artifactId>LiquibaseMariadbp</artifactId>  
   <version>1.0-SNAPSHOT</version>  
   <build>  
       <pluginManagement>  
           <plugins>  
               <plugin>  
                   <groupId>org.liquibase</groupId>  
                   <artifactId>liquibase-maven-plugin</artifactId>  
                   <version>3.8.0</version>  
                   <configuration>  
                       <propertyFile>liquibase.properties</propertyFile>  
                   </configuration>  
                   <dependencies>  
					<dependency>
					    <groupId>org.mariadb.jdbc</groupId>
					    <artifactId>mariadb-java-client</artifactId>
					    <version>2.7.3</version>
					</dependency>					
				   <dependency>  
                       <groupId>org.hibernate</groupId>  
                       <artifactId>hibernate-core</artifactId>  
                       <version>5.4.6.Final</version>  
                   </dependency>  
                 </dependencies>  
               </plugin>  
           </plugins>  
       </pluginManagement>  
   </build>  
 </project>

Test 1: Update database with changeLog in XML format

From the command line in the project directory

...

[INFO] SELECT * FROM DATABASECHANGELOG ORDER BY DATEEXECUTED ASC, ORDEREXECUTED ASC
[INFO] SELECT count(1) FROM information_schema.tables
WHERE table_name = 'PDPSTATISTICS'
[INFO] INSERT INTO PDPSTATISTICS
(ID, NAME, VERSION, PDPGROUPNAME, PDPSUBGROUPNAME, POLICYDEPLOYCOUNT, POLICYDEPLOYFAILCOUNT,
POLICYDEPLOYSUCCESSCOUNT, POLICYEXECUTEDCOUNT, POLICYEXECUTEDFAILCOUNT,
POLICYEXECUTEDSUCCESSCOUNT, TIMESTAMP)
VALUES (3, 'TestName', '1.0.0', 'TestGroup', 'TestSubGroup', 1, 1, 1, 1, 1, 1, now())
[INFO] Custom SQL executed
[INFO] ChangeSet dbchangelog.xml::policy-1::policy ran successfully in 7ms

Test 2: Rollback update

mvn liquibase:rollback -Dliquibase.rollbackCount=1

...

[INFO] SELECT * FROM DATABASECHANGELOG ORDER BY DATEEXECUTED ASC, ORDEREXECUTED ASC
[INFO] Rolling Back Changeset:dbchangelog.xml::policy-1::policy
[INFO] DELETE FROM PDPSTATISTICS WHERE ID=3
[INFO] DELETE FROM DATABASECHANGELOG WHERE ID = 'policy-1' AND AUTHOR = 'policy' AND FILENAME = 'dbchangelog.xml'

Test 3: Generate database dump chageLog in YAML format

Add the following 2 lines to liquibase.properties

...

View file
namedbchangelog.mariadb.yaml
height250

Test 4: Generate database dump chageLog in SQL format

change outputChangeLogFile: dbchangelog.mariadb.yaml to outputChangeLogFile: dbchangelog.mariadb.sql

...

View file
namedbchangelog.mariadb.sql
height250

Test 5: Update database with changeLog in YAML format

Add dbchangelog.yaml

Code Block
languageyml
titledbchangelog.yaml
databaseChangeLog:

  -  changeSet:
      id:  policy-2
      author:  admin
      changes:
        -  createTable:
            tableName:  JpaPdpPolicyDeploymentAudit
            columns:
              -  column:
                  name:  id
                  type:  int
                  autoIncrement:  true
                  constraints:
                    primaryKey:  true
                    nullable:  false
              -  column:
                  constraints:
                    primaryKey:  true
                    nullable:  false
                  name:  name
                  type:  varchar(120)
              -  column:
                  name:  version
                  type:  varchar(20)
                  constraints:
                    primaryKey:  true
                    nullable:  false
              -  column:
                  name:  pdpGroup
                  type:  varchar(255)
                  constraints:
                    nullable:  true
              -  column:
                  name:  pdpType
                  type:  varchar(255)
                  constraints:
                    nullable:  true    
              -  column:
                  name:  action
                  type:  int
                  constraints:
                    nullable:  true  
              -  column:
                  name:  timeStamp
                  type:  datetime
                  constraints:
                    nullable:  true                       

  -  changeSet:
      id:  policy-3
      author:  admin
      changes:
        -  addColumn:
            tableName:  JpaPdpPolicyDeploymentAudit
            columns:
              -  column:
                  name:  changedByUser
                  type:  varchar(255)
                  constraints:
                    nullable:  true 

...

Code Block
languagetext
titledatabasechangelog
select * from databasechangelog;
+----+--------+------------------+---------------------+---------------+----------+------------------------------------+---------------------------------------------------------------------------------------------------------+----------+------+-----------+----------+--------+---------------+
| ID | AUTHOR | FILENAME         | DATEEXECUTED        | ORDEREXECUTED | EXECTYPE | MD5SUM                             | DESCRIPTION                                                                                             | COMMENTS | TAG  | LIQUIBASE | CONTEXTS | LABELS | DEPLOYMENT_ID |
+----+--------+------------------+---------------------+---------------+----------+------------------------------------+---------------------------------------------------------------------------------------------------------+----------+------+-----------+----------+--------+---------------+
| policy-2   | admin  | dbchangelog.yaml               | 2021-05-24 15:04:49 |            10 | EXECUTED | 8:f5bd9cec4e2374254cc1985e8e9bfaae | createTable tableName=JpaPdpPolicyDeploymentAudit |          | NULL | 3.8.0     | NULL     | NULL       | 1865089400    |
| policy-3   | admin  | dbchangelog.yaml               | 2021-05-24 15:04:49 |            11 | EXECUTED | 8:4c9ec93973102be9b6d966cf6bc724b4 | addColumn tableName=JpaPdpPolicyDeploymentAudit   |          | NULL | 3.8.0     | NULL     | NULL       | 1865089400    |
+----+--------+------------------+---------------------+---------------+----------+------------------------------------+---------------------------------------------------------------------------------------------------------+----------+------+-----------+----------+--------+---------------+

Test 6: Rollback last update

mvn liquibase:rollback -Dliquibase.rollbackCount=1

[INFO] SELECT * FROM DATABASECHANGELOG ORDER BY DATEEXECUTED ASC, ORDEREXECUTED ASC
[INFO] Rolling Back Changeset:dbchangelog.yaml::policy-3::admin
[INFO] ALTER TABLE JpaPdpPolicyDeploymentAudit DROP COLUMN changedByUser
[INFO] DELETE FROM DATABASECHANGELOG WHERE ID = 'policy-3' AND AUTHOR = 'admin' AND FILENAME = 'dbchangelog.yaml'

Test 7: Rollback last update

mvn liquibase:rollback -Dliquibase.rollbackCount=1

[INFO] Rolling Back Changeset:dbchangelog.yaml::policy-2::admin
[INFO] DROP TABLE JpaPdpPolicyDeploymentAudit
[INFO] DELETE FROM DATABASECHANGELOG WHERE ID = 'policy-2' AND AUTHOR = 'admin' AND FILENAME = 'dbchangelog.yaml'

Test 8: Update database with changeLog in YAML format which includes raw SQL

Add dbchangelog-sql.yaml

Code Block
languageyml
titledbchangelog-sql.yaml
 databaseChangeLog:
  -  changeSet:  
       id:  policy-4 
       author:  admin  
       changes:  
        -  sql:  
            comment:  create JpaPdpPolicyDeploymentAudit table  
            dbms:  '!h2,  oracle,  mysql, mariadb'  
            endDelimiter:  \nGO  
            splitStatements:  true  
            sql:  CREATE TABLE JpaPdpPolicyDeploymentAudit (
                                 ID BIGINT NOT NULL, 
                                 NAME VARCHAR(120) NOT NULL, 
                                 VERSION VARCHAR(20) NOT NULL,
                                 PDPGROUP VARCHAR(255) DEFAULT NULL NULL,
                                 PDPTYPE VARCHAR(255) DEFAULT NULL NULL,
                                 ACTION BIGINT DEFAULT NULL NULL, 
                                 TIMESTAMP datetime DEFAULT NULL NULL,  
                                 CHANGEDBYUSER VARCHAR(255) DEFAULT NULL NULL,
                   CONSTRAINT PK_PDPSTATISTICS PRIMARY KEY (ID, NAME, VERSION)
                   );             
            stripComments:  true
  -  changeSet:  
       id:  policy-5
       author:  admin  
       changes:  
        -  sql:  
            comment:  insert JpaPdpPolicyDeploymentAudit 
            dbms:  '!h2,  oracle,  mysql, mariadb'  
            endDelimiter:  \nGO  
            splitStatements:  true  
            sql:  insert into JpaPdpPolicyDeploymentAudit 
                  (id, name, version, pdpGroup, PdpType, action, timeStamp, changedByUser) 
                  values 
                  (1, 'TestName', '1.0.0', 'TestGroup', 'TestType', 1, now(), 'Admin')  
            stripComments:  true                       

...

A new table called JpaPdpPolicyDeploymentAudit with one entry has been added to the database

Test 9: Rollback 2 previous updates

Run mvn liquibase:rollback -Dliquibase.rollbackCount=2

[INFO] Rolling Back Changeset:dbchangelog-sql.yaml::policy-5::admin
[INFO] delete from JpaPdpPolicyDeploymentAudit where id = 1
[INFO] DELETE FROM DATABASECHANGELOG WHERE ID = 'policy-5' AND AUTHOR = 'admin' AND FILENAME = 'dbchangelog-sql.yaml'
[INFO] Rolling Back Changeset:dbchangelog-sql.yaml::policy-4::admin
[INFO] drop table JpaPdpPolicyDeploymentAudit
[INFO] DELETE FROM DATABASECHANGELOG WHERE ID = 'policy-4' AND AUTHOR = 'admin' AND FILENAME = 'dbchangelog-sql.yaml'

Test 10: Update database with changeLog in YAML format which calls 2 embedded SQL changeLogs

Add JpaPdpPolicyDeploymentAudit-changelog-1.sql

...

Note: When using raw SQL you must provide the rollback statements in order to revert the database to it's previous state


Test 11: Update database with changeLog in YAML format which includes raw SQL and tags

Add Add dbchangelog-sql2.yaml

...

2 new records have been added to the JpaPdpPolicyDeploymentAudit table

Test 12: Rollback update using tag

mvn liquibase:rollback -Dliquibase.rollbackTag=1.0

...

Last insert has been deleted


Test 13: Re-run last update

mvn liquibase:update

[INFO] Tag '1.1' applied to database
[INFO] insert into JpaPdpPolicyDeploymentAudit (id, name, version, pdpGroup, PdpType, action, timeStamp, changedByUser) values (4, 'TestName4', '1.0.0', 'TestGroup4', 'TestType4', 1, now(), 'Admin')
[INFO] Custom SQL executed
[INFO] ChangeSet dbchangelog-sql2.yaml::policy-9::admin ran successfully in 7ms
[INFO] SELECT MAX(ORDEREXECUTED) FROM DATABASECHANGELOG
[INFO] INSERT INTO DATABASECHANGELOG (ID, AUTHOR, FILENAME, DATEEXECUTED, ORDEREXECUTED, MD5SUM, `DESCRIPTION`, COMMENTS, EXECTYPE, CONTEXTS, LABELS, LIQUIBASE, 

Only the second insert statement ran.


Test 14: Add labels to changeSets

Add dbchangelog-sql3.yaml

...

Only changeSet with label release1.0 has been applied


Test 15: Apply second label

Run mvn liquibase:update -Dliquibase.labels=release1.0,release1.1

...

ChangeSet with label release1.1 has been applied


Test 16: Test contexts

Add dbchangelog-sql4.yaml

...

To see more about contexts and labels please visit: Understanding Contexts vs. Labels

Test 17: Test YAML changelog

Add dbchangelog-sql5.yaml

...

[INFO] Rolling Back Changeset:dbchangelog-sql5.yaml::policy-15::admin
[INFO] Successfully released change log lock
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.454 s
[INFO] Finished at: 2021-05-25T09:33:29+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.liquibase:liquibase-maven-plugin:3.8.0:rollback (default-cli) on project LiquibaseMariadbp: Error setting up or running Liquibase: liquibase.exception.RollbackImpossibleException: No inverse to liquibase.change.core.InsertDataChange created -> [Help 1]


Cannot rollback.


Test 18: Test YAML changelog with embedded SQL

Add dbchangelog-sql6.yaml

...

[INFO] SELECT MAX(ORDEREXECUTED) FROM DATABASECHANGELOG
[INFO] INSERT INTO DATABASECHANGELOG (ID, AUTHOR, FILENAME, DATEEXECUTED, ORDEREXECUTED, MD5SUM, `DESCRIPTION`, COMMENTS, EXECTYPE, CONTEXTS, LABELS, LIQUIBASE, DEPLOYMENT_ID, TAG) VALUES ('policy-16', 'admin', 'dbchangelog-sql6.yaml', NOW(), 22, '8:9d2fcf472209118ad3760ca621ab787e', 'tagDatabase; sql', '', 'MARK_RAN', NULL, NULL, '3.8.0', '1934277203', '22')
[INFO] Tag '23' applied to database
[INFO] INSERT INTO pdpstatistics (PDPGROUPNAME, PDPSUBGROUPNAME, POLICYDEPLOYCOUNT, POLICYDEPLOYFAILCOUNT, POLICYDEPLOYSUCCESSCOUNT, POLICYEXECUTEDCOUNT, POLICYEXECUTEDFAILCOUNT, POLICYEXECUTEDSUCCESSCOUNT, TIMESTAMP, ID, name, version) VALUES ('group', 'subgroup', 2, 1, 1, 2, 1, 1, NOW(), 11, 'name2', '0.0.0');
[INFO] Custom SQL executed
[INFO] INSERT INTO pdpstatistics (PDPGROUPNAME, PDPSUBGROUPNAME, POLICYDEPLOYCOUNT, POLICYDEPLOYFAILCOUNT, POLICYDEPLOYSUCCESSCOUNT, POLICYEXECUTEDCOUNT, POLICYEXECUTEDFAILCOUNT, POLICYEXECUTEDSUCCESSCOUNT, TIMESTAMP, ID, name, version) VALUES ('group', 'subgroup', 2, 1, 1, 2, 1, 1, NOW(), 12, 'name2', '0.0.0');
[INFO] Custom SQL executed
[INFO] INSERT INTO pdpstatistics (PDPGROUPNAME, PDPSUBGROUPNAME, POLICYDEPLOYCOUNT, POLICYDEPLOYFAILCOUNT, POLICYDEPLOYSUCCESSCOUNT, POLICYEXECUTEDCOUNT, POLICYEXECUTEDFAILCOUNT, POLICYEXECUTEDSUCCESSCOUNT, TIMESTAMP, ID, name, version) VALUES ('group', 'subgroup', 2, 1, 1, 2, 1, 1, NOW(), 13, 'name2', '0.0.0');
[INFO] Custom SQL executed
[INFO] INSERT INTO pdpstatistics (PDPGROUPNAME, PDPSUBGROUPNAME, POLICYDEPLOYCOUNT, POLICYDEPLOYFAILCOUNT, POLICYDEPLOYSUCCESSCOUNT, POLICYEXECUTEDCOUNT, POLICYEXECUTEDFAILCOUNT, POLICYEXECUTEDSUCCESSCOUNT, TIMESTAMP, ID, name, version) VALUES ('group', 'subgroup', 2, 1, 1, 2, 1, 1, NOW(), 14, 'name2', '0.0.0');
[INFO] Custom SQL executed
[INFO] INSERT INTO pdpstatistics (PDPGROUPNAME, PDPSUBGROUPNAME, POLICYDEPLOYCOUNT, POLICYDEPLOYFAILCOUNT, POLICYDEPLOYSUCCESSCOUNT, POLICYEXECUTEDCOUNT, POLICYEXECUTEDFAILCOUNT, POLICYEXECUTEDSUCCESSCOUNT, TIMESTAMP, ID, name, version) VALUES ('group', 'subgroup', 2, 1, 1, 2, 1, 1, NOW(), 15, 'name2', '0.0.0');
[INFO] Custom SQL executed
[INFO] INSERT INTO pdpstatistics (PDPGROUPNAME, PDPSUBGROUPNAME, POLICYDEPLOYCOUNT, POLICYDEPLOYFAILCOUNT, POLICYDEPLOYSUCCESSCOUNT, POLICYEXECUTEDCOUNT, POLICYEXECUTEDFAILCOUNT, POLICYEXECUTEDSUCCESSCOUNT, TIMESTAMP, ID, name, version) VALUES ('group', 'subgroup', 2, 1, 1, 2, 1, 1, NOW(), 16, 'name2', '0.0.0');
[INFO] Custom SQL executed
[INFO] INSERT INTO pdpstatistics (PDPGROUPNAME, PDPSUBGROUPNAME, POLICYDEPLOYCOUNT, POLICYDEPLOYFAILCOUNT, POLICYDEPLOYSUCCESSCOUNT, POLICYEXECUTEDCOUNT, POLICYEXECUTEDFAILCOUNT, POLICYEXECUTEDSUCCESSCOUNT, TIMESTAMP, ID, name, version) VALUES ('group', 'subgroup', 2, 1, 1, 2, 1, 1, NOW(), 17, 'name2', '0.0.0');
[INFO] Custom SQL executed
[INFO] INSERT INTO pdpstatistics (PDPGROUPNAME, PDPSUBGROUPNAME, POLICYDEPLOYCOUNT, POLICYDEPLOYFAILCOUNT, POLICYDEPLOYSUCCESSCOUNT, POLICYEXECUTEDCOUNT, POLICYEXECUTEDFAILCOUNT, POLICYEXECUTEDSUCCESSCOUNT, TIMESTAMP, ID, name, version) VALUES ('group', 'subgroup', 2, 1, 1, 2, 1, 1, NOW(), 18, 'name2', '0.0.0');
[INFO] Custom SQL executed
[INFO] INSERT INTO pdpstatistics (PDPGROUPNAME, PDPSUBGROUPNAME, POLICYDEPLOYCOUNT, POLICYDEPLOYFAILCOUNT, POLICYDEPLOYSUCCESSCOUNT, POLICYEXECUTEDCOUNT, POLICYEXECUTEDFAILCOUNT, POLICYEXECUTEDSUCCESSCOUNT, TIMESTAMP, ID, name, version) VALUES ('group', 'subgroup', 2, 1, 1, 2, 1, 1, NOW(), 19, 'name2', '0.0.0');
[INFO] Custom SQL executed
[INFO] INSERT INTO pdpstatistics (PDPGROUPNAME, PDPSUBGROUPNAME, POLICYDEPLOYCOUNT, POLICYDEPLOYFAILCOUNT, POLICYDEPLOYSUCCESSCOUNT, POLICYEXECUTEDCOUNT, POLICYEXECUTEDFAILCOUNT, POLICYEXECUTEDSUCCESSCOUNT, TIMESTAMP, ID, name, version) VALUES ('group', 'subgroup', 2, 1, 1, 2, 1, 1, NOW(), 20, 'name2', '0.0.0');
[INFO] Custom SQL executed
[INFO] ChangeSet dbchangelog-sql6.yaml::policy-17::admin ran successfully in 52ms
[INFO] INSERT INTO DATABASECHANGELOG (ID, AUTHOR, FILENAME, DATEEXECUTED, ORDEREXECUTED, MD5SUM, `DESCRIPTION`, COMMENTS, EXECTYPE, CONTEXTS, LABELS, LIQUIBASE, DEPLOYMENT_ID, TAG) VALUES ('policy-17', 'admin', 'dbchangelog-sql6.yaml', NOW(), 23, '8:0dfbbf124f39d3e3ae9a0b8a41fb62d0', 'tagDatabase; sql; sql; sql; sql; sql; sql; sql; sql; sql; sql', '', 'EXECUTED', NULL, NULL, '3.8.0', '1934277203', '23')



Test 19: Test rollup table creation

Add dbchangelog-sql7.yaml

...

Code Block
languagetext
titlePDPSTATISTICS_ROLLUP
MariaDB [policy]> select * from pdpstatistics_rollup;
+-------+---------+--------------+-----------------+------+-------+-----------------------+---------------------------+------------------------------+-------------------------+-----------------------------+--------------------------------+
| name  | version | PDPGROUPNAME | PDPSUBGROUPNAME | YEAR | MONTH | AVG_POLICYDEPLOYCOUNT | AVG_POLICYDEPLOYFAILCOUNT | AVG_POLICYDEPLOYSUCCESSCOUNT | AVG_POLICYEXECUTEDCOUNT | AVG_POLICYEXECUTEDFAILCOUNT | AVG_POLICYEXECUTEDSUCCESSCOUNT |
+-------+---------+--------------+-----------------+------+-------+-----------------------+---------------------------+------------------------------+-------------------------+-----------------------------+--------------------------------+
| name2 | 0.0.0   | group        | subgroup        | 2004 |     3 |                     2 |                         1 |                            1 |                       2 |                           1 |                              1 |
| name2 | 0.0.0   | group        | subgroup        | 2021 |     5 |                     2 |                         1 |                            1 |                       2 |                           1 |                              1 |
+-------+---------+--------------+-----------------+------+-------+-----------------------+---------------------------+------------------------------+-------------------------+-----------------------------+--------------------------------+



Test 20: Test preconditions

Add dbchangelog-sql8.yaml

...

Common tests used in liquibase are available here: common.tests.changelog.yaml

Important concepts to be considered

  1. Labels and Context - to separate environments and releases
  2. Tags - to set  a rollback check point
  3. Prechecks - to check if a particular changeSet needs to be executed
  4. Different formats - decide the best one for the team to work with
  5. Include rollback statement with changeSet to allow user to undo changes. 

Proposed Liquibase Format

Something like the following example might work best for us.


We can use all the liquibase functions with SQL at the same timeand include rollback:



Code Block
languageyml
titledbchangelog-sql10.yaml
databaseChangeLog:
  -  changeSet:  
       id:  policy-26
       author:  admin  
       label: release1.5
       preConditions:
         - onFail: HALT
         - tableExists:
             tableName: JpaPdpPolicyDeploymentAudit
       changes:
       - tagDatabase:  
          tag:  31       
       -  sqlFile:  
           comment:  insert JpaPdpPolicyDeploymentAudit
           dbms:  '!h2,  oracle,  mysql, mariadb'  
           endDelimiter:  \nGO  
           splitStatements:  true  
           stripComments:  true               
           path: sql/insert_JpaPdpPolicyDeploymentAudit.sql
           relativeToChangelogFile: true  
       rollback:    
       -  sqlFile:  
           comment:  rollback JpaPdpPolicyDeploymentAudit
           dbms:  '!h2,  oracle,  mysql, mariadb'  
           endDelimiter:  \nGO  
           splitStatements:  true  
           stripComments:  true               
           path: sql/rollback_insert_JpaPdpPolicyDeploymentAudit.sql
           relativeToChangelogFile: true   

...

Code Block
languagetext
titlerollback_insert_JpaPdpPolicyDeploymentAudit.sql
delete from JpaPdpPolicyDeploymentAudit where id in (9,10);


Important concepts to be considered

  1. Labels and Context - to separate environments and releases
  2. Tags - to set  a rollback check point

...

  1. Prechecks - to check if a particular changeSet needs to be executed
  2. Different formats - decide the best one for the team to work with
  3. Include rollback statement with changeSet to allow user to undo changes. 


See Some best practices to keep in mind when using Liquibase for more information