The schema statements (create_table, add_column, add_index and so on) that the migrations DSL exposes as global functions. The functions in here run their statement right away and keep the in-memory schema and metadata up to date; the global functions of the same name (see helpers.lua) go through ActiveRecord.ddl, which can also record the statements, so that a migration's #change can be reverted.

Summary

Functions

Makes the primary key of an SQLite table AUTOINCREMENT, so that the id of a deleted row is never given to a new one; without it, SQLite gives the highest id of the table plus one, which is the id of the last row again once that row has been deleted.
Adds a column to an existing table.
Adds a foreign key constraint.
Creates an index, unless an index with the same name is already recorded in the metadata.
Creates the foreign keys that the metadata declares but the tables of an SQLite database do not have, such as those of a table that was created or copied without them.
Adds a reference column ('<name>_id'), its index and optionally a foreign key to an existing table.
Adds the 'created_at' and 'updated_at' columns to an existing table.
Creates the indexes and foreign keys that t:references queued on a query.
Alters an existing database table.
Applies the ON DELETE action of a foreign key to the rows that refer to rows that do not exist, the way the database applies it to the rows of a referenced row that is deleted: with 'cascade' the rows are deleted, with 'nullify' their column is set to NULL.
Splits the arguments of add_column / remove_column into their parts.
Adds a PRIMARY KEY constraint named '<table_name>_pkey' to a table, unless it is already recorded in the metadata.
Adds a foreign key constraint along with an index on the key column.
Creates a database table.
Creates the database table of a model: a table with the 'id' primary key, the given columns and the 'created_at' / 'updated_at' timestamps.
Drops a database table, along with what the metadata knows about its indexes and foreign keys.
Runs a raw SQL statement.
Runs a function.
Checks whether a foreign key is recorded in the metadata.
Returns the name a foreign key constraint gets when none is given.
Returns the name an index gets when none is given: '<table>_<columns>_index'.
Removes a column from an existing table.
Drops a foreign key constraint and removes it from the metadata.
Drops an index and removes it from the metadata.
Removes a reference column along with its index and foreign key.
Removes the 'created_at' and 'updated_at' columns from a table.
Renames a column of an existing table.
Renames a database table.
Checks whether a table is part of the schema that ActiveRecord keeps, which holds every table created through it.

Functions

Statements.add_autoincrement(table_name)

shared packages/active_record/lib/schema_statements.lua:1302

Makes the primary key of an SQLite table AUTOINCREMENT, so that the id of a deleted row is never given to a new one; without it, SQLite gives the highest id of the table plus one, which is the id of the last row again once that row has been deleted. The table is rebuilt for it (see rebuild_sqlite_table), which keeps its rows and their ids. Does nothing on other databases, whose primary keys never give an id out twice, and for a table that is AUTOINCREMENT already, that has no primary key of the INTEGER type or that does not exist. A table that cannot be rebuilt is left as it is, with a warning.

Parameters

  • table_name String

Returns

  • Boolean whether the table was rebuilt

Statements.add_column(table_name, name, type, options)

shared packages/active_record/lib/schema_statements.lua:231

Adds a column to an existing table.

add_column('users', 'role', 'string', { default = '\'user\'' })
add_column('users', { 'banned', type = 'boolean', default = false })

Parameters

  • table_name String table name
  • name String/Map column name, or a table holding the name at index 1, the abstract column type under 'type' and the options as keys
  • type String optional, defaults to nil abstract column type, e.g. 'string', 'integer' or 'datetime'
  • options Map optional, defaults to nil null (Boolean) and default (inserted into the SQL as is)

Statements.add_foreign_key(from_table, to_table, options)

shared packages/active_record/lib/schema_statements.lua:938

Adds a foreign key constraint. Does nothing if a constraint with the same name is already recorded in the metadata. On SQLite, which cannot add constraints to existing tables, the table is rebuilt with the foreign key (see rebuild_sqlite_table), unless it has one from the same column to the same table already.

add_foreign_key('characters', 'users')
add_foreign_key('characters', 'users', { column = 'owner_id', on_delete = 'cascade' })

Parameters

  • from_table String table that holds the key
  • to_table String table the key refers to
  • options Map optional, defaults to nil column (defaults to '<singular of to_table>_id'), primary_key ('id' by default), on_delete ('cascade', 'nullify' or 'restrict') and name

Statements.add_index(table_name, columns, options)

shared packages/active_record/lib/schema_statements.lua:801

Creates an index, unless an index with the same name is already recorded in the metadata.

add_index('users', 'steam_id')
add_index('characters', { 'user_id', 'name' }, { unique = true })
add_index { 'users', 'steam_id', unique = true }

Parameters

  • table_name String/Map table name, or a table holding the table name at index 1, the column(s) at index 2 and the options as keys
  • columns String/List<String> optional, defaults to nil column name or a list of column names
  • options Map optional, defaults to nil name, unique, length, using, where and if_not_exists

Statements.add_missing_foreign_keys()

shared packages/active_record/lib/schema_statements.lua:1150

Creates the foreign keys that the metadata declares but the tables of an SQLite database do not have, such as those of a table that was created or copied without them. Does nothing on other databases, which create a foreign key when it is declared, and on an SQLite database that has every declared foreign key.

Before the foreign keys are created, the rows that refer to rows that do not exist through any foreign key of the database, declared or created, are dealt with by the action of that foreign key (see clean_orphans), over and over until the deleted rows leave no more such rows behind. A foreign key whose rows still refer to rows that do not exist then, because its action is neither 'cascade' nor 'nullify', is not created: a warning names the table, the column and the number of those rows, which are left untouched. The tables are rebuilt to create the foreign keys (see rebuild_sqlite_table), each table once, and a table that cannot be rebuilt is left without them, with a warning. What has been cleaned and created is printed. Runs in a transaction of its own while foreign keys are not enforced, unless it is called inside of a migration, which runs in one already.

Returns

  • Map numbers of the foreign keys that were created (created) and of those that were left out (skipped), and of the rows that were deleted (deleted) and set to NULL (nullified)

Statements.add_reference(table_name, name, options)

shared packages/active_record/lib/schema_statements.lua:746

Adds a reference column ('<name>_id'), its index and optionally a foreign key to an existing table.

Parameters

  • table_name String
  • name String name of the referenced model, singular
  • options Map optional, defaults to nil the options of t:references

Statements.add_timestamps(table_name, options)

shared packages/active_record/lib/schema_statements.lua:727

Adds the 'created_at' and 'updated_at' columns to an existing table.

Parameters

  • table_name String
  • options Map optional, defaults to nil column options, e.g. { null = true }

Statements.apply_references(table_name, query)

shared packages/active_record/lib/schema_statements.lua:48

Creates the indexes and foreign keys that t:references queued on a query.

Parameters

  • table_name String
  • query ActiveRecord::Query

Statements.change_table(name, callback)

shared packages/active_record/lib/schema_statements.lua:211

Alters an existing database table.

change_table('users', function(t)
  t:rename('name', 'nickname')
  t:remove('banned')
  t:integer 'playtime'
  t:references 'faction'
end)

Parameters

  • name String table name
  • callback Function receives the 'change' ActiveRecord::Query to describe the changes on

Statements.clean_orphans(from_table, to_table, options)

shared packages/active_record/lib/schema_statements.lua:1092

Applies the ON DELETE action of a foreign key to the rows that refer to rows that do not exist, the way the database applies it to the rows of a referenced row that is deleted: with 'cascade' the rows are deleted, with 'nullify' their column is set to NULL. Rows of a foreign key with any other action, and rows whose column cannot be NULL, are left as they are and only counted. A foreign key can only be added to a table once none of its rows refers to a row that does not exist, so this is run before a foreign key is added to a table that already holds rows. The rows that are deleted are not followed further: the rows that refer to them are left for the foreign keys of their own table. Has to be called while the adapter is in sync mode.

clean_orphans('character_permissions', 'characters', { on_delete = 'cascade' })
add_foreign_key('character_permissions', 'characters', { on_delete = 'cascade' })

Parameters

  • from_table String table that holds the key
  • to_table String table the key refers to
  • options Map optional, defaults to nil column (defaults to '<singular of to_table>_id'), primary_key ('id' by default) and on_delete ('cascade', 'nullify', or anything else to leave the rows alone)

Returns

  • Number the rows that were deleted or set to NULL, or that are left as they are; String what was done with them: 'deleted', 'nullified' or 'kept'

Statements.column_definition(name, type, options)

shared packages/active_record/lib/schema_statements.lua:18

Splits the arguments of add_column / remove_column into their parts. Both the positional (name, type, options) form and the table form { name, type = ..., ... } are accepted.

Parameters

  • name String/Map
  • type String optional, defaults to nil
  • options Map optional, defaults to nil

Returns

  • String name
  • String type
  • Map options

Statements.create_primary_key(table_name, key)

shared packages/active_record/lib/schema_statements.lua:1353

Adds a PRIMARY KEY constraint named '<table_name>_pkey' to a table, unless it is already recorded in the metadata.

Parameters

  • table_name String
  • key String column name

Statements.create_reference(args)

shared packages/active_record/lib/schema_statements.lua:1339

Adds a foreign key constraint along with an index on the key column.

create_reference {
  table_name = 'characters', key = 'user_id',
  foreign_table = 'users', foreign_key = 'id',
  cascade = true
}

Parameters

  • args Map table_name, key, foreign_table and foreign_key, optionally cascade (Boolean, adds ON DELETE CASCADE) and name (name of the constraint)

Statements.create_table(name, options, callback)

shared packages/active_record/lib/schema_statements.lua:83

Creates a database table. The 'id' primary key is added automatically.

create_table('logs', function(t)
  t:text 'body'
  t:string { 'action', null = false }
  t:timestamps()
end)

create_table('users', { force = true }, function(t) ... end)

Parameters

  • name String table name
  • options Map optional, defaults to nil force (drop an existing table first), if_not_exists (leave an existing table alone), id (false to not add a primary key), primary_key (name of the primary key column, 'id' by default)
  • callback Function optional, defaults to nil receives the 'create' ActiveRecord::Query to define the columns on

Statements.define_model(name, callback)

shared packages/active_record/lib/schema_statements.lua:129

Creates the database table of a model: a table with the 'id' primary key, the given columns and the 'created_at' / 'updated_at' timestamps.

Parameters

  • name String table name, the lowercase plural of the model's class name
  • callback Function receives the table definition

See also

Statements.drop_table(name, options)

shared packages/active_record/lib/schema_statements.lua:140

Drops a database table, along with what the metadata knows about its indexes and foreign keys.

Parameters

  • name String table name
  • options Map optional, defaults to nil if_exists (ignore a missing table)

Runs a raw SQL statement.

Parameters

  • sql String

Returns

  • Any whatever the adapter returns

Statements.execute_block(callback)

shared packages/active_record/lib/schema_statements.lua:1373

Runs a function. Used by Migration#reversible to put a block of code into the list of recorded statements.

Parameters

  • callback Function

Statements.foreign_key_exists(from_table, to_table, options)

shared packages/active_record/lib/schema_statements.lua:1043

Checks whether a foreign key is recorded in the metadata.

foreign_key_exists('characters', 'users')
foreign_key_exists('items', { column = 'dropped_by_character_id' })

Parameters

  • from_table String table that holds the key
  • to_table String/Map optional, defaults to nil table the key refers to, or the options
  • options Map optional, defaults to nil column or name of the constraint

Returns

  • Boolean

Statements.foreign_key_name(from_table, column, to_table, primary_key)

shared packages/active_record/lib/schema_statements.lua:916

Returns the name a foreign key constraint gets when none is given.

Parameters

  • from_table String
  • column String
  • to_table String
  • primary_key String

Returns

  • String

Statements.index_name(table_name, columns)

shared packages/active_record/lib/schema_statements.lua:778

Returns the name an index gets when none is given: '<table>_<columns>_index'.

Parameters

  • table_name String
  • columns List<String>

Returns

  • String

Statements.remove_column(table_name, name, type, options)

shared packages/active_record/lib/schema_statements.lua:698

Removes a column from an existing table. On SQLite the table is rebuilt without the column, which only works for tables whose definition the rebuild can carry over; see rebuild_sqlite_table.

Parameters

  • table_name String table name
  • name String/Map column name, or the same table add_column takes
  • type String optional, defaults to nil abstract column type; only needed to make the removal reversible
  • options Map optional, defaults to nil column options; only needed to make the removal reversible

Statements.remove_foreign_key(from_table, to_table, options)

shared packages/active_record/lib/schema_statements.lua:992

Drops a foreign key constraint and removes it from the metadata. On SQLite, the table is rebuilt without the foreign key (see rebuild_sqlite_table) if it has it.

remove_foreign_key('characters', 'users')
remove_foreign_key('characters', { column = 'owner_id' })
remove_foreign_key('characters', { name = 'ar_123456' })

Parameters

  • from_table String table that holds the key
  • to_table String/Map table the key refers to, or the options
  • options Map optional, defaults to nil column or name of the constraint

Statements.remove_index(table_name, options)

shared packages/active_record/lib/schema_statements.lua:877

Drops an index and removes it from the metadata.

remove_index('users', 'steam_id')
remove_index('characters', { 'user_id', 'name' })
remove_index('users', { name = 'users_steam_id_index' })
remove_index('users', { column = 'steam_id', if_exists = true })

Parameters

  • table_name String
  • options String/List<String>/Map column name, list of column names, or a table with name or column(s) and optionally if_exists

Statements.remove_reference(table_name, name, options)

shared packages/active_record/lib/schema_statements.lua:756

Removes a reference column along with its index and foreign key.

Parameters

  • table_name String
  • name String name of the referenced model, singular
  • options Map optional, defaults to nil column (defaults to '<name>_id')

Statements.remove_timestamps(table_name, _options)

shared packages/active_record/lib/schema_statements.lua:736

Removes the 'created_at' and 'updated_at' columns from a table.

Parameters

  • table_name String
  • _options Map optional, defaults to nil unused

Statements.rename_column(table_name, name, new_name)

shared packages/active_record/lib/schema_statements.lua:716

Renames a column of an existing table.

Parameters

  • table_name String table name
  • name String current column name
  • new_name String

Statements.rename_table(name, new_name)

shared packages/active_record/lib/schema_statements.lua:177

Renames a database table.

Parameters

  • name String current table name
  • new_name String

Statements.table_exists(table_name)

shared packages/active_record/lib/schema_statements.lua:1068

Checks whether a table is part of the schema that ActiveRecord keeps, which holds every table created through it.

Parameters

  • table_name String

Returns

  • Boolean

Defined in