ActiveRecord.Migration class
Part of Active Record
A migration: one versioned step in the history of the database schema. A migration
file, named <version>_<name>.lua, creates a migration, describes its changes with the
schema statements in change (or in up and down) and returns it. Running the
migration up applies the changes; running it down reverts them, which happens
automatically for a change made of reversible statements.
The migrations of a schema live in its db/migrate/ folder, where
flux generate migration creates new ones; those of packages and plugins live in their
migrations folder and are copied into the schema's folder when the server starts. The
class also keeps track of the migration that is running and prints its progress to the
console.
Summary
Functions
Methods
Functions
ActiveRecord.Migration.current_migration()
shared packages/active_record/lib/migrator/migration.lua:50Returns the migration that is running at the moment, if any.
Returns
- ActiveRecord::Migration
ActiveRecord.Migration.current_recorder()
shared packages/active_record/lib/migrator/migration.lua:57Returns the command recorder that is collecting the statements of a #change that is being reverted, if any.
Returns
- ActiveRecord::CommandRecorder
ActiveRecord.Migration.next_migration_number(number)
shared packages/active_record/lib/migrator/migration.lua:276Generates the version for a new migration from the current time (YYYYMMDDHHMMSS in UTC), increased past the given version if that one is newer.
Parameters
- number Number/String optional, defaults to
nilversion that the new one has to be greater than
Returns
- String 14-digit version
Methods
ActiveRecord.Migration:announce(message)
shared packages/active_record/lib/migrator/migration.lua:216Prints a line that stands out, prefixed with the version and the name of the migration.
Parameters
- message String
Describes the schema changes of the migration. Meant to be overridden. The statements used in here are reverted automatically when the migration is rolled back, as long as they are reversible: create_table, add_column, rename_column, add_index, add_foreign_key, add_reference, add_timestamps and their counterparts are. For the rest, use #reversible or define #up and #down instead.
Returns
- ActiveRecord::Migration(self)
Returns the name used in the console output: the camel-cased name of the migration file, or the class name.
Returns
- String
Reverts the migration. Reverts #change by default.
Returns
- ActiveRecord::Migration(self)
ActiveRecord.Migration:exec_migration(direction)
shared internal packages/active_record/lib/migrator/migration.lua:128Internal
This function is internal. It is not part of the public API and may change without notice.
Runs the migration's #change, or #up / #down if #change is not overridden.
Parameters
- direction String 'up' or 'down'
ActiveRecord.Migration:execute_block(callback)
shared packages/active_record/lib/migrator/migration.lua:203Runs a function. When the migration is being reverted through its #change, the function is recorded like a statement and run in its turn.
Parameters
- callback Function
Checks whether the migration overrides #change.
Returns
- Boolean
Creates a migration. A migration file defines a migration, describes its changes in #change (or in #up and #down) and returns it. The version and the name of the migration come from its file name, '<version>_<name>.lua'.
-- db/migrate/20190309120000_add_role_to_users.lua
local AddRoleToUsers = ActiveRecord.Migration.new()
function AddRoleToUsers:change()
add_column('users', 'role', 'string', { default = '\'user\'' })
end
return AddRoleToUsers
Parameters
- version Number/String optional, defaults to
nilversion of the migration; overridden by the file name when the migration is loaded by the migrator
Returns
- ActiveRecord::Migration(self)
Checks whether the migration prints its progress.
Returns
- Boolean
ActiveRecord.Migration:migrate(direction)
shared packages/active_record/lib/migrator/migration.lua:101Runs the migration in the given direction, printing what happens.
Parameters
- direction String 'up' or 'down'
Returns
- ActiveRecord::Migration(self)
ActiveRecord.Migration:reversible(callback)
shared packages/active_record/lib/migrator/migration.lua:188Lets #change run code that differs between migrating and reverting. The helper that the callback receives runs its 'up' function only when migrating and its 'down' function only when reverting.
function Migration:change()
reversible(function(dir)
dir:up(function() execute("UPDATE users SET role = 'user'") end)
dir:down(function() execute("UPDATE users SET role = NULL") end)
end)
end
Parameters
- callback Function receives the direction helper
ActiveRecord.Migration:revert(callback)
shared packages/active_record/lib/migrator/migration.lua:151Runs the inverse of the statements made inside of the function. Normally used inside of #change to undo what an earlier migration did.
function Migration:change()
revert(function()
create_table('logs', function(t) t:text 'body' end)
end)
end
Parameters
- callback Function/ActiveRecord::Migration function that makes the statements, or a migration whose #change is reverted
Checks whether the migration is being reverted at the moment.
Returns
- Boolean
ActiveRecord.Migration:say(message, subitem)
shared packages/active_record/lib/migrator/migration.lua:227Prints a message.
Parameters
- message String
- subitem Boolean optional, defaults to
falseindent the message as a sub-item of the previous one
ActiveRecord.Migration:say_with_time(message, callback)
shared packages/active_record/lib/migrator/migration.lua:237Prints a message, runs a function and prints the time it took.
Parameters
- message String
- callback Function
Returns
- Any whatever the function returns
ActiveRecord.Migration:suppress_messages(callback)
shared packages/active_record/lib/migrator/migration.lua:251Runs a function without printing anything.
Parameters
- callback Function
Returns
- Any whatever the function returns
Applies the migration. Runs #change by default; override it together with #down for migrations that cannot be described by a reversible #change.
Returns
- ActiveRecord::Migration(self)