noob: how to handle both new and existing databases

My story:
I have an app using Spring and hibernate annotations.  Made some schema changes that resulted in errors w/ an existing DB.  Added liquibase Spring-bean and a db-changelog.xml that made the necessary schema adjustments, and the errors went away when I started my app again and everything was great.

I then deleted the DB so my app would regenerate a fresh DB on startup, as it does.  Started my app and the liquibase Spring-bean barfed on load because it tried to do a dropColumn on a column that didn’t exist.

I fixed it with a precondition on the dropColumn changeSet. 

So, how does liquibase handle both new and existing DBs?  And shouldn’t this question be in the FAQ?

Do I have to include preconditions with every changeSet?  If so, perhaps this should be added as a “Con” to your presentations, as the database version strategy does not impose this requirement.

Originally posted by: mrpantsuit
So, how does liquibase handle both new and existing DBs?  And shouldn't this question be in the FAQ?

It’s very easy once you adopt any strategy for managing schema/data changes.

The one that IMO works best (and is easiest to understand) is “manage it all with liquibase”.

If you create your schema with Liquibase, update it with liquibase, and do not allow other tools (such as hibernate, or manually crafted scripts) to modify the schema behind liquibase’s back, then it will work just great for new & legacy DBs.

I found that introducing liquibase to existing databases is quite easy, and my preferred way is:

  1. generate a changelog from your existing schema. The liquibase CLI can do that for you. I usually take the resulting XML and smooth it out a bit (group related changes into single changelogs, do vendor-specific cleanups and so on), but Liquibase does most of the legwork.

  2. run that changelog against the existing database, but only marking it as applied (without actually modifying the schema).

  3. use liquibase for applying new changes from that point on.

That way, if you need to drop your DB, or simply create a new one, you have a liquibase changelog that can completely reconstruct the schema from scratch, without relying on hibernate.

Grzegorz, do you have a command line to do this? Noob on liquibase and the help isn’t helpful. Would think it’s a diff but not sure how to get it to diff non-existing to existing (and thus capture the schema).


In the command-line liquibase version it’s a dedicated option:

thanks! was using the diffChangeLog against an auto created DB (always empty) – this is much more clear.