我正在尝试使用Flyway在Spring Boot应用程序中对嵌入式H2数据库进行迁移演示。
application.properties
logging.level.org.org.springframework=DEBUG
server.port=8181
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.flyway.baseline-on-migrate=true
spring.jpa.hibernate.ddl-auto=none
db / migration 下的migration-script(V2__create_shipwreck.sql)
CREATE TABLE SHIPWRECK(ID INT AUTO_INCREMENT,
NAME VARCHAR(255),
DESCRIPTION VARCHAR(2000),
CONDITION VARCHAR(255),
DEPTH INT,
LATITUDE DOUBLE,
LANGITUDE DOUBLE,
YEARS_DISCOERED INT);
控制台日志
INFO 7284 --- [main] o.f.c.internal.database.DatabaseFactory: 数据库:jdbc:h2:mem:testdb(H2 1.4)
INFO 7284 --- [main] o.f.core.internal.command.DbValidate: 成功验证了1次迁移(执行时间00:00.031s)
INFO 7284 --- [main] o.f.c.i.s.JdbcTableSchemaHistory:创建架构 历史表:" PUBLIC"。" flyway_schema_history"
INFO 7284 --- [main] o.f.core.internal.command.DbMigrate:Current 架构的版本" PUBLIC":<<空架构>>
INFO 7284 --- [main] o.f.core.internal.command.DbMigrate:Migrating schema" PUBLIC"到版本2 - 创建沉船
INFO 7284 --- [main] o.f.core.internal.command.DbMigrate: 成功应用了1个迁移到架构" PUBLIC" (执行时间处理时间 00:00.098s)
INFO 7284 --- [main] j.LocalContainerEntityManagerFactoryBean: 为持久性单元构建JPA容器EntityManagerFactory '默认'
INFO 7284 --- [main] o.hibernate.jpa.internal.util.LogHelper: HHH000204:处理PersistenceUnitInfo [name:default ...]
main] org.hibernate.Version:HHH000412:Hibernate 核心{5.2.14.Final}
main] org.hibernate.cfg.Environment:HHH000206: 找不到hibernate.properties
main] o.hibernate.annotations.common.Version:HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
main] org.hibernate.dialect.Dialect:HHH000400:正在使用 方言:org.hibernate.dialect.H2Dialect
main] j.LocalContainerEntityManagerFactoryBean:初始化的JPA 用于持久性单元的EntityManagerFactory'默认'
的pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.boot</groupId>
<artifactId>das-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>das-boot</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>5.0.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.0.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
H2数据库表UI
启动Spring Boot应用程序后,表格尚未创建,那么问题是什么?
答案 0 :(得分:2)
根据您的描述(Flyway执行正常,但之后没有观察到架构更改),这听起来像Spring Boot不会持续H2更改。您可以尝试将spring.jpa.hibernate.ddl-auto=none
添加到application.properties
,以便JPA配置不会覆盖Flyway迁移后的架构更改,具体为this question。
答案 1 :(得分:1)
我遇到了同样的问题,将application.properties更改为 datasource.url到文件,而不是 mem
能够创建带有详细信息的表格
正确:
spring.datasource.url=jdbc:h2:file:~/dasboot
application.properties
logging.level.org.org.springframework=DEBUG
server.port=8080
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:file:~/dasboot
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.flyway.baseline-on-migrate=true
spring.jpa.hibernate.ddl-auto=none
POM.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.boot</groupId>
<artifactId>das-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<name>das-boot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>