Class: Hanami::Model::Migrator::Adapter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/model/migrator/adapter.rb

Overview

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

Migrator base adapter

Since:

  • 0.4.0

Direct Known Subclasses

MySQLAdapter, PostgresAdapter, SQLiteAdapter

Constant Summary collapse

MIGRATIONS_TABLE =

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

Migrations table to store migrations metadata.

Since:

  • 0.4.0

:schema_migrations
MIGRATIONS_TABLE_VERSION_COLUMN =

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

Migrations table version column

Since:

  • 0.4.0

:filename

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Adapter

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.

Initialize an adapter

Since:

  • 0.4.0



53
54
55
# File 'lib/hanami/model/migrator/adapter.rb', line 53

def initialize(connection)
  @connection = connection
end

Class Method Details

.for(configuration) ⇒ 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.

Loads and returns a specific adapter for the given connection.

Since:

  • 0.4.0



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hanami/model/migrator/adapter.rb', line 31

def self.for(configuration)
  connection = Connection.new(configuration)

  case connection.database_type
  when :sqlite
    require "hanami/model/migrator/sqlite_adapter"
    SQLiteAdapter
  when :postgres
    require "hanami/model/migrator/postgres_adapter"
    PostgresAdapter
  when :mysql
    require "hanami/model/migrator/mysql_adapter"
    MySQLAdapter
  else
    self
  end.new(connection)
end

Instance Method Details

#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.

Create database. It must be implemented by subclasses.

Raises:

See Also:

Since:

  • 0.4.0



64
65
66
# File 'lib/hanami/model/migrator/adapter.rb', line 64

def create
  raise MigrationError.new("Current adapter (#{connection.database_type}) doesn't support 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.

Drop database. It must be implemented by subclasses.

Raises:

See Also:

Since:

  • 0.4.0



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

def drop
  raise MigrationError.new("Current adapter (#{connection.database_type}) doesn't support drop.")
end

#loadObject

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.

Load database schema. It must be implemented by subclasses.

Raises:

See Also:

Since:

  • 0.4.0



107
108
109
# File 'lib/hanami/model/migrator/adapter.rb', line 107

def load
  raise MigrationError.new("Current adapter (#{connection.database_type}) doesn't support load.")
end

#migrate(migrations, version) ⇒ 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.

Since:

  • 0.4.0



81
82
83
84
85
86
87
# File 'lib/hanami/model/migrator/adapter.rb', line 81

def migrate(migrations, version)
  version = Integer(version) unless version.nil?

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

#rollback(migrations, steps) ⇒ 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.

Since:

  • 1.1.0



91
92
93
94
95
96
97
98
# File 'lib/hanami/model/migrator/adapter.rb', line 91

def rollback(migrations, steps)
  table = migrations_table_dataset
  version = version_to_rollback(table, steps)

  Sequel::Migrator.run(connection.raw, migrations, target: version, allow_missing_migration_files: true)
rescue Sequel::Migrator::Error => exception
  raise MigrationError.new(exception.message)
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.

Database version.

Since:

  • 0.4.0



115
116
117
118
119
120
121
122
123
# File 'lib/hanami/model/migrator/adapter.rb', line 115

def version
  table = migrations_table_dataset
  return if table.nil?

  record = table.order(MIGRATIONS_TABLE_VERSION_COLUMN).last
  return if record.nil?

  record.fetch(MIGRATIONS_TABLE_VERSION_COLUMN).scan(MIGRATIONS_FILE_NAME_PATTERN).first.to_s
end