Any way to “compress” Flyway migrations?

你离开我真会死。 提交于 2019-11-29 01:17:54
David Atkinson

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()

I haven't tried this, but what if you deleted all the migrations, create a new migration that creates the new starting point as version 1, set it as the baseline version -- and then modify your configuration to use a different table (e.g. flyway_schema_history_2)?

In existing databases, Flyway will see that you have a non-empty schema with no (recognized) flyway table and ignore the baseline migration. In new environments it will run the baseline migration too.

Am I missing anything?

(Of course a separate problem is how to generate the "compressed" migration. If you don't need any seed data you can just do a schema-only backup of your database and use that. If your migrations populate data too you will probably have to work that out manually.)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!