Liquibase不会更新内存中的h2数据库

时间:2019-12-22 14:15:58

标签: java spring spring-boot maven liquibase

我建立了一个简单的spring-boot-application,并希望包括liquibase来进行数据库迁移。我定义了一个master.xml,它由对另一个文件的单个引用组成。该文件定义了一个变更集,用于创建名称为test的表。到目前为止,一切似乎都还不错。但是,在尝试通过执行liquibase:update通过liquibase-maven-plugin应用更改之后,将不会创建我的test表。

我在application-propertiespom.xml中做错了吗?任何提示都会有所帮助:)

代码在这里:

master.xml

<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-3.1.xsd">
    <include relativeToChangelogFile="true" file="migration/0000-init-schema.xml"/>
</databaseChangeLog>

0000-init-schema.xml

<databaseChangeLog logicalFilePath="db.changelog-1.0.xml" 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-3.4.xsd">
    <changeSet author="kezersa72274" id="2020-01-01-1">
        <comment>Create table test.</comment>

        <createTable tableName="test">
            <column name="id" type="BIGINT">
                <constraints nullable="false" primaryKey="true"/>
            </column>
        </createTable>
    </changeSet>
</databaseChangeLog>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>de.digiplan</groupId>
    <artifactId>backend</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>digiplan-backend</name>
    <description>Digiplan-Backend</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.197</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <configuration>
                    <changeLogFile>
                        /src/main/resources/master.xml
                    </changeLogFile>
                    <driver>org.h2.Driver</driver>
                    <url>jdbc:h2:mem:digiplan</url>
                    <username>sa</username>
                    <password></password>
                </configuration>
                <version>3.6.2</version>
            </plugin>
        </plugins>
    </build>
</project>

application.properties

spring.jpa.hibernate.ddl-auto=none

spring.datasource.name=digiplan
spring.datasource.url=jdbc:h2:mem:digiplan
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

BackendApplication.java

package de.digiplan.backend;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BackendApplication {
    public static void main(String[] args) {
        SpringApplication.run(BackendApplication.class, args);
    }
}

BackendApplicationTests.java

package de.digiplan.backend;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public final class BackendApplicationTests {
    @Test
    void contextLoads() {
    }
}

调用liquibase:update的日志:

"/Applications/IntelliJ IDEA.app/Contents/jbr/Contents/Home/bin/java" -Dmaven.multiModuleProjectDirectory=/Users/saltukkezer/digiplan-backend "-Dmaven.home=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3" "-Dclassworlds.conf=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/bin/m2.conf" "-Dmaven.ext.class.path=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven-event-listener.jar" "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=64094:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds-2.6.0.jar" org.codehaus.classworlds.Launcher -Didea.version2019.2 org.liquibase:liquibase-maven-plugin:3.6.2:update
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< de.digiplan:backend >-------------------------
[INFO] Building digiplan-backend 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- liquibase-maven-plugin:3.6.2:update (default-cli) @ backend ---
[INFO] ------------------------------------------------------------------------
[INFO] Starting Liquibase at Sun, 22 Dec 2019 15:39:29 CET (version 3.6.2 built at 2018-07-03 11:28:09)
[INFO] Executing on Database: jdbc:h2:mem:digiplan
[INFO] SELECT COUNT(*) FROM DATABASECHANGELOGLOCK
[INFO] CREATE TABLE DATABASECHANGELOGLOCK (ID INT NOT NULL, LOCKED BOOLEAN NOT NULL, LOCKGRANTED TIMESTAMP, LOCKEDBY VARCHAR(255), CONSTRAINT PK_DATABASECHANGELOGLOCK PRIMARY KEY (ID))
[INFO] SELECT COUNT(*) FROM DATABASECHANGELOGLOCK
[INFO] DELETE FROM DATABASECHANGELOGLOCK
[INFO] INSERT INTO DATABASECHANGELOGLOCK (ID, LOCKED) VALUES (1, FALSE)
[INFO] SELECT LOCKED FROM DATABASECHANGELOGLOCK WHERE ID=1
[INFO] Successfully acquired change log lock
[INFO] Creating database history table with name: DATABASECHANGELOG
[INFO] CREATE TABLE DATABASECHANGELOG (ID VARCHAR(255) NOT NULL, AUTHOR VARCHAR(255) NOT NULL, FILENAME VARCHAR(255) NOT NULL, DATEEXECUTED TIMESTAMP NOT NULL, ORDEREXECUTED INT NOT NULL, EXECTYPE VARCHAR(10) NOT NULL, MD5SUM VARCHAR(35), DESCRIPTION VARCHAR(255), COMMENTS VARCHAR(255), TAG VARCHAR(255), LIQUIBASE VARCHAR(20), CONTEXTS VARCHAR(255), LABELS VARCHAR(255), DEPLOYMENT_ID VARCHAR(10))
[INFO] SELECT COUNT(*) FROM DATABASECHANGELOG
[INFO] Reading from DATABASECHANGELOG
[INFO] SELECT * FROM DATABASECHANGELOG ORDER BY DATEEXECUTED ASC, ORDEREXECUTED ASC
[INFO] SELECT COUNT(*) FROM DATABASECHANGELOGLOCK
[INFO] CREATE TABLE test (id BIGINT NOT NULL, CONSTRAINT PK_TEST PRIMARY KEY (id))
[INFO] Table test created
[INFO] ChangeSet db.changelog-1.0.xml::2020-01-01-1::kezersa72274 ran successfully in 1ms
[INFO] SELECT MAX(ORDEREXECUTED) FROM DATABASECHANGELOG
[INFO] INSERT INTO DATABASECHANGELOG (ID, AUTHOR, FILENAME, DATEEXECUTED, ORDEREXECUTED, MD5SUM, DESCRIPTION, COMMENTS, EXECTYPE, CONTEXTS, LABELS, LIQUIBASE, DEPLOYMENT_ID) VALUES ('2020-01-01-1', 'kezersa72274', 'db.changelog-1.0.xml', NOW(), 1, '8:86a27a6c6ac5d44947040cf6a87f248c', 'createTable tableName=test', 'Create table test.', 'EXECUTED', NULL, NULL, '3.6.2', '7025570948')
[INFO] Successfully released change log lock
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.600 s
[INFO] Finished at: 2019-12-22T15:39:30+01:00
[INFO] ------------------------------------------------------------------------

-> What the database looks like

2 个答案:

答案 0 :(得分:0)

在您的示例中,您不要求spring-boot在启动时启动liquibase。添加csv

在您的应用程序中(对yml格式很抱歉)

在那之后,您可以从pom.xml中删除数据库的配置,因为liquibase会将spring配置用于数据库。

NB:yml格式,类名的属性为'driver-class-name'

答案 1 :(得分:0)

从日志中可以明显看出liquibase已运行并创建了数据库,但是由于它是H2且配置为内存,因此一旦liquibase更新结束,更改将丢失。如果您使用了其他RDBMS,则这些更改本来可以保存的。

为了使H2与您的项目一起使用,您需要通过更改liquibase maven插件中的URL,使H2在文件系统上持久化数据库。

<plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
         <groupId>org.liquibase</groupId>
         <artifactId>liquibase-maven-plugin</artifactId>
         <configuration>
            <changeLogFile>
               /src/main/resources/master.xml
            </changeLogFile>
            <driver>org.h2.Driver</driver>
            <url>jdbc:h2:file:~/digiplan</url>
            <username>sa</username>
            <password></password>
         </configuration>
         <version>3.6.2</version>
      </plugin>

还从

更改spring.datasource.url属性。

jdbc:h2:mem:digiplan到jdbc:h2:file:〜/ digiplan