52

Despite reading all available docs on Flyway website, I still don't understand what is baseline good for. Could somebody explain it in plain English and mention some use cases for this command?

Matt Warrick
  • 1,385
  • 1
  • 13
  • 22
  • 1
    Examples how to use baseline in real life: https://stackoverflow.com/questions/33509131/ and https://stackoverflow.com/questions/53594218/ – Alexey Vazhnov Apr 21 '20 at 12:22

1 Answers1

60

Baselining a database at version V1_0__baseline.sql, for example, instructs Flyway to only apply migration scripts that have been created after V1_0. It does this by inserting a migration entry in the SCHEMA_VERSION table used by Flyway. When you run a migration, available migration scripts will only be applied if their version is higher than the baseline version.

Example

You wish to use Flyway on a production and development database. You create a schema only dump of production. This will be the first migration script applied when you create a new empty database using Flyway.

However, because your existing production and development machines are already on this version, you don't want to apply this initial script. To prevent this, you create a SCHEMA_VERSION table and insert "1_0" into the table to instruct Flyway that the database is already at 1_0. Instead of manually creating this table and inserting a row via SQL, you can run the Flyway baseline command.

Then, a few weeks later, there is another database that you haven't brought onto Flyway, but have still been applying update scripts to (maybe you didn't have time). When you bring this database onto Flyway, you may need to baseline it at V3_0 or some other version.

Kevin
  • 4,070
  • 4
  • 45
  • 67
  • Provided you have `V1_0__baseline.sql` in your `flyway.locations`, what command do you run exactly from a terminal to baseline the database at that version? – Marco Lackovic Jul 16 '20 at 12:34
  • 1
    @Marco Lackovic: To baseline to a later version, I used the command line argument `-baselineVersion=1.11` to bring it to version 1.11 instead of 1 which is the default. It then marks the previous migrations as "Below Baseline". However, I was not able to do this after running migrate once, it gave me a "already baselined" error. So I deleted the `flyway_schema_history` table and ran the baseline command. Maybe someone can specify the right way to re-baseline without dropping the flyway_schema_history table. – Chintan Pathak Aug 06 '20 at 23:16