module

Lustra::Migration

Inherits Lustra::Migration::Helper < Lustra::Migration::FullTextSearchableHelpers < Lustra::ErrorMessages

Lustra's migration system

Migrations in Lustra 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:

Lustra command

TL;DR

You can create a new file which will be present in src/db/migrate using:

lustra-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 lustra-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 Lustra::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

Constants

Log = ::Log.for("lustra.migration")

Instance methods

Nested types