Search code examples
postgresqlliquibaseliquibase-sql

How does one update a view with Liquibase on a Postgres database


I’m using Liquibase w/ a yaml changelog file. In my yaml I’m using something of the sort.

databaseChangeLog:
…
    - changeSet:
        id: unique-id-1
        author: unique-author
        - changes:
            - createView:
                schema_name: my_schema
                path: my_schema/views/vw_my_view.sql
                viewName: vw_my_view
                replaceIfExists: true

I run my liquibase update to create my view.

I then find I want to update my view (add columns, change columns, whatever), so I edit my original vw_my_view.sql.

When I go to create/append another changeset, liquibase flips out on me.

$ liquibase update
…
Starting Liquibase…
Liquibase Version: 4.27.0
Liquibase Open Source 4.27.0 by Liquibase
ERROR: Exception Details
ERROR: Exception Primary Class: ValidationFailedException
ERROR: Exception Primary Reason: Validation Failed:
    1 change sets check sum
        changelog/changelog-table.yml::unique-id-1::unique-author was 9:{{guid}} but is now 9:{{another guid}}

Do I need to generate a new .sql file for every view update? This kind of defeats the purpose of source controlling my original .sql file and the changelog.yml…doesn’t it?


Solution

  • While I agree that it might look confusing, short answer to your question is:

    No, it doesn't.

    The content of vw_my_view.sql is part of changeSet's checksum, so when you update vw_my_view.sql, it changes the checksum and Liquibase throws a checksum validation error.

    If you want to be able to make updates to vw_my_view.sql and not write new changeSets, you can add runOnChange attribute to your changeSet.

    Liquibase determines that a changeset has been modified by comparing the MD5 checksum for the changeset to the checksum stored in the DATABASECHANGELOG table.

    changeset:
          id: changeset1
          author: your.name
          runOnChange: true
          changes:
              .....