Lotus DB Migrations Tips

August 24, 2015 · 2 mins read

Lotus, just like Rails, also supports database migration through Sequel Migrations. It´s pretty simple but some things are not so clear.

Create a migration

lotus g migration create_my_table

Enable UUID (PostgreSQL only)

Lotus::Model.migration do
  up do
    execute 'CREATE EXTENSION "uuid-ossp"'
  end

  down do
    execute 'DROP EXTENSION "uuid-ossp"'
  end
end

Use UUID as Primary Key

Lotus::Model.migration do
  up do
    create_table :my_table do
      column :id, :uuid, null: false, default: Sequel.function(:uuid_generate_v4), primary_key: true
    end
  end

  down do
    drop_table :my_table
  end
end

Use UUID as Foreign Key

Lotus::Model.migration do
  up do
    create_table :my_table do
      column :id, :uuid, null: false, default: Sequel.function(:uuid_generate_v4), primary_key: true
      foreign_key :author_id, :authors, type: 'uuid', null: false
    end
  end
end

Index a column

Lotus::Model.migration do
  up do
    create_table :my_table do
      column :code, :integer, null: false
      index :code
    end
  end
end

Table documentation

Lotus::Model.migration do
  up do
    create_table :my_table do
      column :code, :integer, null: false
    end

    execute %Q(COMMENT ON TABLE my_table IS 'You should do it')
    execute %Q(COMMENT ON COLUMN my_table.code IS 'Easy and useful')
  end
end

Migrate up

lotus db migrate

Migrate down

There´s no db migrate down command, although you can specify a version to migrate. Just specify 0 to migrate to initial version (before any migration).

lotus db migrate 0
← Previous Post Next Post