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

Returns the migration that is running at the moment, if any.
Returns the command recorder that is collecting the statements of a #change that is being reverted, if any.
Generates the version for a new migration from the current time (YYYYMMDDHHMMSS in UTC), increased past the given version if that one is newer.

Methods

Prints a line that stands out, prefixed with the version and the name of the migration.
change()shared
Describes the schema changes of the migration.
Returns the name used in the console output: the camel-cased name of the migration file, or the class name.
down()shared
Reverts the migration.
Runs the migration's #change, or #up / #down if #change is not overridden.
Runs a function.
Checks whether the migration overrides #change.
Creates a migration.
Checks whether the migration prints its progress.
Runs the migration in the given direction, printing what happens.
Lets #change run code that differs between migrating and reverting.
Runs the inverse of the statements made inside of the function.
Checks whether the migration is being reverted at the moment.
Prints a message.
Prints a message, runs a function and prints the time it took.
Runs a function without printing anything.
up()shared
Applies the migration.

Functions

ActiveRecord.Migration.current_migration()

shared packages/active_record/lib/migrator/migration.lua:50

Returns 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:57

Returns 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:276

Generates 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 nil version 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:216

Prints a line that stands out, prefixed with the version and the name of the migration.

Parameters

  • message String

ActiveRecord.Migration:change()

shared packages/active_record/lib/migrator/migration.lua:67

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)

ActiveRecord.Migration:display_name()

shared packages/active_record/lib/migrator/migration.lua:210

Returns the name used in the console output: the camel-cased name of the migration file, or the class name.

Returns

  • String

ActiveRecord.Migration:down()

shared packages/active_record/lib/migrator/migration.lua:81

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:128

Internal

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:203

Runs 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

ActiveRecord.Migration:has_change()

shared packages/active_record/lib/migrator/migration.lua:88

Checks whether the migration overrides #change.

Returns

  • Boolean

ActiveRecord.Migration:init(version)

shared packages/active_record/lib/migrator/migration.lua:43

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 nil version of the migration; overridden by the file name when the migration is loaded by the migrator

Returns

  • ActiveRecord::Migration(self)

ActiveRecord.Migration:is_verbose()

shared packages/active_record/lib/migrator/migration.lua:268

Checks whether the migration prints its progress.

Returns

  • Boolean

ActiveRecord.Migration:migrate(direction)

shared packages/active_record/lib/migrator/migration.lua:101

Runs 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:188

Lets #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:151

Runs 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

ActiveRecord.Migration:reverting()

shared packages/active_record/lib/migrator/migration.lua:94

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:227

Prints a message.

Parameters

  • message String
  • subitem Boolean optional, defaults to false indent 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:237

Prints 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:251

Runs a function without printing anything.

Parameters

  • callback Function

Returns

  • Any whatever the function returns

ActiveRecord.Migration:up()

shared packages/active_record/lib/migrator/migration.lua:74

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)

Defined in