class ActiveGraph::Migrations::Runner
Constants
- DOWN_MESSAGE
- FILE_MISSING
- INCOMPLETE_MESSAGE
- MIGRATION_DONE
- MIGRATION_RUNNING
- SEPARATOR
- STATUS_TABLE_FORMAT
- STATUS_TABLE_HEADER
- UP_MESSAGE
Public Class Methods
new(options = {})
click to toggle source
# File lib/active_graph/migrations/runner.rb 18 def initialize(options = {}) 19 @silenced = options[:silenced] || !!ENV['MIGRATIONS_SILENCED'] 20 label = SchemaMigration.mapped_label 21 label.create_constraint(:migration_id, type: :unique) unless label.constraint?(:migration_id) 22 @schema_migrations = SchemaMigration.all.to_a 23 @up_versions = SortedSet.new(@schema_migrations.map(&:migration_id)) 24 end
Private Class Methods
app_root()
click to toggle source
# File lib/active_graph/migrations/runner.rb 193 def app_root 194 defined?(Rails) ? Rails.root : Pathname.new('.') 195 end
files()
click to toggle source
# File lib/active_graph/migrations/runner.rb 183 def files 184 Dir[files_path].sort 185 end
files_path()
click to toggle source
# File lib/active_graph/migrations/runner.rb 189 def files_path 190 app_root.join('db', 'neo4j', 'migrate', '*.rb') 191 end
latest_migration()
click to toggle source
# File lib/active_graph/migrations/runner.rb 179 def latest_migration 180 migration_files.last 181 end
migration_files()
click to toggle source
# File lib/active_graph/migrations/runner.rb 175 def migration_files 176 files.map! { |file_path| MigrationFile.new(file_path) } 177 end
migration_files_versions()
click to toggle source
# File lib/active_graph/migrations/runner.rb 171 def migration_files_versions 172 migration_files.map!(&:version) 173 end
Public Instance Methods
all()
click to toggle source
# File lib/active_graph/migrations/runner.rb 26 def all 27 handle_incomplete_states! 28 migration_files.each do |migration_file| 29 next if up?(migration_file.version) 30 migrate(:up, migration_file) 31 end 32 end
complete_migration_versions()
click to toggle source
# File lib/active_graph/migrations/runner.rb 59 def complete_migration_versions 60 @schema_migrations.map(&:migration_id) 61 end
down(version)
click to toggle source
# File lib/active_graph/migrations/runner.rb 41 def down(version) 42 handle_incomplete_states! 43 migration_file = find_by_version!(version) 44 return unless up?(version) 45 migrate(:down, migration_file) 46 end
mark_versions_as_complete(versions)
click to toggle source
# File lib/active_graph/migrations/runner.rb 63 def mark_versions_as_complete(versions) 64 ActiveGraph::Base.new_query 65 .with('$versions AS versions').params(versions: versions).break 66 .unwind(version: :versions).break 67 .merge('(:`ActiveGraph::Migrations::SchemaMigration` {migration_id: version})') 68 .exec 69 end
pending_migrations()
click to toggle source
# File lib/active_graph/migrations/runner.rb 55 def pending_migrations 56 all_migrations.select { |migration| !up?(migration) } 57 end
reset(version)
click to toggle source
# File lib/active_graph/migrations/runner.rb 87 def reset(version) 88 SchemaMigration.find_by!(migration_id: version).destroy 89 output "Migration #{version} reset." 90 end
resolve(version)
click to toggle source
# File lib/active_graph/migrations/runner.rb 82 def resolve(version) 83 SchemaMigration.find_by!(migration_id: version).update!(incomplete: false) 84 output "Migration #{version} resolved." 85 end
rollback(steps)
click to toggle source
# File lib/active_graph/migrations/runner.rb 48 def rollback(steps) 49 handle_incomplete_states! 50 @up_versions.to_a.reverse.first(steps).each do |version| 51 down(version) 52 end 53 end
status()
click to toggle source
# File lib/active_graph/migrations/runner.rb 71 def status 72 output STATUS_TABLE_FORMAT, *STATUS_TABLE_HEADER 73 output SEPARATOR 74 all_migrations.each do |version| 75 status = migration_status(version) 76 migration_file = find_by_version(version) 77 migration_name = migration_file ? migration_file.class_name : FILE_MISSING 78 output STATUS_TABLE_FORMAT, status, version, migration_name 79 end 80 end
up(version)
click to toggle source
# File lib/active_graph/migrations/runner.rb 34 def up(version) 35 handle_incomplete_states! 36 migration_file = find_by_version!(version) 37 return if up?(version) 38 migrate(:up, migration_file) 39 end
Private Instance Methods
all_migrations()
click to toggle source
# File lib/active_graph/migrations/runner.rb 160 def all_migrations 161 @up_versions + migration_files_versions 162 end
find_by_version(version)
click to toggle source
# File lib/active_graph/migrations/runner.rb 156 def find_by_version(version) 157 migration_files.find { |file| file.version == version } 158 end
find_by_version!(version)
click to toggle source
# File lib/active_graph/migrations/runner.rb 152 def find_by_version!(version) 153 find_by_version(version) || fail(UnknownMigrationVersionError, "No such migration #{version}") 154 end
handle_incomplete_states!()
click to toggle source
# File lib/active_graph/migrations/runner.rb 99 def handle_incomplete_states! 100 return unless incomplete_states.any? 101 incomplete_versions = incomplete_states.map(&:migration_id) 102 fail MigrationError, <<-MSG 103 There are migrations struck in an incomplete states, that could not be fixed automatically: 104 #{incomplete_versions.join('\n')} 105 This can happen when there's a critical error inside a migration. 106 107 If you think they were was completed correctly, run: 108 109 #{task_migration_messages('resolve', incomplete_versions)} 110 111 If you want to reset and run the migration again, run: 112 113 #{task_migration_messages('reset', incomplete_versions)} 114 115 MSG 116 end
incomplete_states()
click to toggle source
# File lib/active_graph/migrations/runner.rb 164 def incomplete_states 165 @incomplete_states ||= SortedSet.new(@schema_migrations.select(&:incomplete?)) 166 end
migrate(direction, migration_file)
click to toggle source
# File lib/active_graph/migrations/runner.rb 128 def migrate(direction, migration_file) 129 migration_message(direction, migration_file) do 130 migration = migration_file.create(silenced: @silenced) 131 migration.migrate(direction) 132 end 133 end
migration_message(direction, migration) { || ... }
click to toggle source
# File lib/active_graph/migrations/runner.rb 135 def migration_message(direction, migration) 136 output_migration_message "#{migration.version} #{migration.class_name}: #{MIGRATION_RUNNING[direction]}..." 137 time = format('%.4fs', yield) 138 output_migration_message "#{migration.version} #{migration.class_name}: #{MIGRATION_DONE[direction]} (#{time})" 139 output '' 140 end
migration_status(version)
click to toggle source
# File lib/active_graph/migrations/runner.rb 94 def migration_status(version) 95 return DOWN_MESSAGE unless up?(version) 96 incomplete_states.find { |v| v.migration_id == version } ? INCOMPLETE_MESSAGE : UP_MESSAGE 97 end
output(*string_format)
click to toggle source
# File lib/active_graph/migrations/runner.rb 142 def output(*string_format) 143 puts format(*string_format) unless @silenced 144 end
output_migration_message(message)
click to toggle source
# File lib/active_graph/migrations/runner.rb 146 def output_migration_message(message) 147 out = "== #{message} " 148 tail = '=' * [0, 80 - out.length].max 149 output "#{out}#{tail}" 150 end
task_migration_messages(type, versions)
click to toggle source
# File lib/active_graph/migrations/runner.rb 118 def task_migration_messages(type, versions) 119 versions.map do |version| 120 "rake neo4j:migrate:#{type} VERSION=#{version}" 121 end.join("\n") 122 end
up?(version)
click to toggle source
# File lib/active_graph/migrations/runner.rb 124 def up?(version) 125 @up_versions.include?(version) 126 end