Class: Hanami::Model::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/model/migrator.rb,
lib/hanami/model/migrator/logger.rb,
lib/hanami/model/migrator/adapter.rb,
lib/hanami/model/migrator/connection.rb,
lib/hanami/model/migrator/mysql_adapter.rb,
lib/hanami/model/migrator/sqlite_adapter.rb,
lib/hanami/model/migrator/postgres_adapter.rb

Overview

Database schema migrator

Since:

  • 0.4.0

Defined Under Namespace

Classes: Adapter, Connection, Logger, MySQLAdapter, PostgresAdapter, SQLiteAdapter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration: self.class.configuration) ⇒ Hanami::Model::Migrator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instantiate a new migrator

Parameters:

Since:

  • 0.7.0


277
278
279
280
# File 'lib/hanami/model/migrator.rb', line 277

def initialize(configuration: self.class.configuration)
  @configuration = configuration
  @adapter       = Adapter.for(configuration)
end

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.

NOTE: Class level interface SHOULD be removed in Hanami 2.0

Examples:

Apply Migrations

require 'hanami/model'
require 'hanami/model/migrator'

Hanami::Model.configure do
  # ...
  adapter    :sql, '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"
Hanami::Model::Migrator.apply

Raises:

See Also:

Since:

  • 0.4.0


204
205
206
# File 'lib/hanami/model/migrator.rb', line 204

def self.apply
  new.apply
end

.configurationObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Hanami::Model configuration

Since:

  • 0.4.0


349
350
351
# File 'lib/hanami/model/migrator.rb', line 349

def self.configuration
  Model.configuration
end

.createObject

Create database defined by current configuration.

It's only implemented for the following databases:

* SQLite3
* PostgreSQL
* MySQL

NOTE: Class level interface SHOULD be removed in Hanami 2.0

Examples:

require 'hanami/model'
require 'hanami/model/migrator'

Hanami::Model.configure do
  # ...
  adapter :sql, 'postgres://localhost/foo'
end

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

Raises:

See Also:

  • Configuration#adapter

Since:

  • 0.4.0


45
46
47
# File 'lib/hanami/model/migrator.rb', line 45

def self.create
  new.create
end

.dropObject

Drop database defined by current configuration.

It's only implemented for the following databases:

* SQLite3
* PostgreSQL
* MySQL

NOTE: Class level interface SHOULD be removed in Hanami 2.0

Examples:

require 'hanami/model'
require 'hanami/model/migrator'

Hanami::Model.configure do
  # ...
  adapter :sql, 'postgres://localhost/foo'
end

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

Raises:

See Also:

  • Configuration#adapter

Since:

  • 0.4.0


75
76
77
# File 'lib/hanami/model/migrator.rb', line 75

def self.drop
  new.drop
end

.migrate(version: nil) ⇒ Object

Migrate database schema

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

NOTE: Class level interface SHOULD be removed in Hanami 2.0

Examples:

Migrate Up

require 'hanami/model'
require 'hanami/model/migrator'

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

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

Migrate Down

require 'hanami/model'
require 'hanami/model/migrator'

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

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

# Migrate to a specific version
Hanami::Model::Migrator.migrate(version: "20150610133853")

Parameters:

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

    target version

Raises:

See Also:

Since:

  • 0.4.0


124
125
126
# File 'lib/hanami/model/migrator.rb', line 124

def self.migrate(version: nil)
  new.migrate(version: version)
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.

NOTE: Class level interface SHOULD be removed in Hanami 2.0

Examples:

Prepare Database

require 'hanami/model'
require 'hanami/model/migrator'

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

Hanami::Model::Migrator.prepare # => creates `foo' and runs migrations

Prepare Database (with schema dump)

require 'hanami/model'
require 'hanami/model/migrator'

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

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

Raises:

See Also:

Since:

  • 0.4.0


246
247
248
# File 'lib/hanami/model/migrator.rb', line 246

def self.prepare
  new.prepare
end

.rollback(steps: 1) ⇒ Object

Rollback database schema

NOTE: Class level interface SHOULD be removed in Hanami 2.0

Examples:

Rollback

require 'hanami/model'
require 'hanami/model/migrator'

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

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

# By default only rollback one version
Hanami::Model::Migrator.rollback

# Use a hash passing a number of versions to rollback, it will rollbacks those versions
Hanami::Model::Migrator.rollback(versions: 2)

Parameters:

  • steps (Number, NilClass) (defaults to: 1)

    number of versions to rollback

Raises:

See Also:

Since:

  • 1.1.0


160
161
162
# File 'lib/hanami/model/migrator.rb', line 160

def self.rollback(steps: 1)
  new.rollback(steps: steps)
end

.versionString, NilClass

Return current database version timestamp

If no migrations were ran, it returns nil.

NOTE: Class level interface SHOULD be removed in Hanami 2.0

Examples:

# Given last migrations is:
#  20150610133853_create_books.rb

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

Returns:

  • (String, NilClass)

    current version, if previously migrated

Since:

  • 0.4.0


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

def self.version
  new.version
end

Instance Method Details

#applyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See Also:

Since:

  • 0.7.0


318
319
320
321
322
# File 'lib/hanami/model/migrator.rb', line 318

def apply
  migrate
  adapter.dump
  delete_migrations
end

#createObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See Also:

Since:

  • 0.7.0


286
287
288
# File 'lib/hanami/model/migrator.rb', line 286

def create
  adapter.create
end

#dropObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See Also:

Since:

  • 0.7.0


294
295
296
# File 'lib/hanami/model/migrator.rb', line 294

def drop
  adapter.drop
end

#migrate(version: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See Also:

Since:

  • 0.7.0


302
303
304
# File 'lib/hanami/model/migrator.rb', line 302

def migrate(version: nil)
  adapter.migrate(migrations, version) if migrations?
end

#prepareObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See Also:

Since:

  • 0.7.0


328
329
330
331
332
333
334
335
# File 'lib/hanami/model/migrator.rb', line 328

def prepare
  drop
rescue # rubocop:disable Lint/HandleExceptions
ensure
  create
  adapter.load
  migrate
end

#rollback(steps: 1) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See Also:

Since:

  • 1.1.0


310
311
312
# File 'lib/hanami/model/migrator.rb', line 310

def rollback(steps: 1)
  adapter.rollback(migrations, steps.abs) if migrations?
end

#versionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See Also:

Since:

  • 0.7.0


341
342
343
# File 'lib/hanami/model/migrator.rb', line 341

def version
  adapter.version
end