Class DbMigration

java.lang.Object
rife.database.migrations.DbMigration
Direct Known Subclasses:
ReversibleDbMigration

public abstract class DbMigration extends Object
Provides a single declarative database migration.

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 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 add methods, in the order in which they have to be executed.

      Since:
      1.10
      See Also:
    • add

      protected DbMigration add(Query query)
      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

      protected DbMigration add(String sql)
      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

      protected DbMigration add(DbMigrationAction action)
      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

      protected Datasource 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 up or down.

      Returns:
      the active datasource
      Since:
      1.10
    • createTable

      protected CreateTable createTable(String table)
      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 CreateTable query builder
      Since:
      1.10
      See Also:
    • alterTable

      protected AlterTable alterTable(String table)
      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 each AlterTable query 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 AlterTable query builder
      Since:
      1.10
      See Also:
    • dropTable

      protected DropTable dropTable(String table)
      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 DropTable query builder
      Since:
      1.10
      See Also:
    • createIndex

      protected CreateIndex createIndex(String name)
      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 CreateIndex query builder
      Since:
      1.10
      See Also:
    • dropIndex

      protected DropIndex dropIndex(String name)
      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 DropIndex query builder
      Since:
      1.10
      See Also:
    • createView

      protected CreateView createView(String view)
      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 CreateView query builder
      Since:
      1.10
      See Also:
    • dropView

      protected DropView dropView(String view)
      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 DropView query builder
      Since:
      1.10
      See Also:
    • createSequence

      protected CreateSequence createSequence(String name)
      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 CreateSequence query builder
      Since:
      1.10
      See Also:
    • dropSequence

      protected DropSequence dropSequence(String name)
      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 DropSequence query builder
      Since:
      1.10
      See Also:
    • truncate

      protected Truncate truncate(String table)
      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 Truncate query builder
      Since:
      1.10
      See Also:
    • insert

      protected Insert insert(String table)
      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 Insert query builder
      Since:
      1.10
      See Also:
    • update

      protected Update update(String table)
      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 Update query builder
      Since:
      1.10
      See Also:
    • delete

      protected Delete delete(String table)
      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 Delete query builder
      Since:
      1.10
      See Also: