有没有可能在springboot应用程序中使用postgres的flyway脚本创建数据库?如果是,如何



这是我第一次使用flyway。我的应用程序是spring-boot,带有用于数据库版本控制的flyway和作为数据库的postgres。我已经成功地编写了一个脚本V1_1_0__create_schema1.sql,用于编写模式:

Create schema if not exists schema1;

以及创建表V1_1_1__create_table.sql的第二个脚本,用于写入表:

create table if not exists schema1.table1( id int NOT NULL, name varchar(255), CONSTRAINT table1_pkey PRIMARY KEY (id) );

现在我想用flyway约定编写用于数据库创建的sql脚本。我该怎么做?谢谢你的帮助!

是否可以在带有flyway的springboot应用程序中使用脚本创建数据库为postgres?

当然是:D!

如果是,如何?

首先,你需要在你的项目上添加飞行通道,作为一种依赖(如果你使用的是弹簧依赖管理或BOM,则没有版本(:

<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>

然后spring-Boot将自动检测并自动连接带有DataSource的Flyway,并在启动时调用它。

默认情况下,flyway在src/main/resources/db/migration文件夹中查找迁移脚本,这些脚本必须遵循V[version_number]__[Description_of_script].sql的命名约定,请注意您有2个下划线,因此例如您可以有V1__init_db.sql

然后,您可以在application.properties中配置数据源属性,以使用激活飞行通道

spring.flyway.enabled=true
spring.flyway.baseline-on-migrate=true
#if you have multiple schemas
spring.flyway.schemas=schema1,schema2

您可以查看这些资源以了解更多详细信息,其中有很多选项可供探索:

  • 使用flyway进行数据库迁移
  • 建立一个春天-开始-应用程序-与flyway和postgres
  • spring_boot_flyway_database

相关内容

最新更新