module

Lustra::Migration::Helper

Inherits Lustra::Migration::FullTextSearchableHelpers

Constants

TYPE_MAPPING = {"string" => "text", "int32" => "int", "int64" => "bigint", "long" => "bigint", "bigdecimal" => "numeric", "datetime" => "timestamp without time zone"}

Class methods

datatype(type : String)

Replace some common type to their equivalent in postgresql if the type is not found in the correspondance table, then return itself

Source

Instance methods

add_column(table, column, datatype, nullable = false, constraint = nil, default = nil, with_values = false, comment : String | Nil = nil)

Add a column to a specific table

Source
add_containment_check(table : String, column : String, bounds, name : String | Nil = nil)

Add check constraint for containment operations Primarily used with geometric bounds but works with any type supporting <@ operator

Source
add_exclusion_constraint(table : String, column : String, operator : String = "&&", name : String | Nil = nil)

Add exclusion constraint using GIST index Commonly used for preventing overlaps in geometric data, time ranges, etc.

Source
add_index(table, columns, name = nil, unique = false, using = nil)
Source
add_operation(op : Operation)
Source
apply(dir : Direction = Lustra::Migration::Direction::Up)

This will apply the migration in a given direction (up or down)

Source
change(dir)
Source
change_column_comment(table, column, to : String | Nil)

Change a column's comment.

  • Pass a String to set comment, or nil to remove it (not auto-reversible).
  • Pass a NamedTuple {from: "old", to: "new"} to make the change reversible.
Source
change_column_comment(table, column, changes : NamedTuple(from: String | Nil, to: String | Nil))
Source
change_column_default(table, column, to : String | Nil)

Change a column's default value.

  • Pass a SQL literal as String to set the default (e.g. "'guest'", "1", "now()") or nil to drop it.
  • Pass a NamedTuple {from: <old>, to: <new>} to make the change reversible.
Source
change_column_default(table, column, changes : NamedTuple(from: String | Nil, to: String | Nil))
Source
change_column_null(table, column, null : Bool, default = nil)
Source
change_column_type(table, column, from, to)
Source
create_enum(name, arr : Enumerable(T)) forall T
Source
create_enum(name, e)
Source
create_index(table, columns : Array(String), name = nil, using = nil, unique = false)
Source
create_index(table, column, name = nil, using = nil, unique = false)

Add a column to a specific table

Source
create_table(name, id : Symbol | Bool = true, schema = "public", &)

Helper used in migration to create a new table.

Usage:

create_table(:users) do |t|
  t.column :first_name, :string
  t.column :last_name, :string
  t.column :email, :string, unique: true
  t.timestamps
end

By default, a column id of type integer will be created as primary key of the table. This can be prevented using primary: false

create_table(:users, id: false) do |t|
  t.column :user_id, :integer, primary: true # Use custom name for the primary key
  t.column :first_name, :string
  t.column :last_name, :string
  t.column :email, :string, unique: true
  t.timestamps
end
Source
drop_column(table, column, type)
Source
drop_constraint(table : String, constraint_name : String)

Drop any named constraint

Source
drop_enum(name, arr : Enumerable(T) | Nil = nil) forall T
Source
execute(sql : String)
Source
irreversible!
Source
rename_column(table, from, to)
Source