java - Flyway无法在java spring中设置模式?

标签 java spring flyway

我的问题是,即使检测到迁移文件,并且相同的迁移文件可以从 cmd 运行,我也无法从 Flyway java Spring 执行迁移。

我已经尝试设置我在互联网上找到的所有可能有用的参数来配置架构,但它仍然停留在“PUBLIC”

首先问题如下:(来自Java spring的日志)

"2019-07-01 15:06:04.296  INFO 296 --- [           main] o.f.core.internal.command.DbMigrate      : Current version of schema "PUBLIC": << Empty Schema >>
2019-07-01 15:06:04.297  INFO 296 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema "PUBLIC" to version 1 - Create person table
2019-07-01 15:06:04.324  INFO 296 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema "PUBLIC" to version 2 - Add people
2019-07-01 15:06:04.339  INFO 296 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema "PUBLIC" to version 3 - Add people2
2019-07-01 15:06:04.356  INFO 296 --- [           main] o.f.core.internal.command.DbMigrate      : Successfully applied 3 migrations to schema "PUBLIC" (execution time 00:00.094s)"

该表名为public,我也无法从mysql工作台看到它。

但是,如果我使用 Flyway migrate 从命令行执行此操作,它会更改名为 td 的架构,这就是我的意图:

"Migrating schema `td` to version 1 - Create person table
Migrating schema `td` to version 2 - Add people
Successfully applied 2 migrations to schema `td` (execution time 00:00.207s)"

Java 的 Flyway 配置:

public static void main(String[] args) {
    Flyway flyway = new Flyway();
    flyway.setBaselineOnMigrate(true);
    flyway.migrate();
    SpringApplication.run(TimeReportApplication.class, args);
}

应用程序属性:

flyway.user=root
flyway.password=root
flyway.url=jdbc:mysql://localhost:3306/td
flyway.schemas=TD

命令行的工作flyway配置:

flyway.url=jdbc:mysql://localhost:3306/td
flyway.user=root
flyway.password=root

您对可能出现的问题有什么建议吗?

最佳答案

经过一天的尝试,我找到了解决方案:

  1. 您必须将“数据源”添加到初始化文件中。这将由 Spring 从您的 application.properties 文件自动配置,您必须将其放置在 src/main/resources 中:

    公共(public)类 TimeReportApplication {

    @Autowired
    static DataSource dataSource;
    
    public static void main(String[] args) {
        PrintLog.print("Server started");
        System.out.println("Server started");
        Flyway flyway = new Flyway();
        flyway.clean();
        flyway.setDataSource(dataSource);
        flyway.setSqlMigrationPrefix("V");
        flyway.setBaselineOnMigrate(true);
        flyway.migrate();
        SpringApplication.run(TimeReportApplication.class, args);
    }
    

    }

  2. 在 application.properties 文件中的每个参数“spring”之前写入,例如: spring.flyway.user=root

关于java - Flyway无法在java spring中设置模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56836570/

相关文章:

java - 类似于 HTML5 的 canvas.putImageData(arrayOfPixels, 0,0) 的 Swing 方法

java - 如何更改 JFoenix 拾色器库的 CSS 样式?

java - 如果 bean 在定义上实例化,Spring 不会注入(inject) bean

mysql - Flyway:init 不创建模式数据库

flyway - flyway 中数据插入/更新脚本的最佳常见做法

Flyway:没有元数据表的非空模式

java - 错误: incompatible types: void cannot be converted to NumPal

Java 枚举和 Objective-C 枚举

java - byte[]转为图片下载

Spring Cloud DataFlow 文档缺少部分