Module: Lotus::Model::Migrator
- Defined in:
- lib/lotus/model/migrator.rb,
lib/lotus/model/migrator/adapter.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, 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.
202 203 204 205 206 |
# File 'lib/lotus/model/migrator.rb', line 202 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
82 83 84 |
# File 'lib/lotus/model/migrator.rb', line 82 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
110 111 112 |
# File 'lib/lotus/model/migrator.rb', line 110 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"
)
156 157 158 159 160 161 162 |
# File 'lib/lotus/model/migrator.rb', line 156 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
.
244 245 246 247 248 249 |
# File 'lib/lotus/model/migrator.rb', line 244 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
.
264 265 266 |
# File 'lib/lotus/model/migrator.rb', line 264 def self.version adapter(connection).version end |