Class: ActiveGraph::Migrations::Runner
- Inherits:
-
Object
- Object
- ActiveGraph::Migrations::Runner
- 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
- .files ⇒ Object
- .latest_migration ⇒ Object
- .migration_files ⇒ Object
- .migration_files_versions ⇒ Object
Instance Method Summary collapse
- #all ⇒ Object
- #complete_migration_versions ⇒ Object
- #down(version) ⇒ Object
-
#initialize(options = {}) ⇒ Runner
constructor
A new instance of Runner.
- #mark_versions_as_complete(versions) ⇒ Object
- #pending_migrations ⇒ Object
- #reset(version) ⇒ Object
- #resolve(version) ⇒ Object
- #rollback(steps) ⇒ Object
- #status ⇒ Object
- #up(version) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Runner
Returns a new instance of Runner.
14 15 16 17 18 19 20 |
# File 'lib/active_graph/migrations/runner.rb', line 14 def initialize( = {}) @silenced = [: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
.files ⇒ Object
179 180 181 |
# File 'lib/active_graph/migrations/runner.rb', line 179 def files Dir[files_path].sort end |
.latest_migration ⇒ Object
175 176 177 |
# File 'lib/active_graph/migrations/runner.rb', line 175 def latest_migration migration_files.last end |
.migration_files ⇒ Object
171 172 173 |
# File 'lib/active_graph/migrations/runner.rb', line 171 def migration_files files.map! { |file_path| MigrationFile.new(file_path) } end |
.migration_files_versions ⇒ Object
167 168 169 |
# File 'lib/active_graph/migrations/runner.rb', line 167 def migration_files_versions migration_files.map!(&:version) end |
Instance Method Details
#all ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/active_graph/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_versions ⇒ Object
55 56 57 |
# File 'lib/active_graph/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/active_graph/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/active_graph/migrations/runner.rb', line 59 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_migrations ⇒ Object
51 52 53 |
# File 'lib/active_graph/migrations/runner.rb', line 51 def pending_migrations all_migrations.select { |migration| !up?(migration) } end |
#reset(version) ⇒ Object
83 84 85 86 |
# File 'lib/active_graph/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/active_graph/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/active_graph/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 |
#status ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/active_graph/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/active_graph/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 |