Spring-Boot 仅在一个配置文件中执行 data.sql [英] Spring-Boot execute data.sql in one profile only

查看:48
本文介绍了Spring-Boot 仅在一个配置文件中执行 data.sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个应用程序,其中使用配置文件默认"连接到 PostgreSQL 数据库并使用 Flyway 进行迁移.

I have this application where using a profile "default" it connects to a PostgreSQL database and do migrations using Flyway.

我想创建另一个名为devEmbeddedCreate"的配置文件,我需要在其中使用嵌入式数据库服务器 (h2),使用 spring.jpa.hibernate.ddl-auto=create-drop 创建数据库在 application.properties 文件中,并执行一个不同的名为data.sql"的脚本来初始化一些表.

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.

如果我将脚本保留为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.

我尝试过的事情:

  1. 文档提到可以有一个 schema-${platform}.sql 文件,您可以使用 spring.datasource.platform 在配置.它不适用于 data-${platform}.sql 文件的问题.(此处)

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

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

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-drop does. (here and here)

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

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)

必须有一种更简单的方法来说嘿,春天……当我的配置文件是 devEmbeddedCreate 时,在 strartup 这个 data-devEmbeddedCreate.sql 脚本上运行,对吧?

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?

推荐答案

您的方法是正确的1),但是您应该通过 spring.datasource 设置数据源平台.平台,而不是spring.jpa.database-platform.脚本执行功能不是特定于 JPA 的.

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.

您还可以通过设置 spring.datasource.schema 属性来手动指定要执行的 SQL 脚本文件.这是摘自 org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration 文件:

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:

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.

这篇关于Spring-Boot 仅在一个配置文件中执行 data.sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆