如何让FlyWay运行我的迁移? “Schema是最新的。不需要迁移。”

时间:2018-05-09 23:12:22

标签: java spring-boot flyway

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

$ 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 schema galaxybadge without 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

2 个答案:

答案 0 :(得分:0)

好的,我发现了这个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/中的旧文件名,但是这样做了#39}。另一个故事。)

我相信可以设置

flyway.baselineVersion=0

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

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

答案 1 :(得分:0)

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

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

然后从1开始迁移脚本,例如:V1__script_description.sqlV2__script_description.sql,...