Mixing creation and migration scripts with Flyway -
can flyway used mix creation , migration scripts that:
- new installations run schema creation script
- existing installations run migration scripts, , never see creation scripts of subsequent versions
?
e.g. given:
db/create/v1/v1__schema.sql db/create/v2/v2__schema.sql db/create/v3/v3__schema.sql db/migration/v1/v1.1__migratea.sql db/migration/v2/v2.1__migrateb.sql db/migration/v2/v2.2__migratec.sql
an existing v1 installation run following v3:
- db/migration/v1/v1.1__migratea.sql
- db/migration/v2/v2.1__migrateb.sql
- db/migration/v2/v2.2__migratec.sql
it never run following, these represent schema-only sql produced mysqldump:
- db/create/v2/v2__schema.sql
- db/create/v3/v3__schema.sql
a new v3 installation run:
- db/create/v3/v3__schema.sql
the above conflicts approach recommended upgrade scenario when using flyway required data populated independently of migration.
it looks should possible use flyway.locations support this, installations need include path creation script flyway can see it.
the alternative appears to run creation scripts outside of flyway , set baseline, nice if flyway manage everything.
in end, developed tool this. tool has latest schema in:
db/schema/schema.sql
and migration scripts in:
db/migration/<version>/<version>.<sequence>__<jira issue>.sql
e.g.:
db/migration/v1/v1.1__jira-15.sql db/migration/v2/v2.1__jira-12.sql db/migration/v2/v2.2__jira-22.sql db/migration/v3/v3.0__jira-34.sql
if database has no tables, schema.sql executed, , flyway baselined recent version, reported flyway's migrationinfoservice.pending() method.
i.e. last migrationinfo element returned pending() determines version pass flyway.setbaselineversion() before invoking flway.baseline() e.g:
dbsupport support = dbsupportfactory.createdbsupport(connection, true); schema schema = support.getoriginalschema(); if (schema.alltables().length == 0) { resource resource = new classpathresource("db/schema/schema.sql", getclass().getclassloader()); sqlscript script = new sqlscript(resource.loadasstring("utf-8"), support); script.execute(support.getjdbctemplate()); migrationinfo[] pending = flyway.info().pending(); migrationinfo version = pending.length > 0 ? pending[pending.length - 1] : null; if (version != null) { flyway.setbaselineversion(version.getversion()); flyway.setbaselinedescription(version.getdecription()); flyway.baseline(); } }
this ensures none of migration scripts invoked newly created databases, mean schema.sql must contain of changes.
if database has tables, no flyway information, baselined according detected schema version.
Comments
Post a Comment