我有这个应用程序使用配置文件“默认”它连接到PostgreSQL数据库并使用Flyway进行迁移。
我想创建另一个名为“devEmbeddedCreate”的配置文件,我需要使用嵌入式数据库服务器(h2),使用创建数据库 spring.jpa.hibernate.ddl-auto=create-drop
在里面 application.properties
文件并执行一个不同的“data.sql”脚本来初始化一些表。
如果我将脚本保留为“data.sql”文件名,则每次应用程序启动时都会执行该文件。这是我不想发生的事情,我需要它只在某个特定的配置文件中执行。
我尝试过的事情:
文档提到可以有一个 schema-${platform}.sql
文件,您可以使用定义平台 spring.datasource.platform
在配置中。这个问题不起作用 data-${platform}.sql
文件。 (这里)
创建了一个 EmbeddedDatabaseBuilder
。问题是当我使用它时,它不会自动创建数据库并仅应用指定的脚本。无法找到自动创建数据库的方法 spring.jpa.hibernate.ddl-auto=create-drop
确实。 (这里 和 这里)
寻找一种将XML配置转换为基于Java的配置的方法,找到了一种创建数据库的方法。经过大量的调整和更改在内存中工作后,它看起来很有前景,但无法找到数据库启动时关闭(并擦除其所有结构)的原因(这里)
必须有一个更简单的方法来说“嘿春天......继续这个 data-devEmbeddedCreate.sql
我的个人资料的脚本 devEmbeddedCreate
, 对?
用你的方法走在正确的轨道上 1),但你应该通过设置数据源平台 spring.datasource.platform
不是 spring.jpa.database-platform
。脚本执行功能不是JPA特定的。
您还可以通过设置来手动指定执行哪些SQL脚本文件 spring.datasource.schema
属性。这是摘录自 1.0.2.RELEASE中的org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration文件:
String schema = this.datasourceProperties.getProperty("schema");
if (schema == null) {
schema = "classpath*:schema-"
+ this.datasourceProperties.getProperty("platform", "all")
+ ".sql,classpath*:schema.sql,classpath*:data.sql";
}
如您所见,只有在未指定自己的列表时才会使用文档中指定的文件集。
用你的方法走在正确的轨道上 1),但你应该通过设置数据源平台 spring.datasource.platform
不是 spring.jpa.database-platform
。脚本执行功能不是JPA特定的。
您还可以通过设置来手动指定执行哪些SQL脚本文件 spring.datasource.schema
属性。这是摘录自 1.0.2.RELEASE中的org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration文件:
String schema = this.datasourceProperties.getProperty("schema");
if (schema == null) {
schema = "classpath*:schema-"
+ this.datasourceProperties.getProperty("platform", "all")
+ ".sql,classpath*:schema.sql,classpath*:data.sql";
}
如您所见,只有在未指定自己的列表时才会使用文档中指定的文件集。