Class: Reins::Migrator
- Inherits:
-
Object
- Object
- Reins::Migrator
- Defined in:
- lib/reins/migrator.rb
Constant Summary collapse
- DEFAULT_PATH =
"db/migrate".freeze
Instance Method Summary collapse
- #applied_versions ⇒ Object
-
#initialize(migrations_path: DEFAULT_PATH) ⇒ Migrator
constructor
A new instance of Migrator.
- #rollback(steps = 1) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(migrations_path: DEFAULT_PATH) ⇒ Migrator
Returns a new instance of Migrator.
7 8 9 |
# File 'lib/reins/migrator.rb', line 7 def initialize(migrations_path: DEFAULT_PATH) @migrations_path = migrations_path end |
Instance Method Details
#applied_versions ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/reins/migrator.rb', line 37 def applied_versions ensure_schema_migrations_table rows = Reins::Database.connection.execute( "SELECT version FROM schema_migrations ORDER BY version" ) rows.map { |row| row.is_a?(Hash) ? row["version"] : row[0] } end |
#rollback(steps = 1) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/reins/migrator.rb', line 23 def rollback(steps = 1) ensure_schema_migrations_table versions_to_undo = applied_versions.last(steps).reverse versions_to_undo.each do |version| file = find_file_for(version) _v, klass = load_migration(file) Reins::Database.connection.transaction do klass.new.run_down remove_version(version) end say "rolled back #{version}" end end |
#run ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/reins/migrator.rb', line 11 def run ensure_schema_migrations_table pending_files.each do |file| version, klass = load_migration(file) Reins::Database.connection.transaction do klass.new.run_up record_version(version) end say "migrated #{version}" end end |