Package migratedb.v1.core.api.migration
Class ScriptMigration
java.lang.Object
migratedb.v1.core.api.migration.BaseJavaMigration
migratedb.v1.core.api.migration.ScriptMigration
- All Implemented Interfaces:
JavaMigration
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetChecksum(Configuration configuration) final voidExecutes this migration.protected abstract Objectscript()Provides the script to execute.Methods inherited from class migratedb.v1.core.api.migration.BaseJavaMigration
canExecuteInTransaction, getDescription, getVersion, isBaselineMigrationMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface migratedb.v1.core.api.migration.JavaMigration
getChecksum
-
Constructor Details
-
ScriptMigration
public ScriptMigration()
-
-
Method Details
-
script
Provides the script to execute. If this returns aReader, its lines are executed and the reader is properly closed. Everything else is converted to a String viaObject.toString(). -
getChecksum
- Returns:
- The checksum of this migration,
nullto disable checksum verification.
-
migrate
Description copied from interface:JavaMigrationExecutes this migration. The execution will automatically take place within a transaction, when the underlying database supports it and the canExecuteInTransaction returnstrue.- 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.
-