Reputation: 49
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
Reputation: 451
To setup Liquibase on Spring Framework application.
<dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> <version>4.6.2</version> </dependency>
@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.
<?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>
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
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
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