Class DbMigration
- Direct Known Subclasses:
ReversibleDbMigration
A migration declares an ordered series of steps in up() by
calling one of the add methods. The steps are query builders,
literal SQL statements, or DbMigrationAction instances for the
data transforms that need Java logic. A migration never executes
anything itself, since the steps are merely collected and are afterwards
executed by DbMigrations.
The protected factory methods create query builders that are bound to
the datasource that is being migrated. Creating such a builder doesn't
add a step by itself, since every step has to be added explicitly with
one of the add methods.
A migration that extends this class directly is irreversible, which
means that rolling it back raises
IrreversibleMigrationException.
When a migration can be reversed, it extends
ReversibleDbMigration instead and also declares the reverse
steps in its down method.
- Since:
- 1.10
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected DbMigrationAdds a literal SQL step to this migration.protected DbMigrationadd(DbMigrationAction action) Adds a Java logic step to this migration.protected DbMigrationAdds a query builder step to this migration.protected AlterTablealterTable(String table) Creates a query builder for altering a table.protected CreateIndexcreateIndex(String name) Creates a query builder for creating an index.protected CreateSequencecreateSequence(String name) Creates a query builder for creating a sequence.protected CreateTablecreateTable(String table) Creates a query builder for creating a table.protected CreateViewcreateView(String view) Creates a query builder for creating a view.protected DatasourceRetrieves the datasource that this migration is being collected for.protected DeleteCreates a query builder for deleting data.protected DropIndexCreates a query builder for dropping an index.protected DropSequencedropSequence(String name) Creates a query builder for dropping a sequence.protected DropTableCreates a query builder for dropping a table.protected DropViewCreates a query builder for dropping a view.protected InsertCreates a query builder for inserting data.protected TruncateCreates a query builder for truncating a table.abstract voidup()Declares the steps that perform this migration.protected UpdateCreates a query builder for updating data.
-
Constructor Details
-
DbMigration
public DbMigration()
-
-
Method Details
-
up
public abstract void up()Declares the steps that perform this migration.This method will be called while the steps of the migration are being collected; you declare each of them by calling one of the
addmethods, in the order in which they have to be executed.- Since:
- 1.10
- See Also:
-
add
Adds a query builder step to this migration.This makes it possible to declare the step with the object-oriented query builders so that the SQL that is eventually executed stays database-independent.
- Parameters:
query- the query to execute as this step- Returns:
- this migration
- Since:
- 1.10
- See Also:
-
add
Adds a literal SQL step to this migration.This makes it possible to execute statements that the query builders don't cover, at the cost of tying the migration to the SQL dialect that you're writing it in.
- Parameters:
sql- the SQL statement to execute as this step- Returns:
- this migration
- Since:
- 1.10
- See Also:
-
add
Adds a Java logic step to this migration.This makes it possible to perform data transforms that can't be expressed as a single query. The action receives the query manager of the datasource that is being migrated at the moment that the step is executed.
- Parameters:
action- the action to execute as this step- Returns:
- this migration
- Since:
- 1.10
- See Also:
-
datasource
Retrieves the datasource that this migration is being collected for.The datasource is only available while the steps are being declared, which is why you can only use it and the query builder factory methods that rely on it from inside
upordown.- Returns:
- the active datasource
- Since:
- 1.10
-
createTable
Creates a query builder for creating a table.The returned builder is bound to the datasource that is being migrated and still has to be added as a step with
add(Query).- Parameters:
table- the name of the table to create- Returns:
- the
CreateTablequery builder - Since:
- 1.10
- See Also:
-
alterTable
Creates a query builder for altering a table.The returned builder is bound to the datasource that is being migrated and still has to be added as a step with
add(Query). Since eachAlterTablequery performs exactly one alteration, you add one of them for every alteration that the migration needs.- Parameters:
table- the name of the table to alter- Returns:
- the
AlterTablequery builder - Since:
- 1.10
- See Also:
-
dropTable
Creates a query builder for dropping a table.The returned builder is bound to the datasource that is being migrated and still has to be added as a step with
add(Query).- Parameters:
table- the name of the table to drop- Returns:
- the
DropTablequery builder - Since:
- 1.10
- See Also:
-
createIndex
Creates a query builder for creating an index.The returned builder is bound to the datasource that is being migrated and still has to be added as a step with
add(Query).- Parameters:
name- the name of the index to create- Returns:
- the
CreateIndexquery builder - Since:
- 1.10
- See Also:
-
dropIndex
Creates a query builder for dropping an index.The returned builder is bound to the datasource that is being migrated and still has to be added as a step with
add(Query).- Parameters:
name- the name of the index to drop- Returns:
- the
DropIndexquery builder - Since:
- 1.10
- See Also:
-
createView
Creates a query builder for creating a view.The returned builder is bound to the datasource that is being migrated and still has to be added as a step with
add(Query).- Parameters:
view- the name of the view to create- Returns:
- the
CreateViewquery builder - Since:
- 1.10
- See Also:
-
dropView
Creates a query builder for dropping a view.The returned builder is bound to the datasource that is being migrated and still has to be added as a step with
add(Query).- Parameters:
view- the name of the view to drop- Returns:
- the
DropViewquery builder - Since:
- 1.10
- See Also:
-
createSequence
Creates a query builder for creating a sequence.The returned builder is bound to the datasource that is being migrated and still has to be added as a step with
add(Query).- Parameters:
name- the name of the sequence to create- Returns:
- the
CreateSequencequery builder - Since:
- 1.10
- See Also:
-
dropSequence
Creates a query builder for dropping a sequence.The returned builder is bound to the datasource that is being migrated and still has to be added as a step with
add(Query).- Parameters:
name- the name of the sequence to drop- Returns:
- the
DropSequencequery builder - Since:
- 1.10
- See Also:
-
truncate
Creates a query builder for truncating a table.The returned builder is bound to the datasource that is being migrated and still has to be added as a step with
add(Query).- Parameters:
table- the name of the table to truncate- Returns:
- the
Truncatequery builder - Since:
- 1.10
- See Also:
-
insert
Creates a query builder for inserting data.The returned builder is bound to the datasource that is being migrated and still has to be added as a step with
add(Query).- Parameters:
table- the name of the table to insert into- Returns:
- the
Insertquery builder - Since:
- 1.10
- See Also:
-
update
Creates a query builder for updating data.The returned builder is bound to the datasource that is being migrated and still has to be added as a step with
add(Query).- Parameters:
table- the name of the table to update- Returns:
- the
Updatequery builder - Since:
- 1.10
- See Also:
-
delete
Creates a query builder for deleting data.The returned builder is bound to the datasource that is being migrated and still has to be added as a step with
add(Query).- Parameters:
table- the name of the table to delete from- Returns:
- the
Deletequery builder - Since:
- 1.10
- See Also:
-