DEV Community

Leandro Cesquini Pereira
Leandro Cesquini Pereira

Posted on • Originally published at leandrocp.com.br on

2 1

Lotus DB Migrations Tips

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

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Sentry image

Make it make sense

Only the context you need to fix your broken code with Sentry.

Start debugging →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay