Alter Tables

The following methods are available for table alterations:

Hanami::Model.migration do
  change do
    alter_table :users do
      # `users` table is implicit within this block, so it can be omitted.
      add_column :email, String,  null: false, unique: true
      set_column_default :visits_counts, default: 0
    end
  end
end

Some methods can be used without alter_table block. Those methods accept the name of the target table as first argument, then the other args.

add_index :users, :email
#instead of
alter_table(:users) do
  add_index :email
end

List of available methods which are shortcuts to the same methods in alter_table:

Rename Table

Tables can be renamed via #rename_table. It accepts the old name and the new name as arguments.

rename_table :users, :people

Drop Table

Tables can be dropped via #drop_table. It accepts the name as argument.

drop_table :users

Safe operation can be performed via #drop_table?. It drops the table only if it exists.


Looking for Lotus? We renamed the project and it's now called Hanami. Read the announcement.