flyway / flyway-sbt

Flyway SBT plugin
Apache License 2.0
127 stars 36 forks source link

How to make migration task fail for missing migrations? #32

Open micheal-hill opened 6 years ago

micheal-hill commented 6 years ago

I'm trying to have flyway/sbt produce an error (rather than a warning) when migrations are missing, but I haven't been able to find any options that would present this behavior. Specifically, I would like sbt to exit with a non-zero exit status if a migration is missing - the same behavior as when a migration has been modified after apply.

Is there some setting/configuration that I can change to do this?

> sbt flyway/flywayMigrate
[info] Loading settings for project global-plugins from idea.sbt ...
[info] Loading global plugins from __user__/.sbt/1.0/plugins
[info] Loading project definition from __user__/flyway_spike/project/project
[info] Loading settings for project flyway_spike-build from plugins.sbt ...
[info] Loading project definition from __user__/flyway_spike/project
[info] Loading settings for project flyway from build.sbt ...
[info] Loading settings for project flyway_spike from build.sbt ...
[info] Set current project to flyway_spike (in build file:__user__/flyway_spike/)
[info] Flyway Community Edition 5.2.0 by Boxfuse
[info] Database: jdbc:postgresql://localhost:5432/postgres (PostgreSQL 10.5)
[info] Successfully validated 1 migration (execution time 00:00.014s)
[info] Current version of schema "public": 20181018
[warn] Schema "public" has version 20181018, but no migration could be resolved in the configured locations !
[info] Schema "public" is up to date. No migration necessary.
[success] Total time: 0 s, completed 19/10/2018 12:58:17 PM
davidmweber commented 6 years ago

Not that I know of. Do you have a test migration (preferably for H2) that I can use?

micheal-hill commented 6 years ago

Working with a single migration, with the option flywayIgnoreMissingMigrations explicitly set to false (which appears to be the default, anyway).

In file V20181018__create_foo.sql:

CREATE TABLE test_user (
  name VARCHAR(25) NOT NULL,
  PRIMARY KEY(name)
);

Running the migration works without issue. However, if I delete the migration file after it has been run and try to run the migrations again, I only receive a warning and the migration task passes without error.


Researching this issue; it appears that the flyway option ignoreFutureMigrationsproduces the behavior I'm looking for when I use the CLI tool. However, I couldn't get the sbt version of this option to work as I expected (it didn't seem to have any affect). For now, I will use the flyway CLI tool as it appears to do what I need.

davidmweber commented 6 years ago

Thanks! I'll make a test case and dig in to it.

davidmweber commented 6 years ago

I have refactored the logic for setting migration parameters. Seems that ignoreFutureMigrations setting got lost in some deprecation. Can you check if the fail-on-error branch fixes this? You can sbt publishLocal and use 5.2.1-SNAPSHOT for the plugin version. If this fails, I will set up a more complex test case against a persistent database.