Alter Tables

The following methods are available for table alterations:

Those methods accept the name of the target table as first argument, then the other args. There is a convenient shortcut for this: #alter_table. It accepts the name of the table and a block that describes the alterations.

The following syntaxes are equivalent.

Hanami::Model.migration do
  change do
    add_column         :users, :email, String,  null: false, unique: true
    set_column_default :users, :visits_counts, default: 0
  end
end

The code above can be DRY'd with:

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

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.