Any way to "compress" Flyway migrations?

Isn't that what re-baselining would do?

I'm still new to flyway, but this is how I think it would work. Please test the following first before taking my word for it.

Delete the schema_version table. Delete your migration scripts.

Run flyway baseline (this recreates the schema_version table and adds a baseline record as version 1)

Now you're good to go. Bear in mind that you will not be able to 'migrate' to any prior version as you've removed all of your migration scripts, but this might not be a problem for you.

Step by step solution:

  1. drop table schema_version;
  2. Export database structure as a script via MySQL Workbench, for example. Name this script V1__Baseline.sql
  3. Delete all migration scripts and add V1__Baseline.sql to your scripts folder, so it is the only script available for Flyway
  4. Run Flyway's "baseline" command
  5. Done

We do this to allow us to compress scripts for building new DB in dev environments but also run against existing production DB without having to log on and delete the flyway_version_history table, and we can keep the scripts (mainly for reference):

Compress all the scripts to a new script e.g. V1 to V42 into a new scripts V43. Convert V1 to V42 to text files by putting .txt on the end.

Set the baseline to 43. Set flyway to ignore missing migrations.

In script V43 use an 'if' block to protect the create/insert statements so that they don't run for the existing production database. We use postgres so it is something like this:

DO $$
  DECLARE
    flywayVersion INTEGER;
  BEGIN

    SELECT coalesce(max(installed_rank), 0) INTO flywayVersion FROM flyway_schema_history;
    RAISE NOTICE 'flywayVersion = %', flywayVersion;
    IF flywayVersion = 0 THEN
      RAISE NOTICE 'Creating the DB from scratch';
      CREATE TABLE...
      .....
    END IF;
END$$;

The flyway command looks something like this:

Flyway.configure()
      .dataSource(...)
      .baselineVersion("43")
      .ignoreMissingMigrations(true)
      .load()
      .migrate()

Tags:

Flyway