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



279
280
281
282
# File 'lib/hanami/model/migrator.rb', line 279

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



206
207
208
# File 'lib/hanami/model/migrator.rb', line 206

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



351
352
353
# File 'lib/hanami/model/migrator.rb', line 351

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



47
48
49
# File 'lib/hanami/model/migrator.rb', line 47

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



77
78
79
# File 'lib/hanami/model/migrator.rb', line 77

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



126
127
128
# File 'lib/hanami/model/migrator.rb', line 126

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



248
249
250
# File 'lib/hanami/model/migrator.rb', line 248

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



162
163
164
# File 'lib/hanami/model/migrator.rb', line 162

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



267
268
269
# File 'lib/hanami/model/migrator.rb', line 267

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



320
321
322
323
324
# File 'lib/hanami/model/migrator.rb', line 320

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



288
289
290
# File 'lib/hanami/model/migrator.rb', line 288

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



296
297
298
# File 'lib/hanami/model/migrator.rb', line 296

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



304
305
306
# File 'lib/hanami/model/migrator.rb', line 304

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



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

def prepare
  drop
rescue # rubocop:disable Lint/SuppressedException
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



312
313
314
# File 'lib/hanami/model/migrator.rb', line 312

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



343
344
345
# File 'lib/hanami/model/migrator.rb', line 343

def version
  adapter.version
end