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.cr
clear/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, from, to) change_column_type, create_enum(name, arr : Enumerable(T)) forall T
create_enum(name, e)
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, arr : Enumerable(T) | Nil = nil) forall T drop_enum, execute(sql : String) execute, irreversible! irreversible!, rename_column(table, from, to) rename_column

Class methods inherited from module Clear::Migration::Helper

datatype(type : String) datatype

Instance methods inherited from module Clear::Migration::FullTextSearchableHelpers

add_full_text_searchable(table, on : Array(Tuple(String, Char)), column_name = "full_text_vector", catalog = "pg_catalog.english", trigger_name = nil, function_name = nil) add_full_text_searchable

Instance methods inherited from module Clear::ErrorMessages

build_error_message(message : String, ways_to_resolve : Tuple | Array = Tuple.new, manual_pages : Tuple | Array = Tuple.new) build_error_message, converter_error(from, to) converter_error, format_width(x, w = 80) format_width, illegal_setter_access_to_undefined_column(name) illegal_setter_access_to_undefined_column, lack_of_primary_key(model_name) lack_of_primary_key, migration_already_down(number) migration_already_down, migration_already_up(number) migration_already_up, migration_irreversible(name = nil, operation = nil) migration_irreversible, migration_not_found(number) migration_not_found, migration_not_unique(numbers) migration_not_unique, no_migration_yet(version) no_migration_yet, null_column_mapping_error(name, type) null_column_mapping_error, order_by_error_invalid_order(current_order) order_by_error_invalid_order, polymorphic_nil(through) polymorphic_nil, polymorphic_unknown_class(class_name) polymorphic_unknown_class, query_building_error(message) query_building_error, uid_not_found(class_name) uid_not_found, uninitialized_db_connection(connection) uninitialized_db_connection

Instance Method Detail

abstract def uid : Int64 #

[View source]