Summary

Functions

Makes the primary key of an SQLite table AUTOINCREMENT, so that ids are never given out twice.
Adds a column to an existing table.
Adds a foreign key constraint.
Creates the foreign keys that the metadata declares but the tables of an SQLite database do not have, after dealing with the rows that break 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.
Increases the indentation level used when printing queries by one.
Returns the indentation level used when printing queries to the console.
Sets the indentation level used when printing queries to the console.
Decreases the indentation level used when printing queries by one.
Alters an existing database table.
Deletes the rows that refer to rows that do not exist through a foreign key, or sets their column to NULL, as the ON DELETE action of the foreign key says.
Adds a PRIMARY KEY constraint named '<table_name>_pkey' to a table.
Adds a foreign key constraint along with an index on the key column.
Creates a database table.
Drops an index by name.
Drops a database table.
Runs a raw SQL statement.
Checks whether a foreign key is recorded in the metadata.
Prints a query to the console at the current indentation level.
Removes a column from an existing table.
Drops a foreign key constraint.
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.
Lets a migration's #change run code that differs between migrating and reverting.
Runs the inverse of the statements made inside of the function.
Escapes a string for use inside an SQL string literal, using the current adapter.
Turns a string into an escaped, quoted SQL string literal, using the current adapter.
Quotes an identifier such as a table or column name, using the current adapter.
Reverts the escaping of a string read from the database, using the current adapter.
Checks whether a table is part of the schema that ActiveRecord keeps.
Converts a date-time string to a unix timestamp.
Converts a unix timestamp to a date-time string in UTC, in the form that the current database adapter writes into date-time columns: ISO 8601 ('2019-03-09T12:00:00Z') unless the adapter needs another one, as MySQL does ('2019-03-09 12:00:00').
Converts a unix timestamp to a 'YYYYMMDDHHMMSS' string in the server's local time.

Functions

add_autoincrement(table_name)

shared packages/active_record/lib/helpers.lua:295

Makes the primary key of an SQLite table AUTOINCREMENT, so that ids are never given out twice. Does nothing on other databases. Not reversible.

Parameters

  • table_name String

Returns

  • Boolean whether the table was rebuilt

add_column(table_name, name, type, options)

shared packages/active_record/lib/helpers.lua:138

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)

add_foreign_key(from_table, to_table, options)

shared packages/active_record/lib/helpers.lua:235

Adds a foreign key constraint.

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

add_index(table_name, columns, options)

shared packages/active_record/lib/helpers.lua:203

Creates an index. The index is named '<table>_<columns>_index' if no name is given.

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

add_missing_foreign_keys()

shared packages/active_record/lib/helpers.lua:287

Creates the foreign keys that the metadata declares but the tables of an SQLite database do not have, after dealing with the rows that break them. Does nothing on other databases. Not reversible.

Returns

  • Map numbers of the foreign keys created and left out and of the rows cleaned

add_reference(table_name, name, options)

shared packages/active_record/lib/helpers.lua:181

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

add_reference('characters', 'user', { foreign_key = { on_delete = 'cascade' } })

Parameters

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

add_timestamps(table_name, options)

shared packages/active_record/lib/helpers.lua:162

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 }

Increases the indentation level used when printing queries by one.

Returns

  • Number new indentation level

Returns the indentation level used when printing queries to the console.

Returns

  • Number

ar_set_indent(lvl)

shared packages/active_record/lib/helpers.lua:406

Sets the indentation level used when printing queries to the console.

Parameters

  • lvl Number optional, defaults to 1

Returns

  • Number new indentation level

Decreases the indentation level used when printing queries by one.

Returns

  • Number new indentation level

No documentation available.

change_table(name, callback)

shared packages/active_record/lib/helpers.lua:124

Alters an existing database table.

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

Parameters

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

clean_orphans(from_table, to_table, options)

shared packages/active_record/lib/helpers.lua:279

Deletes the rows that refer to rows that do not exist through a foreign key, or sets their column to NULL, as the ON DELETE action of the foreign key says. Meant to be run right before the foreign key is added to a table that holds rows. Not reversible.

clean_orphans('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, primary_key and on_delete ('cascade' or 'nullify'; rows of any other action are left as they are)

Returns

  • Number the rows concerned
  • String 'deleted', 'nullified' or 'kept'

create_primary_key(table_name, key)

shared packages/active_record/lib/helpers.lua:309

Adds a PRIMARY KEY constraint named '<table_name>_pkey' to a table.

Parameters

  • table_name String
  • key String column name

create_reference(args)

shared packages/active_record/lib/helpers.lua:302

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

Parameters

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

create_table(name, options, callback)

shared packages/active_record/lib/helpers.lua:93

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

drop_index(index_name, table_name)

shared packages/active_record/lib/helpers.lua:222

Drops an index by name.

Parameters

  • index_name String
  • table_name String table the index belongs to

drop_table(name, options, callback)

shared packages/active_record/lib/helpers.lua:102

Drops a database table. In a migration's #change this is only reversible when the columns are given, as in drop_table('logs', function(t) t:text 'body' end).

Parameters

  • name String table name
  • options Map optional, defaults to nil if_exists (ignore a missing table)
  • callback Function optional, defaults to nil the column definition, for reverting the statement

Runs a raw SQL statement. Not reversible.

Parameters

  • sql String

Returns

  • Any whatever the adapter returns

foreign_key_exists(from_table, to_table, options)

shared packages/active_record/lib/helpers.lua:256

Checks whether a foreign key is recorded in the metadata. Runs no statement, so it can be used anywhere in a migration.

if foreign_key_exists('recognizes', { column = 'target_id' }) then ... end

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

remove_column(table_name, name, type, options)

shared packages/active_record/lib/helpers.lua:147

Removes a column from an existing table. Give the type to make it reversible.

Parameters

  • table_name String table name
  • name String column name
  • type String optional, defaults to nil abstract column type
  • options Map optional, defaults to nil column options

remove_foreign_key(from_table, to_table, options)

shared packages/active_record/lib/helpers.lua:243

Drops a foreign key constraint.

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

remove_index(table_name, options)

shared packages/active_record/lib/helpers.lua:215

Drops an index.

remove_index('users', 'steam_id')
remove_index('users', { name = 'users_steam_id_index' })

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

remove_reference(table_name, name, options)

shared packages/active_record/lib/helpers.lua:189

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')

remove_timestamps(table_name, options)

shared packages/active_record/lib/helpers.lua:169

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

Parameters

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

rename_column(table_name, name, new_name)

shared packages/active_record/lib/helpers.lua:155

Renames a column of an existing table.

Parameters

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

rename_table(name, new_name)

shared packages/active_record/lib/helpers.lua:109

Renames a database table.

Parameters

  • name String current table name
  • new_name String

reversible(callback)

shared packages/active_record/lib/helpers.lua:339

Lets a migration's #change run code that differs between migrating and reverting. Shorthand for ActiveRecord::Migration#reversible on the running migration.

reversible(function(dir)
  dir:up(function() execute("UPDATE users SET role = 'user'") end)
  dir:down(function() execute("UPDATE users SET role = NULL") end)
end)

Parameters

  • callback Function receives the direction helper

Runs the inverse of the statements made inside of the function. Shorthand for ActiveRecord::Migration#revert on the running migration.

Parameters

  • callback Function/ActiveRecord::Migration

Escapes a string for use inside an SQL string literal, using the current adapter.

Parameters

  • str String

Returns

  • String

Turns a string into an escaped, quoted SQL string literal, using the current adapter.

Parameters

  • str String

Returns

  • String

sql_quote_name(str)

shared packages/active_record/lib/helpers.lua:510

Quotes an identifier such as a table or column name, using the current adapter.

Parameters

  • str String

Returns

  • String

Reverts the escaping of a string read from the database, using the current adapter.

Parameters

  • str String

Returns

  • String

table_exists(table_name)

shared packages/active_record/lib/helpers.lua:264

Checks whether a table is part of the schema that ActiveRecord keeps. Runs no statement, so it can be used anywhere in a migration.

Parameters

  • table_name String

Returns

  • Boolean

time_from_timestamp(timestamp)

shared packages/active_record/lib/helpers.lua:466

Converts a date-time string to a unix timestamp. It understands both the ISO 8601 form ('YYYY-MM-DDTHH:MM:SSZ') and the 'YYYY-MM-DD HH:MM:SS' form, which to_datetime writes depending on the database and in which databases return date-time columns, and reads either as UTC. A number is taken for a unix timestamp and returned as is.

time_from_timestamp('2019-03-09T12:00:00Z') -- 1552132800
time_from_timestamp('2019-03-09 12:00:00')  -- 1552132800

Parameters

  • timestamp String/Number

Returns

  • Number unix timestamp, or nil if the value is not a date-time

to_datetime(unix_time)

shared packages/active_record/lib/helpers.lua:355

Converts a unix timestamp to a date-time string in UTC, in the form that the current database adapter writes into date-time columns: ISO 8601 ('2019-03-09T12:00:00Z') unless the adapter needs another one, as MySQL does ('2019-03-09 12:00:00').

Parameters

  • unix_time Number

Returns

  • String

to_timestamp(unix_time)

shared packages/active_record/lib/helpers.lua:368

Converts a unix timestamp to a 'YYYYMMDDHHMMSS' string in the server's local time.

Parameters

  • unix_time Number

Returns

  • String

Defined in