如何使 FlyWay 运行 成为我的迁移? "Schema is up to date. No migration necessary."

How do I make FlyWay run my migrations? "Schema is up to date. No migration necessary."

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

$ 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 table。

> 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

好的,我找到了这个 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

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

(实际上 SQL 有很多调试,我不得不多次删除表并删除 schema_verion 中的行并从 [=19= 中删除旧文件名] 但那是另一回事了。)

我相信可以设置

flyway.baselineVersion=0

并根据此处的信息跳过 SQL 转储(初始化):https://flywaydb.org/documentation/configfiles。但是,为未来的开发人员提供架构似乎是正确的方法。

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

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

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

并从 1 开始迁移脚本,如下所示:V1__script_description.sqlV2__script_description.sql、...