stackMVC
stackMVC

Reputation: 49

How can I use liquibase in Spring Project

I am trying learn liquibase. But I couldn't apply steps in samples. Can you tell me step by step how can I add a table or column to my database and see changes ?

My project is a Spring MVC project and I use Maven, Hibernate, PostgreSQL and I change database programmatically.

Upvotes: 2

Views: 2191

Answers (3)

Olavi Vaino
Olavi Vaino

Reputation: 451

To setup Liquibase on Spring Framework application.

  1. Add dependency
<dependency>
  <groupId>org.liquibase</groupId>
  <artifactId>liquibase-core</artifactId>
  <version>4.6.2</version>
</dependency>
  1. Add bean
@Bean
public SpringLiquibase springLiquibase(DataSource dataSource) {
    SpringLiquibase springLiquibase = new SpringLiquibase();
    springLiquibase.setDataSource(dataSource);
    springLiquibase.setChangeLog("db/changelog/db.changelog-master.xml");
    return springLiquibase;
}

where "db/changelog/" is default and recommended location for changelog files.

  1. Add "db.changelog-master.xml" file into "db/changelog/" folder under resources. Add following content to it, at least:
<?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog   http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd">   
<includeAll  path="db/migrations"/> 
</databaseChangeLog>
  1. Add one changelog file into "db/migrations" folder, for example changelog_base.sql. Its content can be trivial SQL sentence or just SQL comment.

When build system does not include all resource files by default you may need to configure it to do so. And application also needs to have DB connection properly configured and setup by Spring.

Now run the application and look for output log. If all is fine then there should be some output from liquibase, something like:

25-Jan-2022 14:36:53.088 INFO [localhost-startStop-12] liquibase.database.null Set default schema name to public
25-Jan-2022 14:36:53.217 INFO [localhost-startStop-12] liquibase.lockservice.null Successfully acquired change log lock
25-Jan-2022 14:36:53.332 INFO [localhost-startStop-12] liquibase.changelog.null Reading resource: db/migrations/changelog_base.sql
25-Jan-2022 14:36:53.453 INFO [localhost-startStop-12] liquibase.changelog.null Reading from public.databasechangelog
25-Jan-2022 14:36:53.506 INFO [localhost-startStop-12] liquibase.lockservice.null Successfully released change log lock

Check the database - it should have new tables "databasechangelog" and "databasechangeloglock". And some content in databasechangelog table.

Upvotes: 0

Pramod H G
Pramod H G

Reputation: 1613

Add the following maven dependency in the pom.xml,

<!-- https://mvnrepository.com/artifact/org.liquibase/liquibase-core -->
<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
    <version>2.0.3</version>
</dependency>

Add the following liquibase bean to the applicationcontext.xml

<bean id="LiquibaseUpdater" class="liquibase.integration.spring.SpringLiquibase">
    <property name="dataSource" ref="dataSource" />
    <property name="changeLog" value="classpath:db-changelog.xml" />
</bean>

Add the db-changelog.xml in the classpath, Ex:

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">

<changeSet author="John" id="add-column" onValidationFail="MARK_RAN" failOnError="false">
    <preConditions onFail="MARK_RAN">
        <or>
            <not>
                <columnExists tableName="TABLENAME" columnName="NEWCOLUMN"/>
            </not>
        </or>
    </preConditions>
    <addColumn tableName="TABLENAME">
        <column name="NEWCOLUMN" type="VARCHAR(50)"/>
    </addColumn>
</changeSet>

Upvotes: 0

Amogh
Amogh

Reputation: 4573

You will need liquibase and hibernate jars. Consider you have a pojo class person with Id,Name,Gender properties. Create getters and setters of these properties.

You need to create liquibase file (db-changelog.xml)

For example :

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">

    <changeSet author="rover" id="123456789-1">
        <createTable tableName="PERSON">
            <column autoIncrement="true" name="PERSON_ID" type="BIGINT">
                <constraints nullable="false" primaryKey="true" />
            </column>
            <column name="NAME" type="VARCHAR(255)" />
            <column name="GENDER" type="VARCHAR(2)" />           
        </createTable>
    </changeSet>
</databaseChangeLog>

Don't forget to add liquibase bean in your bean

<bean id="LiquibaseUpdater" class="liquibase.integration.spring.SpringLiquibase">
    <property name="dataSource" ref="dataSource" />
    <property name="changeLog" value="classpath:db-changelog.xml" />
</bean>

You also need to add spring/hibernate beans.

Upvotes: 1

Related Questions