Class: CycloneLariat::Services::Migrate

Inherits:
Object
  • Object
show all
Defined in:
lib/cyclone_lariat/services/migrate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo:, dir:) ⇒ Migrate

Returns a new instance of Migrate.



8
9
10
11
# File 'lib/cyclone_lariat/services/migrate.rb', line 8

def initialize(repo:, dir:)
  @repo = repo
  @dir = dir
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



6
7
8
# File 'lib/cyclone_lariat/services/migrate.rb', line 6

def dir
  @dir
end

#repoObject (readonly)

Returns the value of attribute repo.



6
7
8
# File 'lib/cyclone_lariat/services/migrate.rb', line 6

def repo
  @repo
end

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cyclone_lariat/services/migrate.rb', line 13

def call
  return 'No migration exists' if !Dir.exist?(dir) || Dir.empty?(dir)

  output = []

  migration_paths.each do |path|
    filename = File.basename(path, '.rb')
    version, title = filename.split('_', 2)

    next if existed_migrations.include? version.to_i

    class_name = title.split('_').collect(&:capitalize).join
    output << "Up - #{version} #{class_name} #{path}"
    require_relative Pathname.new(Dir.pwd) + Pathname.new(path)
    Object.const_get(class_name).new.up
    repo.add(version)
  end

  output
end