Module: ActualDbSchema::Patches::MigrationContext

Defined in:
lib/actual_db_schema/patches/migration_context.rb

Overview

Add new command to roll back the phantom migrations

Instance Method Summary collapse

Instance Method Details

#phantom_migrationsObject



18
19
20
21
22
23
24
25
26
# File 'lib/actual_db_schema/patches/migration_context.rb', line 18

def phantom_migrations
  paths = Array(migrations_paths)
  current_branch_files = Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }]
  current_branch_file_names = current_branch_files.map { |f| ActualDbSchema.migration_filename(f) }

  migrations.reject do |migration|
    current_branch_file_names.include?(ActualDbSchema.migration_filename(migration.filename))
  end
end

#rollback_branches(manual_mode: false) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/actual_db_schema/patches/migration_context.rb', line 7

def rollback_branches(manual_mode: false)
  phantom_migrations.reverse_each do |migration|
    next unless status_up?(migration)

    show_info_for(migration) if manual_mode
    migrate(migration) if !manual_mode || user_wants_rollback?
  rescue StandardError => e
    ActualDbSchema.failed << FailedMigration.new(migration: migration, exception: e)
  end
end