Class: MonarchMigrate::Migration
- Inherits:
-
Object
- Object
- MonarchMigrate::Migration
- Defined in:
- lib/monarch_migrate/migration.rb
Defined Under Namespace
Modules: Lookup
Instance Method Summary collapse
- #after_commit(&block) ⇒ Object
- #filename ⇒ Object
-
#initialize(path) ⇒ Migration
constructor
A new instance of Migration.
- #name ⇒ Object
- #pending? ⇒ Boolean
- #run ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(path) ⇒ Migration
Returns a new instance of Migration.
15 16 17 18 |
# File 'lib/monarch_migrate/migration.rb', line 15 def initialize(path) @path = path.to_s @after_commit_callback = nil end |
Instance Method Details
#after_commit(&block) ⇒ Object
36 37 38 |
# File 'lib/monarch_migrate/migration.rb', line 36 def after_commit(&block) @after_commit_callback = block end |
#filename ⇒ Object
20 21 22 |
# File 'lib/monarch_migrate/migration.rb', line 20 def filename File.basename(path) end |
#name ⇒ Object
24 25 26 |
# File 'lib/monarch_migrate/migration.rb', line 24 def name File.basename(path, ".rb").delete_prefix("#{version}_").humanize end |
#pending? ⇒ Boolean
32 33 34 |
# File 'lib/monarch_migrate/migration.rb', line 32 def pending? !MigrationRecord.exists?(version: version) end |
#run ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/monarch_migrate/migration.rb', line 40 def run ActiveRecord::Base.connection.transaction do puts "Running data migration #{version}: #{name}" begin instance_eval File.read(path), path MigrationRecord.create!(version: version) puts "Migration complete" rescue => e puts "Migration failed due to #{e}" # Deliberately raising ActiveRecord::Rollback does not # pass on the exception and the callback will be triggered raise end puts end after_commit_callback&.call end |
#version ⇒ Object
28 29 30 |
# File 'lib/monarch_migrate/migration.rb', line 28 def version filename.match(/^([0-9]+)_/)[1] end |