Class: CycloneLariat::Services::Rollback
- Inherits:
-
Object
- Object
- CycloneLariat::Services::Rollback
- Defined in:
- lib/cyclone_lariat/services/rollback.rb
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#repo ⇒ Object
readonly
Returns the value of attribute repo.
Instance Method Summary collapse
- #call(version = nil) ⇒ Object
- #existed_migrations ⇒ Object
-
#initialize(repo:, dir:) ⇒ Rollback
constructor
A new instance of Rollback.
- #paths_of_downgrades(version) ⇒ Object
Constructor Details
#initialize(repo:, dir:) ⇒ Rollback
Returns a new instance of Rollback.
8 9 10 11 |
# File 'lib/cyclone_lariat/services/rollback.rb', line 8 def initialize(repo:, dir:) @repo = repo @dir = dir end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
6 7 8 |
# File 'lib/cyclone_lariat/services/rollback.rb', line 6 def dir @dir end |
#repo ⇒ Object (readonly)
Returns the value of attribute repo.
6 7 8 |
# File 'lib/cyclone_lariat/services/rollback.rb', line 6 def repo @repo end |
Instance Method Details
#call(version = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/cyclone_lariat/services/rollback.rb', line 13 def call(version = nil) return 'No migration exists' if !Dir.exist?(dir) || Dir.empty?(dir) version ||= existed_migrations[-1] output = [] paths_of_downgrades(version).each do |path| filename = File.basename(path, '.rb') version, title = filename.split('_', 2) class_name = title.split('_').collect(&:capitalize).join output << "Down - #{version} #{class_name} #{path}" require_relative Pathname.new(Dir.pwd) + Pathname.new(path) Object.const_get(class_name).new.down repo.remove(version) end output end |
#existed_migrations ⇒ Object
32 33 34 |
# File 'lib/cyclone_lariat/services/rollback.rb', line 32 def existed_migrations @existed_migrations ||= repo.all.map { |row| row[:version] }.sort end |
#paths_of_downgrades(version) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/cyclone_lariat/services/rollback.rb', line 36 def paths_of_downgrades(version) migrations_to_downgrade = existed_migrations.select { |migration| migration >= version } paths = [] migrations_to_downgrade.each do |migration| path = Pathname.new(Dir.pwd) + Pathname.new(dir) founded = Dir.glob("#{path}/#{migration}_*.rb") raise "Could not found migration: `#{migration}` in #{path}" if founded.empty? raise "Found lot of migration: `#{migration}` in #{path}" if founded.size > 1 paths += founded end paths end |