Class: MigrationManager::HomeController

Inherits:
ApplicationController show all
Defined in:
app/controllers/migration_manager/home_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/controllers/migration_manager/home_controller.rb', line 3

def index
  # Get all migrations and their statuses
  @migrations = ActiveRecord::Base.connection.execute(
    "SELECT version FROM schema_migrations"
  ).map { |row| row["version"] }.sort.reverse # Sort DESC

  @all_migrations = ActiveRecord::Migrator.migrations_paths.flat_map do |path|
    ActiveRecord::MigrationContext.new(path).migrations
  end.sort_by(&:version).reverse

  @pending_migrations = @all_migrations.reject { |m| @migrations.include?(m.version.to_s) }

  @success_message = params[:success] if params[:success].present?
end

#run_migrationObject



18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/migration_manager/home_controller.rb', line 18

def run_migration
  version = params[:version]
  if version.present?
    migrations_paths = ActiveRecord::Migrator.migrations_paths
    context = ActiveRecord::MigrationContext.new(migrations_paths, ActiveRecord::SchemaMigration)
    context.up(version.to_i)
    redirect_to "#{request.base_url}/migration_manager/home", notice: "Migration #{version} successfully run.!"
  else
    redirect_to "#{request.base_url}/migration_manager/home", alert: "Invalid migration version."
  end
end