Class: Hanami::Model::Migrator
- Inherits:
-
Object
- Object
- Hanami::Model::Migrator
- 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
Defined Under Namespace
Classes: Adapter, Connection, Logger, MySQLAdapter, PostgresAdapter, SQLiteAdapter
Class Method Summary collapse
-
.apply ⇒ Object
Migrate, dump schema, delete migrations.
-
.configuration ⇒ Object
private
Hanami::Model configuration.
-
.create ⇒ Object
Create database defined by current configuration.
-
.drop ⇒ Object
Drop database defined by current configuration.
-
.migrate(version: nil) ⇒ Object
Migrate database schema.
-
.prepare ⇒ Object
Prepare database: drop, create, load schema (if any), migrate.
-
.rollback(steps: 1) ⇒ Object
Rollback database schema.
-
.version ⇒ String, NilClass
Return current database version timestamp.
Instance Method Summary collapse
- #apply ⇒ Object private
- #create ⇒ Object private
- #drop ⇒ Object private
-
#initialize(configuration: self.class.configuration) ⇒ Hanami::Model::Migrator
constructor
private
Instantiate a new migrator.
- #migrate(version: nil) ⇒ Object private
- #prepare ⇒ Object private
- #rollback(steps: 1) ⇒ Object private
- #version ⇒ Object private
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
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
.apply ⇒ Object
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
204 205 206 |
# File 'lib/hanami/model/migrator.rb', line 204 def self.apply new.apply end |
.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.
Hanami::Model configuration
349 350 351 |
# File 'lib/hanami/model/migrator.rb', line 349 def self.configuration Model.configuration end |
.create ⇒ Object
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
45 46 47 |
# File 'lib/hanami/model/migrator.rb', line 45 def self.create new.create end |
.drop ⇒ Object
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
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
124 125 126 |
# File 'lib/hanami/model/migrator.rb', line 124 def self.migrate(version: nil) new.migrate(version: version) end |
.prepare ⇒ Object
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
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
160 161 162 |
# File 'lib/hanami/model/migrator.rb', line 160 def self.rollback(steps: 1) new.rollback(steps: steps) end |
.version ⇒ String, 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
265 266 267 |
# File 'lib/hanami/model/migrator.rb', line 265 def self.version new.version end |
Instance Method Details
#apply ⇒ 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.
318 319 320 321 322 |
# File 'lib/hanami/model/migrator.rb', line 318 def apply migrate adapter.dump delete_migrations end |
#create ⇒ 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.
286 287 288 |
# File 'lib/hanami/model/migrator.rb', line 286 def create adapter.create end |
#drop ⇒ 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.
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.
302 303 304 |
# File 'lib/hanami/model/migrator.rb', line 302 def migrate(version: nil) adapter.migrate(migrations, version) if migrations? end |
#prepare ⇒ 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.
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.
310 311 312 |
# File 'lib/hanami/model/migrator.rb', line 310 def rollback(steps: 1) adapter.rollback(migrations, steps.abs) if migrations? end |
#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.
341 342 343 |
# File 'lib/hanami/model/migrator.rb', line 341 def version adapter.version end |