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
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
.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
206 207 208 |
# File 'lib/hanami/model/migrator.rb', line 206 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
351 352 353 |
# File 'lib/hanami/model/migrator.rb', line 351 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
47 48 49 |
# File 'lib/hanami/model/migrator.rb', line 47 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
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
126 127 128 |
# File 'lib/hanami/model/migrator.rb', line 126 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
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
162 163 164 |
# File 'lib/hanami/model/migrator.rb', line 162 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
267 268 269 |
# File 'lib/hanami/model/migrator.rb', line 267 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.
320 321 322 323 324 |
# File 'lib/hanami/model/migrator.rb', line 320 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.
288 289 290 |
# File 'lib/hanami/model/migrator.rb', line 288 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.
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.
304 305 306 |
# File 'lib/hanami/model/migrator.rb', line 304 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.
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.
312 313 314 |
# File 'lib/hanami/model/migrator.rb', line 312 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.
343 344 345 |
# File 'lib/hanami/model/migrator.rb', line 343 def version adapter.version end |