Module: Lotus::Model::Migrator
- Defined in:
- lib/lotus/model/migrator.rb,
lib/lotus/model/migrator/adapter.rb,
lib/lotus/model/migrator/connection.rb,
lib/lotus/model/migrator/mysql_adapter.rb,
lib/lotus/model/migrator/sqlite_adapter.rb,
lib/lotus/model/migrator/postgres_adapter.rb
Overview
Database schema migrator
Defined Under Namespace
Classes: Adapter, Connection, MySQLAdapter, PostgresAdapter, SQLiteAdapter
Class Method Summary collapse
-
.apply ⇒ Object
Migrate, dump schema, delete migrations.
-
.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.
-
.version ⇒ String, NilClass
Return current database version timestamp.
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.
203 204 205 206 207 |
# File 'lib/lotus/model/migrator.rb', line 203 def self.apply migrate adapter(connection).dump delete_migrations end |
.create ⇒ Object
Create database defined by current configuration.
It’s only implemented for the following databases:
* SQLite3
* PostgreSQL
* MySQL
83 84 85 |
# File 'lib/lotus/model/migrator.rb', line 83 def self.create adapter(connection).create end |
.drop ⇒ Object
Drop database defined by current configuration.
It’s only implemented for the following databases:
* SQLite3
* PostgreSQL
* MySQL
111 112 113 |
# File 'lib/lotus/model/migrator.rb', line 111 def self.drop adapter(connection).drop end |
.migrate(version: nil) ⇒ Object
Migrate database schema
It’s possible to migrate “down” by specifying a version (eg. "20150610133853"
)
157 158 159 160 161 162 163 |
# File 'lib/lotus/model/migrator.rb', line 157 def self.migrate(version: nil) version = Integer(version) unless version.nil? Sequel::Migrator.run(connection, migrations, target: version, allow_missing_migration_files: true) if migrations? rescue Sequel::Migrator::Error => e raise MigrationError.new(e.) 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
.
245 246 247 248 249 250 |
# File 'lib/lotus/model/migrator.rb', line 245 def self.prepare drop rescue nil create adapter(connection).load migrate end |
.version ⇒ String, NilClass
Return current database version timestamp
If no migrations were ran, it returns nil
.
265 266 267 |
# File 'lib/lotus/model/migrator.rb', line 265 def self.version adapter(connection).version end |