lu_lu
lu_lu

Reputation: 173

How to create Liquibase changesets automatically?

I want to generate a changelog file automatically via one of the maven liquibase plugin commands. The changelog should include a changeset for creating a database table for the DiscountCode entity. Would diff be the correct plugin command for this case?

@Entity
@Table(name = "discount_code")
public class DiscountCode {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long discountId;

    private String discountCode;
    private LocalDate expiration;
    private BigDecimal discountValue;

    public DiscountCode() {}

    public DiscountCode(String discountCode, LocalDate expiration, BigDecimal discountValue) {
        this.discountCode = discountCode;
        this.expiration = expiration;
        this.discountValue = discountValue;
    }

    public Long getDiscountId() {
        return discountId;
    }

    public void setDiscountId(Long discountId) {
        this.discountId = discountId;
    }

    public String getDiscountCode() {
        return discountCode;
    }

    public void setDiscountCode(String discountCode) {
        this.discountCode = discountCode;
    }

    public LocalDate getExpiration() {
        return expiration;
    }

    public void setExpiration(LocalDate expiration) {
        this.expiration = expiration;
    }

    public BigDecimal getDiscountValue() {
        return discountValue;
    }

    public void setDiscountValue(BigDecimal discountValue) {
        this.discountValue = discountValue;
    }

    @Override
    public String toString() {
        return (
            "DiscountCode{" +
            "discountId=" +
            discountId +
            ", discountCode='" +
            discountCode +
            '\'' +
            ", expiration=" +
            expiration +
            ", discountValue=" +
            discountValue +
            '}'
        );
    }
}

These are the available liquibase commands: liquibase plugin commands

Upvotes: 2

Views: 1929

Answers (1)

lu_lu
lu_lu

Reputation: 173

The solution is using the Liquibase Hibernate plugin.

The plugin configuration:

<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>

Liquibase properties:

changeLogFile=classpath:liquibase-changeLog.xml
url=jdbc:mysql://localhost:3306/oauth_reddit
username=tutorialuser
password=tutorialmy5ql
driver=com.mysql.jdbc.Driver
referenceUrl=hibernate:spring:org.baeldung.persistence.model
  ?dialect=org.hibernate.dialect.MySQLDialect
diffChangeLogFile=src/main/resources/liquibase-diff-changeLog.xml

View this page for further information: https://www.baeldung.com/liquibase-refactor-schema-of-java-app

Upvotes: 2

Related Questions