【问题标题】:How do I make FlyWay run my migrations? "Schema is up to date. No migration necessary."如何让 FlyWay 运行我的迁移? “架构是最新的。无需迁移。”
【发布时间】:2018-10-20 03:47:43
【问题描述】:

我有一个现有的数据库。我创建了两个迁移

$ ls src/main/resources/db/migration/
V1__create_stats.sql  V2__create_sources.sql

我在application.properties中设置了以下内容

# Prevent complaints when starting migrations with existing tables.
flyway.baselineOnMigrate = true

否则会报错org.flywaydb.core.api.FlywayException: Found non-empty schemagalaxybadgewithout metadata table! Use baseline() or set baselineOnMigrate to true to initialize the metadata table.

当我尝试启动应用程序时,它会跳过迁移并且不执行它们!我在 MySQL 中使用 show tables;,发现它们不存在!

>mvn spring-boot:run
...
2018-05-09 18:43:03.671  INFO 24520 --- [  restartedMain] o.f.core.internal.util.VersionPrinter    : Flyway 3.2.1 by Boxfuse
2018-05-09 18:43:04.420  INFO 24520 --- [  restartedMain] o.f.c.i.dbsupport.DbSupportFactory       : Database: jdbc:mysql://localhost:3306/galaxybadge (MySQL 5.5)
2018-05-09 18:43:04.486  INFO 24520 --- [  restartedMain] o.f.core.internal.command.DbValidate     : Validated 0 migrations (execution time 00:00.030s)
2018-05-09 18:43:04.704  INFO 24520 --- [  restartedMain] o.f.c.i.metadatatable.MetaDataTableImpl  : Creating Metadata table: `galaxybadge`.`schema_version`
2018-05-09 18:43:05.116  INFO 24520 --- [  restartedMain] o.f.core.internal.command.DbBaseline     : Schema baselined with version: 1
2018-05-09 18:43:05.145  INFO 24520 --- [  restartedMain] o.f.core.internal.command.DbMigrate      : Current version of schema `galaxybadge`: 1
2018-05-09 18:43:05.146  INFO 24520 --- [  restartedMain] o.f.core.internal.command.DbMigrate      : Schema `galaxybadge` is up to date. No migration necessary.

我查看了this answer,但没有帮助,而且似乎给出了错误的属性名称。这是它创建的schema_version 表。

> select * from schema_version;
+--------------+----------------+---------+-----------------------+----------+-----------------------+----------+--------------+---------------------+----------------+---------+
| version_rank | installed_rank | version | description           | type     | script                | checksum | installed_by | installed_on        | execution_time | success |
+--------------+----------------+---------+-----------------------+----------+-----------------------+----------+--------------+---------------------+----------------+---------+
|            1 |              1 | 1       | << Flyway Baseline >> | BASELINE | << Flyway Baseline >> |     NULL | root         | 2018-05-09 18:43:05 |              0 |       1 |
+--------------+----------------+---------+-----------------------+----------+-----------------------+----------+--------------+---------------------+----------------+---------+

Spring Boot 1.5.6、FlyWay Core 3.2.1

Spring docs - FlyWay docs

【问题讨论】:

标签: java spring-boot flyway


【解决方案1】:

好的,我找到了这个https://flywaydb.org/documentation/existing

但没有遵循。相反,我将迁移从 V1__*V2__* 移动到 V2...V3...,并将生产模式下载到 V1__initialize.sql

mysqldump -h project.us-east-1.rds.amazonaws.com -u username -p --no-data --skip-add-drop-table --compact --skip-set-charset databasename > V1__initialize.sql

然后当我运行 Spring mvn spring-boot:run 它运行迁移。

(实际上对 SQL 进行了很多调试,我不得不多次删除表并从 schema_verion 中删除行并从 target/.../migration/ 中删除旧文件名,但这是另一回事了。)

我相信可以设置

flyway.baselineVersion=0

并根据此处的信息跳过 SQL 转储(初始化):https://flywaydb.org/documentation/configfiles。然而,为未来的开发者提供可用的模式似乎是正确的方法。

我仍然不明白为什么它没有从原始问题运行 V2__... 迁移。如果它从 1 开始,则迁移 2 仍然可以运行。如果它按预期工作,那么我可能会更快地理解这个问题。

【讨论】:

    【解决方案2】:

    对于已有数据库的 Spring Boot 应用程序,在您的 application.yml:

    flyway:
        baseline-on-migrate: true
        baseline-version: 0
    

    然后从 1 开始您的迁移脚本,如下所示:V1__script_description.sql, V2__script_description.sql, ...

    【讨论】:

      猜你喜欢
      • 2017-10-08
      • 2012-07-23
      • 2016-03-17
      • 2021-02-21
      • 2019-01-16
      • 2016-12-12
      • 2020-06-28
      • 2019-12-27
      • 2015-09-21
      相关资源
      最近更新 更多