Spring boot liquibase. Execute DIFF between entities and database

Viewed 923

I am trying to set up liquibase in my spring boot application. What I am trying is to create change sets between my Entities and my DB with mvn liquibase:diff

Here is example of one of my entities:

package com.example.start.project.entity;

import static com.example.start.project.entity.DatabaseConstants.CREATED_AT;
import static com.example.start.project.entity.DatabaseConstants.CREATED_BY;
import static com.example.start.project.entity.DatabaseConstants.LEGACY_ID;
import static com.example.start.project.entity.DatabaseConstants.MODIFIED_AT;
import static com.example.start.project.entity.DatabaseConstants.MODIFIED_BY;
import static com.example.start.project.entity.DatabaseConstants.SCHEMA_NAME;
import static com.example.start.project.entity.DatabaseConstants.TABLE_PREFIX;
import static com.example.start.project.entity.PlantEntity.TSN_PREFIX;

import java.time.ZonedDateTime;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.data.annotation.Immutable;

@Entity
@Table(schema = SCHEMA_NAME, name = TABLE_PREFIX + TSN_PREFIX + "PLANT")
@Immutable
public class PlantEntity {

  /** Table short name. */
  static final String TSN = "PLNT";
  /** Table short name as prefix. */
  static final String TSN_PREFIX = TSN + "_";
  /** Id column name */
  static final String ID_COLUMN = TSN_PREFIX + DatabaseConstants.ID;

  private static final long serialVersionUID = 1L;

  @Id
  @Column(name = ID_COLUMN)
  private UUID id;

  @Column(name = TSN_PREFIX + LEGACY_ID)
  private Long legacyId;

  @Column(name = TSN_PREFIX + "NUMBER")
  private String number;

  @Column(name = TSN_PREFIX + "NAME")
  private String name;

  @Column(name = TSN_PREFIX + "TYPE")
  private String type;

  @Column(name = TSN_PREFIX + CREATED_BY)
  private String createdBy;

  @Column(name = TSN_PREFIX + CREATED_AT)
  private ZonedDateTime createdAt;

  @Column(name = TSN_PREFIX + MODIFIED_BY)
  private String modifiedBy;

  @Column(name = TSN_PREFIX + MODIFIED_AT)
  private ZonedDateTime modifiedAt;

  public UUID getId() {
    return id;
  }

  public void setId(UUID id) {
    this.id = id;
  }

  public Long getLegacyId() {
    return legacyId;
  }

  public void setLegacyId(Long legacyId) {
    this.legacyId = legacyId;
  }

  public String getNumber() {
    return number;
  }

  public void setNumber(String number) {
    this.number = number;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public String getCreatedBy() {
    return createdBy;
  }

  public void setCreatedBy(String createdBy) {
    this.createdBy = createdBy;
  }

  public ZonedDateTime getCreatedAt() {
    return createdAt;
  }

  public void setCreatedAt(ZonedDateTime createdAt) {
    this.createdAt = createdAt;
  }

  public String getModifiedBy() {
    return modifiedBy;
  }

  public void setModifiedBy(String modifiedBy) {
    this.modifiedBy = modifiedBy;
  }

  public ZonedDateTime getModifiedAt() {
    return modifiedAt;
  }

  public void setModifiedAt(ZonedDateTime modifiedAt) {
    this.modifiedAt = modifiedAt;
  }
}

And here is my POM looks like:

*******
<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-core</artifactId>
        <version>3.8.8</version>
      </dependency>
      <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-cdi</artifactId>
        <version>3.8.8</version>
      </dependency>
      <dependency>
        <groupId>org.liquibase.ext</groupId>
        <artifactId>liquibase-hibernate5</artifactId>
        <version>3.8</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  *********
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.liquibase</groupId>
          <artifactId>liquibase-maven-plugin</artifactId>
          <version>3.8.8</version>
          <configuration>
            <propertyFile>liquibase/liquibase.properties</propertyFile>
            <changeLogFile>liquibase/changeLogs/database.yaml</changeLogFile>

            <driver>${test.jdbc.driver}</driver>
            <url>${test.jdbc.url}?currentSchema=${test.jdbc.schema}</url>
            <username>${test.jdbc.userName}</username>
            <password>${test.jdbc.password}</password>

            <!--defaultSchemaName>${test.jdbc.schema}</defaultSchemaName>-->
            <changelogSchemaName>${test.jdbc.schema}</changelogSchemaName>

            <referenceDriver>liquibase.ext.hibernate.database.connection.HibernateDriver</referenceDriver>
            <referenceUrl>hibernate:spring:com.example.start.project.entity?dialect=org.hibernate.dialect.PostgreSQL95Dialect</referenceUrl>

             <diffChangeLogFile>${project.build.directory}/startup.db-diff.yaml</diffChangeLogFile>
             <outputChangeLogFile>${project.build.directory}/db-initial.yaml</outputChangeLogFile>

             <!-- Syntax: [objecttype from liquibase.structure.core]:[regex matching name] -->
            <diffExcludeObjects>sequence:.*_SEQ</diffExcludeObjects>
            <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
            <logging>debug</logging>

          </configuration>
          <executions>
            <execution>
              <phase>process-resources</phase>
              <goals>
                <goal>update</goal>
              </goals>
            </execution>
          </executions>
          <dependencies>
            <dependency>
              <groupId>${test.jdbc.groupId}</groupId>
              <artifactId>${test.jdbc.artifactId}</artifactId>
              <version>${test.jdbc.version}</version>
            </dependency>

            <dependency>
              <groupId>javax.validation</groupId>
              <artifactId>validation-api</artifactId>
              <version>${java-validation-api.version}</version>
            </dependency>

            <dependency>
              <groupId>org.liquibase.ext</groupId>
              <artifactId>liquibase-hibernate5</artifactId>
              <version>${liquibase.hibernate.version}</version>
            </dependency>

            <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-data-jpa</artifactId>
              <version>2.2.5.RELEASE</version>
            </dependency>
          </dependencies>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

When I run mvn liquibase:diff i got No changesets to add. even I have few entities and my DB is empty. DB is Postgress.

I think that issue is with referenceUrl:

<referenceUrl>hibernate:spring:com.example.start.project.entity?dialect=org.hibernate.dialect.PostgreSQL95Dialect</referenceUrl>

and that liquibase is not able to find my entities.

Entities are placed in different maven module than from where I am running mvn liquibase:diff but this module has reference to it.

Does anyone see what I am doing wrong. Thanks in advance.

1 Answers

You can add this liquibase plugin with a path to liquibase properties file:

            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>3.4.1</version>
                <configuration>
                    <propertyFile>src/main/resources/liquibase.properties</propertyFile>
                </configuration>
            </plugin>

And liquibase.properties should look like this:

url=jdbc:oracle:thin:@1.1.1.1:1521:ORCL
username=name
password=pas
driver=oracle.jdbc.driver.OracleDriver
outputChangeLogFile=src/main/resources/liquibase-outputChangeLog.xml
changeLogFile=classpath:/db/changelog/db.changelog-master.xml
referenceUrl=hibernate:spring:com.your.model?dialect=org.hibernate.dialect.Oracle12cDialect
diffChangeLogFile=target/db.changelog-diff.xml

Then: mvn liquibase:diff

Related