Class: Hanami::Model::Migrator

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

Overview

Database schema migrator

Since:

  • 0.4.0

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.

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

def self.apply
  new.apply
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:

Since:

  • 0.4.0

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:

Since:

  • 0.4.0

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)

    target version

Raises:

See Also:

Since:

  • 0.4.0

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

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)

    number of versions to rollback

Raises:

See Also:

Since:

  • 1.1.0

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

def self.version
  new.version
end