Class: MonarchMigrate::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/monarch_migrate/migrator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, version: nil) ⇒ Migrator

Returns a new instance of Migrator.



8
9
10
11
# File 'lib/monarch_migrate/migrator.rb', line 8

def initialize(path, version: nil)
  @path = path.to_s
  @version = version
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/monarch_migrate/migrator.rb', line 5

def path
  @path
end

#versionObject (readonly)

Returns the value of attribute version.



6
7
8
# File 'lib/monarch_migrate/migrator.rb', line 6

def version
  @version
end

Instance Method Details

#migrationsObject



13
14
15
16
17
# File 'lib/monarch_migrate/migrator.rb', line 13

def migrations
  migration_files.sort.map do |f|
    Migration.new(f)
  end
end

#migrations_statusObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/monarch_migrate/migrator.rb', line 33

def migrations_status
  db_list = MigrationRecord.normalized_versions

  file_list = migrations.filter_map do |migration|
    version = migration.version
    status = db_list.delete(version) ? "up" : "down"
    [status, version, migration.name]
  end

  db_list.map! do |version|
    ["up", version, "***** NO FILE *****"]
  end

  (db_list + file_list).sort_by { |_, version, _| version.to_i }
end

#pending_migrationsObject



19
20
21
# File 'lib/monarch_migrate/migrator.rb', line 19

def pending_migrations
  migrations.select(&:pending?)
end

#runObject



23
24
25
26
27
28
29
30
31
# File 'lib/monarch_migrate/migrator.rb', line 23

def run
  if pending_migrations.any?
    puts "Running #{pending_migrations.size} data migrations"
    pending_migrations.sort_by(&:version).each(&:run)
  else
    puts "No data migrations pending"
    []
  end
end