Class ScriptMigration

  • All Implemented Interfaces:
    JavaMigration

    public abstract class ScriptMigration
    extends BaseJavaMigration
    An easy way to define SQL migration scripts as code. This can eliminate the need for build-time class path scanning when used with FluentConfiguration.javaMigrations(JavaMigration...). Example:
        @Component
         
          public class V009__Add_Cool_Stuff extends ScriptMigration {
              protected Object script() {
                  return """
                     -- Add cool stuff
                     create table cool_stuff(id int primary key);
                     insert into cool_stuff(id) values (${cool_id});
                     drop table old_stuff;
                  """;
              }
          }
     }

    Computes a checksum from the script contents by default, just like MigrateDB does for migration scripts. You can turn this off by overriding getChecksum(Configuration).

    • Constructor Detail

      • ScriptMigration

        public ScriptMigration()
    • Method Detail

      • script

        protected abstract Object script()
        Provides the script to execute. If this returns a Reader, its lines are executed and the reader is properly closed. Everything else is converted to a String via Object.toString().
      • getChecksum

        public Checksum getChecksum​(Configuration configuration)
        Returns:
        The checksum of this migration, null to disable checksum verification.
      • migrate

        public final void migrate​(Context context)
                           throws Exception
        Description copied from interface: JavaMigration
        Executes this migration. The execution will automatically take place within a transaction, when the underlying database supports it and the canExecuteInTransaction returns true.
        Parameters:
        context - The context relevant for this migration, containing things like the JDBC connection to use and the current MigrateDB configuration.
        Throws:
        Exception - when the migration failed.