在 Spring Boot 2 中,如何自动生成数据库并将模式生成命令记录到文件中?

In Spring Boot 2, how can I auto-generate my database and also record the schema generation commands to a file?

我正在使用 Spring Boot 2.1 和 Java 11。我正在使用 Maven 构建我的工件。在本地 运行ning 时,我喜欢让我自​​动创建数据库的 Spring JPA 指令 ...

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true

我也喜欢让我自​​动创建文件的指令...

spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=update
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=update.sql

然而,当我在我的 src/main/resources/application.properties 中结合两者时...

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.jpa.properties.javax.persistence.validation.mode=none

spring.datasource.url=jdbc:postgresql://${PG_DB_HOST:localhost}:5432/${PG_DB_NAME}

spring.datasource.username=${PG_DB_USER}
spring.datasource.password=${PG_DB_PASS}

spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=update
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=update.sql

似乎“spring.jpa.properties.javax.persistence”优先,我的模式更改不是针对数据库的自动运行。有没有一种方法可以配置两者都发生的事情 - 更改被记录到一个文件并自动 运行 针对我的数据库?

添加 database.actionjavax.persistence 如下,这将根据模型更新数据库架构在 Database Schema Creation

中解释

application.properties

spring.jpa.properties.javax.persistence.schema-generation.database.action=update

还建议将 (已弃用) PostgreSQLDialect 方言更改为 PostgreSQL82Dialect 或根据您使用的版本。 Dailects

application.properties

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.jpa.properties.javax.persistence.validation.mode=none

spring.datasource.url=jdbc:postgresql://${PG_DB_HOST:localhost}:5432/${PG_DB_NAME}

spring.datasource.username=${PG_DB_USER}
spring.datasource.password=${PG_DB_PASS}

spring.jpa.properties.javax.persistence.schema-generation.database.action=update
spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=update
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=update.sql