Module: Lotus::Model::Migrator

Defined in:
lib/lotus/model/migrator.rb,
lib/lotus/model/migrator/adapter.rb,
lib/lotus/model/migrator/connection.rb,
lib/lotus/model/migrator/mysql_adapter.rb,
lib/lotus/model/migrator/sqlite_adapter.rb,
lib/lotus/model/migrator/postgres_adapter.rb

Overview

Database schema migrator

Since:

  • 0.4.0

Defined Under Namespace

Classes: Adapter, Connection, MySQLAdapter, PostgresAdapter, SQLiteAdapter

Class Method Summary collapse

Class Method Details

.applyObject

Migrate, dump schema, delete migrations.

This is an experimental feature. It may change or be removed in the future.

Actively developed applications accumulate tons of migrations. In the long term they are hard to maintain and slow to execute.

“Apply” feature solves this problem.

It keeps an updated SQL file with the structure of the database. This file can be used to create fresh databases for developer machines or during testing. This is faster than to run dozen or hundred migrations.

When we use “apply”, it eliminates all the migrations that are no longer necessary.

Examples:

Apply Migrations

require 'lotus/model'
require 'lotus/model/migrator'

Lotus::Model.configure do
  # ...
  adapter    type: :sql, uri: 'postgres://localhost/foo'
  migrations 'db/migrations'
  schema     'db/schema.sql'
end

# Reads all files from "db/migrations" and apply and delete them.
# It generates an updated version of "db/schema.sql"
Lotus::Model::Migrator.apply

Raises:

See Also:

Since:

  • 0.4.0



203
204
205
206
207
# File 'lib/lotus/model/migrator.rb', line 203

def self.apply
  migrate
  adapter(connection).dump
  delete_migrations
end

.createObject

Create database defined by current configuration.

It’s only implemented for the following databases:

* SQLite3
* PostgreSQL
* MySQL

Examples:

require 'lotus/model'
require 'lotus/model/migrator'

Lotus::Model.configure do
  # ...
  adapter type: :sql, uri: 'postgres://localhost/foo'
end

Lotus::Model::Migrator.create # Creates `foo' database

Raises:

See Also:

Since:

  • 0.4.0



83
84
85
# File 'lib/lotus/model/migrator.rb', line 83

def self.create
  adapter(connection).create
end

.dropObject

Drop database defined by current configuration.

It’s only implemented for the following databases:

* SQLite3
* PostgreSQL
* MySQL

Examples:

require 'lotus/model'
require 'lotus/model/migrator'

Lotus::Model.configure do
  # ...
  adapter type: :sql, uri: 'postgres://localhost/foo'
end

Lotus::Model::Migrator.drop # Drops `foo' database

Raises:

See Also:

Since:

  • 0.4.0



111
112
113
# File 'lib/lotus/model/migrator.rb', line 111

def self.drop
  adapter(connection).drop
end

.migrate(version: nil) ⇒ Object

Migrate database schema

It’s possible to migrate “down” by specifying a version (eg. "20150610133853")

Examples:

Migrate Up

require 'lotus/model'
require 'lotus/model/migrator'

Lotus::Model.configure do
  # ...
  adapter    type: :sql, uri: 'postgres://localhost/foo'
  migrations 'db/migrations'
end

# Reads all files from "db/migrations" and apply them
Lotus::Model::Migrator.migrate

Migrate Down

require 'lotus/model'
require 'lotus/model/migrator'

Lotus::Model.configure do
  # ...
  adapter    type: :sql, uri: 'postgres://localhost/foo'
  migrations 'db/migrations'
end

# Reads all files from "db/migrations" and apply them
Lotus::Model::Migrator.migrate

# Migrate to a specifiy version
Lotus::Model::Migrator.migrate(version: "20150610133853")

Parameters:

  • version (String, NilClass) (defaults to: nil)

    target version

Raises:

See Also:

Since:

  • 0.4.0



157
158
159
160
161
162
163
# File 'lib/lotus/model/migrator.rb', line 157

def self.migrate(version: nil)
  version = Integer(version) unless version.nil?

  Sequel::Migrator.run(connection, migrations, target: version, allow_missing_migration_files: true) if migrations?
rescue Sequel::Migrator::Error => e
  raise MigrationError.new(e.message)
end

.prepareObject

Prepare database: drop, create, load schema (if any), migrate.

This is designed for development machines and testing mode. It works faster if used with apply.

Examples:

Prepare Database

require 'lotus/model'
require 'lotus/model/migrator'

Lotus::Model.configure do
  # ...
  adapter    type: :sql, uri: 'postgres://localhost/foo'
  migrations 'db/migrations'
end

Lotus::Model::Migrator.prepare # => creates `foo' and run migrations

Prepare Database (with schema dump)

require 'lotus/model'
require 'lotus/model/migrator'

Lotus::Model.configure do
  # ...
  adapter    type: :sql, uri: 'postgres://localhost/foo'
  migrations 'db/migrations'
  schema     'db/schema.sql'
end

Lotus::Model::Migrator.apply   # => updates schema dump
Lotus::Model::Migrator.prepare # => creates `foo', load schema and run pending migrations (if any)

Raises:

See Also:

Since:

  • 0.4.0



245
246
247
248
249
250
# File 'lib/lotus/model/migrator.rb', line 245

def self.prepare
  drop rescue nil
  create
  adapter(connection).load
  migrate
end

.versionString, NilClass

Return current database version timestamp

If no migrations were ran, it returns nil.

Examples:

# Given last migrations is:
#  20150610133853_create_books.rb

Lotus::Model::Migrator.version # => "20150610133853"

Returns:

  • (String, NilClass)

    current version, if previously migrated

Since:

  • 0.4.0



265
266
267
# File 'lib/lotus/model/migrator.rb', line 265

def self.version
  adapter(connection).version
end