Class: ActiveGraph::Migrations::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/active_graph/migrations/runner.rb

Constant Summary collapse

STATUS_TABLE_FORMAT =
'%-10s %-20s %s'
SEPARATOR =
'--------------------------------------------------'
FILE_MISSING =
'**** file missing ****'
STATUS_TABLE_HEADER =
['Status', 'Migration ID', 'Migration Name'].freeze
UP_MESSAGE =
'up'
DOWN_MESSAGE =
'down'
INCOMPLETE_MESSAGE =
'incomplete'
MIGRATION_RUNNING =
{up: 'running', down: 'reverting'}.freeze
MIGRATION_DONE =
{up: 'migrated', down: 'reverted'}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Runner

Returns a new instance of Runner.


18
19
20
21
22
23
24
# File 'lib/active_graph/migrations/runner.rb', line 18

def initialize(options = {})
  @silenced = options[:silenced] || !!ENV['MIGRATIONS_SILENCED']
  label = SchemaMigration.mapped_label
  label.create_constraint(:migration_id, type: :unique) unless label.constraint?(:migration_id)
  @schema_migrations = SchemaMigration.all.to_a
  @up_versions = SortedSet.new(@schema_migrations.map(&:migration_id))
end

Class Method Details

.filesObject


183
184
185
# File 'lib/active_graph/migrations/runner.rb', line 183

def files
  Dir[files_path].sort
end

.latest_migrationObject


179
180
181
# File 'lib/active_graph/migrations/runner.rb', line 179

def latest_migration
  migration_files.last
end

.migration_filesObject


175
176
177
# File 'lib/active_graph/migrations/runner.rb', line 175

def migration_files
  files.map! { |file_path| MigrationFile.new(file_path) }
end

.migration_files_versionsObject


171
172
173
# File 'lib/active_graph/migrations/runner.rb', line 171

def migration_files_versions
  migration_files.map!(&:version)
end

Instance Method Details

#allObject


26
27
28
29
30
31
32
# File 'lib/active_graph/migrations/runner.rb', line 26

def all
  handle_incomplete_states!
  migration_files.each do |migration_file|
    next if up?(migration_file.version)
    migrate(:up, migration_file)
  end
end

#complete_migration_versionsObject


59
60
61
# File 'lib/active_graph/migrations/runner.rb', line 59

def complete_migration_versions
  @schema_migrations.map(&:migration_id)
end

#down(version) ⇒ Object


41
42
43
44
45
46
# File 'lib/active_graph/migrations/runner.rb', line 41

def down(version)
  handle_incomplete_states!
  migration_file = find_by_version!(version)
  return unless up?(version)
  migrate(:down, migration_file)
end

#mark_versions_as_complete(versions) ⇒ Object


63
64
65
66
67
68
69
# File 'lib/active_graph/migrations/runner.rb', line 63

def mark_versions_as_complete(versions)
  ActiveGraph::Base.new_query
                   .with('$versions AS versions').params(versions: versions).break
                   .unwind(version: :versions).break
                   .merge('(:`ActiveGraph::Migrations::SchemaMigration` {migration_id: version})')
                   .exec
end

#pending_migrationsObject


55
56
57
# File 'lib/active_graph/migrations/runner.rb', line 55

def pending_migrations
  all_migrations.select { |migration| !up?(migration) }
end

#reset(version) ⇒ Object


87
88
89
90
# File 'lib/active_graph/migrations/runner.rb', line 87

def reset(version)
  SchemaMigration.find_by!(migration_id: version).destroy
  output "Migration #{version} reset."
end

#resolve(version) ⇒ Object


82
83
84
85
# File 'lib/active_graph/migrations/runner.rb', line 82

def resolve(version)
  SchemaMigration.find_by!(migration_id: version).update!(incomplete: false)
  output "Migration #{version} resolved."
end

#rollback(steps) ⇒ Object


48
49
50
51
52
53
# File 'lib/active_graph/migrations/runner.rb', line 48

def rollback(steps)
  handle_incomplete_states!
  @up_versions.to_a.reverse.first(steps).each do |version|
    down(version)
  end
end

#statusObject


71
72
73
74
75
76
77
78
79
80
# File 'lib/active_graph/migrations/runner.rb', line 71

def status
  output STATUS_TABLE_FORMAT, *STATUS_TABLE_HEADER
  output SEPARATOR
  all_migrations.each do |version|
    status = migration_status(version)
    migration_file = find_by_version(version)
    migration_name = migration_file ? migration_file.class_name : FILE_MISSING
    output STATUS_TABLE_FORMAT, status, version, migration_name
  end
end

#up(version) ⇒ Object


34
35
36
37
38
39
# File 'lib/active_graph/migrations/runner.rb', line 34

def up(version)
  handle_incomplete_states!
  migration_file = find_by_version!(version)
  return if up?(version)
  migrate(:up, migration_file)
end