Java Spring-Boot 仅在一个配置文件中执行 data.sql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23790743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring-Boot execute data.sql in one profile only
提问by elysch
I have this application where using a profile "default" it connects to a PostgreSQL database and do migrations using Flyway.
我有这个应用程序,它使用配置文件“默认”连接到 PostgreSQL 数据库并使用 Flyway 进行迁移。
I want to create another profile called "devEmbeddedCreate" where I need to use an embedded database server (h2), create the database using spring.jpa.hibernate.ddl-auto=create-drop
in the application.properties
file and execute a differently called "data.sql" script to initialize some tables.
我想创建另一个名为“devEmbeddedCreate”的配置文件,我需要在其中使用嵌入式数据库服务器 (h2),spring.jpa.hibernate.ddl-auto=create-drop
在application.properties
文件中使用创建数据库并执行一个不同的名为“data.sql”的脚本来初始化一些表。
If I leave the script with "data.sql" file name, it gets executed every time the application starts. That's something I don't want to happen, I need it to be executed only in a certain profile.
如果我将脚本保留为“data.sql”文件名,则每次应用程序启动时都会执行它。这是我不想发生的事情,我只需要在某个配置文件中执行它。
Things I've tried:
我尝试过的事情:
The documentation mentions there can be a
schema-${platform}.sql
file and you can define the platform usingspring.datasource.platform
in the config. The problem it doesn't work with adata-${platform}.sql
file. (here)Created a
EmbeddedDatabaseBuilder
. The problem is when I use it, it doesn't automatically create the database and only apply the specified script. Couldn't find a way to create the database automatically asspring.jpa.hibernate.ddl-auto=create-drop
does. (hereand here)Looking for a way to transform an XML config to Java-based configuration found a way to create the database and all. After a lot of tweaking and changing to work in memory, it looked promising but haven't been able to find out why the database get closed (and erased all its structure) while starting up (here)
文档提到可以有一个
schema-${platform}.sql
文件,您可以spring.datasource.platform
在配置中定义平台。它不适用于data-${platform}.sql
文件的问题。(这里)创建了一个
EmbeddedDatabaseBuilder
. 问题是当我使用它时,它不会自动创建数据库而只应用指定的脚本。找不到自动创建数据库的方法spring.jpa.hibernate.ddl-auto=create-drop
。(这里和这里)寻找一种将 XML 配置转换为基于 Java 的配置的方法,找到了创建数据库和所有内容的方法。经过大量调整和更改以在内存中工作后,它看起来很有希望,但无法找出数据库在启动时关闭(并擦除其所有结构)的原因(此处)
There must be a simpler way to just say "hey spring... run on strartup this data-devEmbeddedCreate.sql
script when my profile is devEmbeddedCreate
, right?
必须有一种更简单的方法来说“嘿,春天……data-devEmbeddedCreate.sql
当我的个人资料是 时devEmbeddedCreate
,运行这个脚本,对吗?
采纳答案by kryger
You were on the right track with your approach 1), but you should set datasource platform via spring.datasource.platform
, not spring.jpa.database-platform
. The script execution functionality is not JPA-specific.
你在正确的轨道与你的方法上1) ,但你应该通过设置数据源的平台spring.datasource.platform
,而不是spring.jpa.database-platform
。脚本执行功能不是特定于 JPA 的。
You can also manually specify which SQL script files get executed by setting the spring.datasource.schema
property. This is an excerpt from org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration file in 1.0.2.RELEASE:
您还可以通过设置spring.datasource.schema
属性手动指定要执行的 SQL 脚本文件。这是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";
}
As you can see, the set of files specified in the documentation is only used if you don't specify your own list.
如您所见,文档中指定的文件集仅在您未指定自己的列表时使用。