2
votes

My group is considering using Liquibase within our Spring Boot startup. I'm concerned about accidentally rolling back schema changes. My scenario works like this:

  1. Liquibase changelogs 1, 2, and 3 exist.
  2. The db-changelog-master.xml mentions each of changelogs 1, 2, 3.
  3. Spring Boot is started, and liquibase confirms that the database is configured as requested.
  4. For another Spring Boot startup, a user has an obsolete Spring Boot configuration. This configuration has an old db-changelog.master.xml containing only changelogs 1, 2.
  5. When this variant Spring Boot starts up, does it automatically rollback the database schema, so it matches the changelogs 1, 2?

Is liquibase smart enough to NOT do an implicit rollback when the user doesn't explicitly run a rollback request? Or is there some configuration that must be made to prevent my scenario?

1

1 Answers

0
votes

To answer your question - No, Liquibase will not roll back your schema by it's own will.

Let's review case #4.

When Spring-Boot will be started with obsolete version of changeLogs, Liquibase will try to execute only the given set (the obsolete one) of changeLogs (which haven't been run already).

Liquibase is interested only in the given set of changeSets in the given set of changeLogs. Everything else in the schema will be non of Liquibase's concern, e.g. if some_new_table exists in the db schema and it's not described in the given set of changeSets, this table will not be dropped.

BUT

in order for this to work you have to keep in mind the following:

  • When Liquibase executes a changeSet, it stores some metadata about the execution in databasechangelog table. Liquibase does it in order to keep an eye on changeSet's integrity and in order not to execute it againg if it has already been executed

  • you have to write preConditions for your changeSets to ensure that the changeSet will not be executed if it should not be executed. For example:

      <changeSet id="foo" author="bar">
          <preConditions onFail="MARK_RAN">
              <not>
                  <tableExists tableName="some_new_table"/>
              </not>
          </preConditions>
          <comment>Create some_new_table table</comment>
          <createTable tableName="some_new_table">
             <!-- your columns here -->
          </createTable>
      </changeSet>
    

    This way Liquibase will check if some_new_table exists before trying to create it.