module Clear::Migration
Overview
Clear's migration system
Migrations in Clear are very similar to active record's migrations. Migrations are two-way modification of the database.
It helps to keep a consistent database state during development lifecycle of your application.
To create a migration, two ways:
Clear command
TL;DR
You can create a new file which will be present in src/db/migrate
using:
clear-cli migration:g migration_name
Thus will create a migration in src/db/migration/[:uid]_migration_name.cr
(with uid number) and a class MigrationName
Advanced options
You can use clear-cli migration help
to get advanced options.
Manually
You can create a class following this naming convention:
Anything + Number.
The number is then used to order the migration between each others and must be unique.
Following the rule than inclusion is often better than inheritance, just
include the module Clear::Migration
to your class.
Methods of migration
Migration direction
Only one method must be overrided: change
. In comparison to ActiveRecord, there's no
up and down methods, instead you can put specific up/down code like this:
def change(dir)
dir.down { irreversible! }
end
def change(dir)
add_column :users, :full_name, :string
dir.up do
execute("UPDATE users SET full_name = (SELECT first_name || ' ' || last_name) from users")
end
end
Included Modules
Defined in:
clear/extensions/enum/migration.crclear/migration/direction.cr
clear/migration/migration.cr
clear/migration/operation/columns.cr
clear/migration/operation/execute.cr
clear/migration/operation/indexes.cr
clear/migration/operation/operation.cr
clear/migration/operation/table.cr
Constant Summary
-
Log =
::Log.for("clear.migration")
Instance Method Summary
Instance methods inherited from module Clear::Migration::Helper
add_column(table, column, datatype, nullable = false, constraint = nil, default = nil, with_values = false)
add_column,
add_operation(op : Operation)
add_operation,
apply(dir : Direction = Clear::Migration::Direction::Up)
apply,
change(dir)
change,
change_column_type(table, column, to, from = nil)
change_column_type,
change_type_column(table, from, to)
change_type_column,
create_enum(name : Clear::SQL::Symbolic, arr : Enumerable(T)) forall Tcreate_enum(name : Clear::SQL::Symbolic, e : ::Clear::Enum.class) create_enum, create_index(table, columns : Array(String), name = nil, using = nil, unique = false)
create_index(table, column, name = nil, using = nil, unique = false) create_index, create_table(name, id : Symbol | Bool = true, schema = "public", &) create_table, drop_column(table, column, type) drop_column, drop_enum(name : Clear::SQL::Symbolic, arr : Enumerable(T)? = nil) forall T drop_enum, execute(sql : String) execute, irreversible! irreversible!, rename_column(table, from, to) rename_column