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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:13:18  来源:igfitidea点击:

Spring-Boot execute data.sql in one profile only

javaspringhibernatejpaspring-boot

提问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-dropin the application.propertiesfile and execute a differently called "data.sql" script to initialize some tables.

我想创建另一个名为“devEmbeddedCreate”的配置文件,我需要在其中使用嵌入式数据库服务器 (h2),spring.jpa.hibernate.ddl-auto=create-dropapplication.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:

我尝试过的事情:

  1. The documentation mentions there can be a schema-${platform}.sqlfile and you can define the platform using spring.datasource.platformin the config. The problem it doesn't work with a data-${platform}.sqlfile. (here)

  2. 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 as spring.jpa.hibernate.ddl-auto=create-dropdoes. (hereand here)

  3. 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)

  1. 文档提到可以有一个schema-${platform}.sql文件,您可以spring.datasource.platform在配置中定义平台。它不适用于data-${platform}.sql文件的问题。(这里

  2. 创建了一个EmbeddedDatabaseBuilder. 问题是当我使用它时,它不会自动创建数据库而只应用指定的脚本。找不到自动创建数据库的方法spring.jpa.hibernate.ddl-auto=create-drop。(这里这里

  3. 寻找一种将 XML 配置转换为基于 Java 的配置的方法,找到了创建数据库和所有内容的方法。经过大量调整和更改以在内存中工作后,它看起来很有希望,但无法找出数据库在启动时关闭(并擦除其所有结构)的原因(此处

There must be a simpler way to just say "hey spring... run on strartup this data-devEmbeddedCreate.sqlscript 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.schemaproperty. 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.

如您所见,文档中指定的文件集仅在您未指定自己的列表时使用。