当前位置: 首页 > 知识库问答 >
问题:

如何在sprintboot stratup上运行reactive r2dbc驱动程序的flyway迁移

欧阳学真
2023-03-14
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>r2dbmigration</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>r2dbmigration</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot.experimental</groupId>
            <artifactId>spring-boot-starter-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>

        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot.experimental</groupId>
            <artifactId>spring-boot-test-autoconfigure-r2dbc</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot.experimental</groupId>
                <artifactId>spring-boot-bom-r2dbc</artifactId>
                <version>0.1.0.M3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>
DROP TABLE IF EXISTS file_import;
CREATE TABLE file_import
(
    id          BIGINT GENERATED ALWAYS AS IDENTITY,
    file_key    CHARACTER VARYING(255) NOT NULL,
    created_at  TIMESTAMP without time zone NOT NULL,
    created_by  BIGINT NOT NULL,
    PRIMARY KEY (id)
);
spring.r2dbc.url= r2dbc:postgresql://localhost:5432/import
spring.r2dbc.username=postgres
spring.r2dbc.password=password

Github URL

谢谢

共有1个答案

蒋岳
2023-03-14

下面的Java实现基于@sim的Kotlin示例。

示例pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RC1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>flyway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>flyway</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>dev.miku</groupId>
            <artifactId>r2dbc-mysql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>

</project>

示例application.yml

---
spring:
  r2dbc:
    url: r2dbc:pool:mysql://localhost:3306/defaultdb
    username: <user>
    password: <pass>
  flyway:
    url: jdbc:mysql://localhost:3306/defaultdb
    user: ${spring.r2dbc.username}
    password: ${spring.r2dbc.password}
    baseline-on-migrate: true
package com.example.flyway;

import org.flywaydb.core.Flyway;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

// https://stackoverflow.com/a/61412233
@Configuration
public class FlywayConfig {

    private final Environment env;

    public FlywayConfig(final Environment env) {
        this.env = env;
    }

    @Bean(initMethod = "migrate")
    public Flyway flyway() {
        return new Flyway(Flyway.configure()
                .baselineOnMigrate(true)
                .dataSource(
                        env.getRequiredProperty("spring.flyway.url"),
                        env.getRequiredProperty("spring.flyway.user"),
                        env.getRequiredProperty("spring.flyway.password"))
        );
    }
}
: Bootstrapping Spring Data R2DBC repositories in DEFAULT mode.
: Finished Spring Data repository scanning in 7ms. Found 0 R2DBC repository interfaces.
: Flyway Community Edition 6.4.1 by Redgate
: Database: jdbc:mysql://localhost:3306/defaultdb (MySQL 5.5)
: Successfully validated 1 migration (execution time 00:00.006s)
: Creating Schema History table `defaultdb`.`flyway_schema_history` ...
: DB: Name 'flyway_schema_history_pk' ignored for PRIMARY key. (SQL State: 42000 - Error Code: 1280)
: Current version of schema `defaultdb`: << Empty Schema >>
: Migrating schema `defaultdb` to version 1.0.001 - Initialise database tables
: Successfully applied 1 migration to schema `default` (execution time 00:00.036s)
: Netty started on port(s): 8080
:Started Application in 1.829 seconds (JVM running for 2.343)
: No active profile set, falling back to default profiles: default
: Bootstrapping Spring Data R2DBC repositories in DEFAULT mode.
: Finished Spring Data repository scanning in 11ms. Found 0 R2DBC repository interfaces.
: Flyway Community Edition 6.4.1 by Redgate
: Database: jdbc:mysql://localhost:3306/defaultdb (MySQL 5.5)
: Successfully validated 1 migration (execution time 00:00.009s)
: Current version of schema `defaultdb`: 1.0.001
: Schema `defaultdb` is up to date. No migration necessary.
: Netty started on port(s): 8080
: Started Application in 1.273 seconds (JVM running for 1.695)
 类似资料:
  • 我有一个带有几个实体类的Spring Boot应用程序,我正在尝试用Flyway实现数据库迁移。在启动时,似乎Spring Boot根本没有运行flyway。 这是我的应用程序.属性 这是我的建筑里的线.Gradle和flyway有关

  • 我们正在尝试将迁移作为.sql文件置于版本控制之下。开发人员将编写一个vn__*.sql文件,提交到版本控制,并且每5分钟运行一次的作业将自动迁移到开发和测试数据库。一旦更改被证明没有引起问题,其他人就会运行一个手动作业来在生产上运行迁移。 我的问题: 我有一个演示迁移,创建了几个表。我将v4__demotable.sql检查到PC上的版本控制中。 阅读文档时,开发人员似乎建议我应该创建一个新的v

  • 我刚刚在用户表中添加了一个新的时区列,现在我需要为所有现有记录填充它。 这些值应该根据同一表格中的另一列计算出来。 转换由名为OpenCage的地理服务完成。 所以我想创建一个新的Flyway迁移 读取表的每一行,获取其邮政编码值 调用地理服务的API并将邮政编码值传递给它 接收返回的时区, 并将其存储在时区列中 地理服务是我的应用程序中的SpringBoot,所以在迁移类中,我需要它。 而且我认

  • 我是Spark的新手,我了解到转换发生在工作者身上,动作发生在驱动者身上,但是中间动作也可以发生在工作者身上(如果操作是可交换的和关联的),这给出了实际的并行性。 我查看了相关和协方差代码:https://github.com/apache/spark/blob/master/mllib/src/main/scala/org/apache/spark/mllib/stat/correlation/

  • 正如标题所示,我正在寻找任何可以帮助我在Springs应用程序上下文(准确地说是持久性上下文)加载之前运行Flyway迁移的方法。原因是我在应用程序启动时运行的查询很少。这导致我的测试失败,因为正在对尚不存在的数据库表执行查询。我使用H2作为我的测试数据库。现在我只使用flyway核心依赖: 我有一个单一的Flyway配置类,如下所示: 并且属性在 我想实现的是:1.飞行路线做迁移2。Spring

  • 在我的spring-boot项目中,我使用了Flyway的开箱即用集成(),并且有一些迁移脚本,它们在启动时执行,并通过默认的-table进行管理。 该项目还使用了一个自带flyway迁移脚本的模块,这些脚本是以编程方式迁移的,并在其他表中跟踪迁移情况。 由于主项目的迁移需要对通过模块迁移创建的一些表进行操作,因此模块迁移需要在flyway-plugin迁移主项目脚本之前进行。 如何在主要应用的F