Summary
Functions
Functions
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
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
nilabstract column type, e.g. 'string', 'integer' or 'datetime' - options Map optional, defaults to
nilnull (Boolean) and default (inserted into the SQL as is)
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
nilcolumn (defaults to '<singular of to_table>_id'), primary_key ('id' by default), on_delete ('cascade', 'nullify' or 'restrict') and name
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
nilcolumn name or a list of column names - options Map optional, defaults to
nilname, unique, length, using, where and if_not_exists
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
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
nilthe options of t:references
Adds the 'created_at' and 'updated_at' columns to an existing table.
Parameters
- table_name String
- options Map optional, defaults to
nilcolumn 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
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.
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
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
nilcolumn, 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'
Adds a PRIMARY KEY constraint named '<table_name>_pkey' to a table.
Parameters
- table_name String
- key String column name
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)
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
nilforce (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
nilreceives the 'create' ActiveRecord::Query to define the columns on
Drops an index by name.
Parameters
- index_name String
- table_name String table the index belongs to
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
nilif_exists (ignore a missing table) - callback Function optional, defaults to
nilthe column definition, for reverting the statement
Runs a raw SQL statement. Not reversible.
Parameters
- sql String
Returns
- Any whatever the adapter returns
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
niltable the key refers to, or the options - options Map optional, defaults to
nilcolumn or name of the constraint
Returns
- Boolean
Prints a query to the console at the current indentation level. Does nothing in production, unless Settings.debug_output_in_production is set. The values of the bind parameters, if any, are printed after the query, in the order of their placeholders. Values longer than 255 characters are cut short.
print_query('Character Load (0s)', 'SELECT * FROM characters WHERE user_id = ?;', { 42 })
-- Character Load (0s) SELECT * FROM characters WHERE user_id = ?; [42]
Parameters
- prefix String label printed in front of the query, e.g. 'User Load (0.001s)'
- query String text of the query
- bindings List optional, defaults to
nilvalues of the bind parameters of the query
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
nilabstract column type - options Map optional, defaults to
nilcolumn options
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
nilcolumn or name of the constraint
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
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
nilcolumn (defaults to '<name>_id')
Removes the 'created_at' and 'updated_at' columns from a table.
Parameters
- table_name String
- options Map optional, defaults to
nilunused
Renames a column of an existing table.
Parameters
- table_name String table name
- name String current column name
- new_name String
Renames a database table.
Parameters
- name String current table name
- new_name String
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
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
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
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
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
Converts a unix timestamp to a 'YYYYMMDDHHMMSS' string in the server's local time.
Parameters
- unix_time Number
Returns
- String