Applies to:
- Liquibase Pro
- Liquibase Open Source (Community)
Summary:
The runOnChange attribute is a powerful attribute that allows changesets to be successfully deployed again if the content of the changeset has changed. This is excellent for creating Views or Stored Procedures that change from initial deployment. The attribute can be added to any changeset.
One common issue encountered deals with using the runOnChange for DML operation. If any statements require specific columns, tables, etc., ensure that these statements will be valid during the deployment.
Our documentation on runOnChange shows how to use the attribute on each style of changesets (XML, SQL, YAML, JSON), but there are specific scenarios where runOnChange will work differently than expected.
How to:
Multiple items in the changeset
Suppose a changeset has multiple files, statements, etc. In that case, the expected behavior is that if any of the items inside the changeset changes, it will execute everything inside of the changeset. Therefore, it is best practice to separate as much as possible into their own changesets.
Example 1:
main_changelog.xml
<changeSet id="1234" author="liquibase" runOnChange="true">
<sqlFile dbms="..." path="./insert.sql" />
<sqlFile dbms="..." path="./insert2.sql" />
</changeSet>
Changing the contents of either insert.sql or insert2.sql will result in the changeset being deployed again and executing the content of both insert.sql and insert2.sql.
SQL file referenced in the XML file
When there is an SQL file referenced in an XML file, Liquibase will only register the SQL information ignoring any comments, including Liquibase formatted SQL. In this case, the runOnChange (and any attributes) must be set at the XML level. If runOnchange is set in the SQL file, it will be ignored.
During the initial deployment, main_changelog.xml will execute the SQL inside of insert.sql. After which, if insert.sql is changed, this will result in a checksum error because the Liquibase will not pick up the attribute nested inside a file.
Example 1:
main_changelog.xml
<changeSet id="1234" author="liquibase">
<sqlFile dbms="..." path="./insert.sql" />
</changeSet>
insert.sql
-- liquibase formatted sql
-- changeset liquibase:2 runOnChange:true
INSERT INTO ... (...);
Changing the SQL in insert.sql would result in a checksum error.
Example 2:
main_changelog.xml
<changeSet id="1234" author="liquibase" runOnChange="true">
<sqlFile dbms="..." path="./insert.sql" />
</changeSet>
insert.sql
-- liquibase formatted sql
-- changeset liquibase:2
INSERT INTO ... (...);
Changing the SQL in insert.sql would result in the changeset inside main_changelog.xml being rerun.
Comments
0 comments
Article is closed for comments.