0
votes

I can use liquibase to change my database's schema, it's good. But I need to get the changeset during a period, for example

1/1: add table1 => 1.0
1/10: add table2
1/18: alter table1 => 2.0
1/21: alter table2 
1/28: add table3 => 3.0

If I want to get a changeset (SQL command) from 1/10~1/28 or 1/18~1/21, is it possible?

[Update] Thanks for Mark's reply, I add version in above changeset in our development environment. And when our production database is in 1.0 and I want to upgrade to 2.0 (or 3.0) directly but just want use liquibase to generate SQL command and run it manually.

Rollback has a problem that if I rollback my development database, I need to re-create the data already on it. Is there any suggestion for it?

1

1 Answers

0
votes

I suggest reading the command line documentation.

To generate SQL based on date the only way I think it can be done is as follows:

liquibase rollbackToDate <date/time> 
liquibase tag <version so can rollback here again>
liquibase updateSQL > keepUpdate.sql
liquibase update

Update

What you're attempting to do (use SQL to update production) is a common use-case that liquibase is designed to support. It's one of my favourite features.

Problem is.... In an ideal world you should be able to blow-away you dev database and start from scratch. Obviously you don't have liquibase changesets to manage your data , so need to consider some alternatives....

Approach 1: Create a new DEV database

Run a SQL query and establish how many changsets there are on your development DB:

SELECT ORDEREXECUTED, DATEEXECUTED, ID, FILENAME, DESCRIPTION, COMMENTS 
FROM DATABASECHANGELOG 
ORDER BY ORDEREXECUTED

Analyze the dates and establish how many chnagesets would take you back from version 3.0 to 2.0 (2 changesets) and 1.0 (4 changesets)

Create a fresh database and apply your liquibase changesets, bring your schema up to revision 3.0

liquibase update

To create the SQL for version 2.0 -> 3.0 upgrade

liquibase rollbackCount 2
liquibase updateSQL         > upgrade-2_0-3_0.sql
liquibase futureRollbackSQL > rollback-2_0-3_0.sql

And create the SQL for version 1.0 -> 3.0 upgrade

liquibase rollbackCount 2
liquibase updateSQL         > upgrade-1_0-3_0.sql
liquibase futureRollbackSQL > rollback-1_0-3_0.sql

Approach 2: Use dbunit to save datasets

Another really useful tool for database management is dbunit

The following article explains how liquibase and dbunit could be used together to migrate databases:

The two tools are complementary. In your case perhaps you could periodically save snaphots of your development data and use this to repopulate development or test databases (Obviously a dataset is sensitive to the version of a db schema, so some care is required)