Class: Neo4j::Migrations::Runner

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

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Runner

Returns a new instance of Runner.


14
15
16
17
18
19
20
# File 'lib/neo4j/migrations/runner.rb', line 14

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


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

def files
  Dir[files_path].sort
end

.latest_migrationObject


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

def latest_migration
  migration_files.last
end

.migration_filesObject


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

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

.migration_files_versionsObject


167
168
169
# File 'lib/neo4j/migrations/runner.rb', line 167

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

Instance Method Details

#allObject


22
23
24
25
26
27
28
# File 'lib/neo4j/migrations/runner.rb', line 22

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


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

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

#down(version) ⇒ Object


37
38
39
40
41
42
# File 'lib/neo4j/migrations/runner.rb', line 37

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


59
60
61
62
63
64
65
# File 'lib/neo4j/migrations/runner.rb', line 59

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

#pending_migrationsObject


51
52
53
# File 'lib/neo4j/migrations/runner.rb', line 51

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

#reset(version) ⇒ Object


83
84
85
86
# File 'lib/neo4j/migrations/runner.rb', line 83

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

#resolve(version) ⇒ Object


78
79
80
81
# File 'lib/neo4j/migrations/runner.rb', line 78

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

#rollback(steps) ⇒ Object


44
45
46
47
48
49
# File 'lib/neo4j/migrations/runner.rb', line 44

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

#statusObject


67
68
69
70
71
72
73
74
75
76
# File 'lib/neo4j/migrations/runner.rb', line 67

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


30
31
32
33
34
35
# File 'lib/neo4j/migrations/runner.rb', line 30

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