Skip to content

abstract class Athena::ORM::Migrations::AbstractMigration
inherits Reference #

The parent type of every migration.

A migration applies one change to the database's schema or data in #up, and reverts it in #down, by adding the SQL to execute via #add_sql:

class App::Migrations::Version20261014120000 < AORM::Migrations::AbstractMigration
  def description : String
    "Add the users' email"
  end

  def up : Nil
    self.add_sql "ALTER TABLE users ADD email VARCHAR(255) DEFAULT NULL"
  end

  def down : Nil
    self.add_sql "ALTER TABLE users DROP email"
  end
end

A new instance is created each time the migration is executed, via .new with just the connection. A migration therefore can't define an #initialize that takes other arguments. Its SQL is executed once #up or #down returns, so #connection can be used to read the data the migration needs, but changes made through it aren't part of the migration's SQL.

Transactions#

Each migration runs in a transaction, see #transactional?. Some databases, such as MySQL and MariaDB, implicitly commit the transaction when a statement changes the schema, such as CREATE TABLE. Statements executed before such a statement can't be rolled back if a later one fails, so it's best to return false from #transactional? in migrations that change the schema on those databases.

Constructors#

.new(connection : AORM::Connection)#

View source

Methods#

#abort_if(condition : Bool, message : String = "Unknown Reason") : Nil#

Fails the migration with message if condition is true.

Raises AORM::Migrations::Exception::AbortMigration.

View source

#description : String#

Returns a description of what the migration changes.

View source

#down : Nil#

Reverts the change #up applies, by adding the SQL to execute via #add_sql.

Fails the migration by default, see #abort_if.

View source

#post_down : Nil#

Runs after the SQL #down added has been executed.

View source

#post_up : Nil#

Runs after the SQL #up added has been executed.

View source

#pre_down : Nil#

Runs before #down.

View source

#pre_up : Nil#

Runs before #up.

View source

#skip_if(condition : Bool, message : String = "Unknown Reason") : Nil#

Stops the migration without failing it if condition is true, with message as the reason.

Raises AORM::Migrations::Exception::SkipMigration.

View source

#sql : Array(AORM::Migrations::Query)#

Returns the SQL added via #add_sql, in the order it was added.

View source

#transactional? : Bool#

Returns true if the migration is executed in a transaction.

View source

abstract #up : Nil#

Applies the migration's change, by adding the SQL to execute via #add_sql.

View source

#warn_if(condition : Bool, message : String = "Unknown Reason") : Nil#

Logs message as a warning if condition is true.

View source