I'm using spring-boot with the liquibase-maven-plugin to generate database changes according to my classes, but the "mvn compile liquibase: diff" command always generates removals and inclusions of indexes and foreign keys even though the database is updated and has no change in the classes (and therefore should have no change in the database).
Anyone have any idea if this is right or how to avoid it? I want only the new changes to database to be generated in change sets of the Project.
The diff command in Liquibase allows you to compare two databases of the same type, or different types, to one another.
Hibernate can be used with several databases that are supported by Liquibase, such as H2. To use an H2 database with Liquibase, you must have the H2 JDBC driver JAR file., which is pre-installed with Liquibase.
The Liquibase Spring Boot integration ensures the application database is updated along with the application code by using Spring Boot auto-configuration and features. To use Liquibase and Spring Boot: Install Maven and add it to your path. Ensure you have Java Development Kit (JDK 8, 11, or 16).
First of all, I think you are missing the liquibase-hibernate4 maven plugin.
From the project Readme.md
:
This extension lets you use your Hibernate configuration as a comparison database for diff, diffChangeLog and generateChangeLog in Liquibase.
Which actually means that you can use it to compare the real database with your java entities in order to generate the new changelogs.
As the project wiki suggest, it's important to keep in mind that you need to take a look to the new changelogs and modify it manually if there is something wrong.
I also recommend you to read this article from Baeldung that explains that:
Your pom.xml
should look like:
...
<dependencies>
...
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.4.1</version>
</dependency>
...
</dependencies>
...
<plugins>
...
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
</configuration>
<dependencies>
<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-hibernate4</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.7.3.RELEASE</version>
</dependency>
</dependencies>
</plugin>
...
</plugins>
...
And your src/main/resources/liquibase.properties
:
url=jdbc:mysql://localhost:3306/your_db
username=your_user
password=your_pw
driver=com.mysql.jdbc.Driver #orYourDriver
outputChangeLogFile=src/main/resources/liquibase-outputChangeLog.xml
hibernate:spring:your.model.package?dialect=org.hibernate.dialect.MySQLDialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
Im not sure which persistence storage are you using but please be sure to use the correct drivers and datasource urls.
After configure it completely, you should be able to run mvn liquibase:diffChangeLog
to generate the new changelogs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With