savvato-software / tribe-app-backend

3 stars 20 forks source link

Backend - liquibase error when changing between branches #250

Closed mrsbluerose closed 4 months ago

mrsbluerose commented 4 months ago

Steps to recreate error:

  1. On develop branch, drop the database and recreate it
  2. run the backend successfully
  3. switch to any feature branch and run the backend - this produces the error
  4. drop the database and re run the backend on the feature branch, and it runs successfully

Error snippet:

Caused by: liquibase.exception.CommandExecutionException: liquibase.exception.ValidationFailedException: Validation Failed:
     4 changesets check sum
          db/migration/changelog-v0.0.1.xml::v0.0.1-1::haxwell was: 9:8eb1badd85b04c0cb19e0a79462648ff but is now: 9:b8c772c3d71713e25e2c8560c33da2e1
          db/migration/changelog-v0.0.1.xml::v0.0.1-2::haxwell was: 9:4df2d9c55c51b5b75305f470b878ec6d but is now: 9:5d23cbc028195ec957e32cd9a1cc2a57
          db/migration/changelog-v0.0.1.xml::v0.0.1-3::haxwell was: 9:5d23cbc028195ec957e32cd9a1cc2a57 but is now: 9:0794bfcb21a63186bd926cc93591584a
          db/migration/changelog-202309010359.xml::202308300939-01::sun was: 9:40d2ca141e8565a7477a59f8f120007a but is now: 9:a133aa9f9a7a0f80d72879491f2dba0b

Presumably caused by: https://github.com/savvato-software/tribe-app-backend/pull/235, which updated some changeset data to context="test"

ChatGPT:

Why the Error Occurs

  1. Changes to context Attribute: Adding or modifying the context attribute in the changesets changes the way Liquibase interprets and processes those changesets, leading to different checksums.
  2. Checksum Mismatch: When Liquibase detects that the checksum of an already-applied changeset has changed, it raises an error because it expects the content and attributes of the changeset to remain the same after it has been applied.

Why Dropping and Recreating the Database Fixes It

When you drop and recreate the database, the DATABASECHANGELOG table is also recreated from scratch. This means that:

  1. Fresh Start: Liquibase starts with an empty DATABASECHANGELOG table and applies all changesets as if for the first time.
  2. New Checksums: The changesets are applied with their new checksums, so there are no mismatches.

ChatGPT suggestions:

  1. Ensure Consistent Migration Scripts Across Branches:
    • Make sure that your migration scripts are consistent across branches. Avoid modifying existing changesets; instead, create new changesets for new changes.
  2. Use Branch-specific Databases:
    • Consider using separate databases for different branches during development. This way, each branch has its own schema and migration history, preventing conflicts.
  3. Clear the DATABASECHANGELOG Table:
    • Before switching branches, you can clear the DATABASECHANGELOG table. This forces Liquibase to reapply the changesets. Be cautious with this approach, as it can lead to data loss if not handled properly.
mrsbluerose commented 4 months ago

I know it's best practice to avoid changing change sets. This was a seemingly unique change to make sure test data was properly identified. Apparently, that's a no go.

haxwell commented 4 months ago

@mrsbluerose I was able to reproduce this. No, your change was okay. I dropped the ball getting it merged to the feature branches. That's done now, so once folks sync their repos, we should be good on this issue. I've tested, and it works on my box.

mrsbluerose commented 4 months ago

It worked on every branch except feature/attribute-phrase-review. All other branches had updates and worked after I synced and pulled, but this one branch didn't get the merge from develop.