Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work with liquibase, a concrete example

Following the quickstart on liquibase i've created a changeset (very dumb :) )

Code:

<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.6"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.6
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.6.xsd">

    <changeSet id="1" author="me">
        <createTable tableName="first_table">
            <column name="id" type="int">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <createTable tableName="new_table">
            <column name="id" type="int">
                <constraints primaryKey="true" nullable="false"/>
            </column>
        </createTable>
    </changeSet>

</databaseChangeLog>

I've created a clean schema and i've launched the migrate command.

Liquibase created the database, with the support tables databasechangelog and ..lock.

Now how i can track the changes?? i've modified the changeset adding a new createTable element but when i try the command "update" liquibase tells me this

Migration Failed: Validation Failed:
     1 change sets check sum

so i don't think to have understood the way to work with liquibase.

Someone may point me to the right direction??

Thanks

like image 879
apelliciari Avatar asked Jul 18 '09 21:07

apelliciari


People also ask

How do you write a liquibase script?

Liquibase tutorial: SummaryAlways add your changesets to a changelog ( don't change anything without Liquibase!) – changeset should be unique combining AUTHOR:ID(task) and filename (file with your changelog) Verify the SQL you will execute ( always run updateSQL before update command). Run database update command.

How do you make a table with liquibase?

To create a table for your database, follow these steps: Step 1: Add the createTable Change Type to your changeset with the needed attributes as it is shown in the examples. Step 2: Deploy your changeset by running the update command. Now, you should see a new table.


3 Answers

You should never modify a <changeSet> that was already executed. Liquibase calculates checksums for all executed changeSets and stores them in the log. It will then recalculate that checksum, compare it to the stored ones and fail the next time you run it if the checksums differ.

What you need to do instead is to add another <changeSet> and put your new createTable element in it.

QuickStart is good readin' but it is indeed quick :-) Check out the full manual, particularly its ChangeSet section.

like image 146
ChssPly76 Avatar answered Nov 11 '22 15:11

ChssPly76


This currently accepted answer is slightly out of date based on changes in Liquibase 2.x. In the 2.x version, Liquibase will still fail if the md5 checksum has changed for a changeset, but you can specify the runOnChange attribute if you want to be able modify it.

From the documentation:

runOnChange - Executes the change the first time it is seen and each time the change set has been changed

like image 33
Javid Jamae Avatar answered Nov 11 '22 14:11

Javid Jamae


If it's a change to a changeset that basically has already been done, you can manually modify the database so that its md5 for that changeset matches the new one. Good for minor textual changes. Or you can delete that changeset row from your table.

like image 32
rogerdpack Avatar answered Nov 11 '22 14:11

rogerdpack