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#
Methods#
#abort_if(condition : Bool, message : String = "Unknown Reason") : Nil#
Fails the migration with message if condition is true.
#down : Nil#
Reverts the change #up applies, by adding the SQL to execute via #add_sql.
Fails the migration by default, see #abort_if.
#skip_if(condition : Bool, message : String = "Unknown Reason") : Nil#
Stops the migration without failing it if condition is true, with message as the reason.
#sql : Array(AORM::Migrations::Query)#
Returns the SQL added via #add_sql, in the order it was added.
abstract #up : Nil#
Applies the migration's change, by adding the SQL to execute via #add_sql.
#warn_if(condition : Bool, message : String = "Unknown Reason") : Nil#
Logs message as a warning if condition is true.