Class: PgSqlTriggers::MigrationsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/pg_sql_triggers/migrations_controller.rb

Overview

Controller for managing trigger migrations via web UI Provides actions to run migrations up, down, and redo

Instance Method Summary collapse

Methods included from PermissionsHelper

#can?, #can_apply_triggers?, #can_drop_triggers?, #can_enable_disable_triggers?, #can_execute_sql_operations?, #can_generate_triggers?, #can_view_triggers?

Methods included from ErrorHandling

#format_error_for_flash, #handle_kill_switch_error, #handle_standard_error, #rescue_pg_sql_triggers_error

Methods included from PermissionChecking

#can_apply_triggers?, #can_drop_triggers?, #can_enable_disable_triggers?, #can_execute_sql_operations?, #can_generate_triggers?, #can_view_triggers?, #check_admin_permission, #check_viewer_permission, #current_actor, #current_user_id, #current_user_type

Methods included from KillSwitchProtection

#check_kill_switch, #current_environment, #expected_confirmation_text, #kill_switch_active?, #require_kill_switch_override

Instance Method Details

#downObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/pg_sql_triggers/migrations_controller.rb', line 39

def down
  # Check kill switch before rolling back migration
  check_kill_switch(operation: :ui_migration_down, confirmation: params[:confirmation_text])

  target_version = params[:version]&.to_i
  PgSqlTriggers::Migrator.ensure_migrations_table!

  current_version = PgSqlTriggers::Migrator.current_version
  if current_version.zero?
    flash[:warning] = "No migrations to rollback."
    redirect_to root_path
    return
  end

  if target_version
    PgSqlTriggers::Migrator.run_down(target_version)
    flash[:success] = "Migration version #{target_version} rolled back successfully."
  else
    # Rollback one migration by default
    PgSqlTriggers::Migrator.run_down
    flash[:success] = "Rolled back last migration successfully."
  end
  redirect_to root_path
rescue PgSqlTriggers::KillSwitchError => e
  flash[:error] = e.message
  redirect_to root_path
rescue StandardError => e
  Rails.logger.error("Migration down failed: #{e.message}\n#{e.backtrace.join("\n")}")
  flash[:error] = "Failed to rollback migration: #{e.message}"
  redirect_to root_path
end

#redoObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/pg_sql_triggers/migrations_controller.rb', line 71

def redo
  # Check kill switch before redoing migration
  check_kill_switch(operation: :ui_migration_redo, confirmation: params[:confirmation_text])

  target_version = params[:version]&.to_i
  PgSqlTriggers::Migrator.ensure_migrations_table!

  current_version = PgSqlTriggers::Migrator.current_version
  if current_version.zero?
    flash[:warning] = "No migrations to redo."
    redirect_to root_path
    return
  end

  flash[:success] = if target_version
                      redo_target_migration(target_version, current_version)
                    else
                      PgSqlTriggers::Migrator.run_down
                      PgSqlTriggers::Migrator.run_up
                      "Last migration redone successfully."
                    end
  redirect_to root_path
rescue PgSqlTriggers::KillSwitchError => e
  flash[:error] = e.message
  redirect_to root_path
rescue StandardError => e
  Rails.logger.error("Migration redo failed: #{e.message}\n#{e.backtrace.join("\n")}")
  flash[:error] = "Failed to redo migration: #{e.message}"
  redirect_to root_path
end

#upObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/pg_sql_triggers/migrations_controller.rb', line 9

def up
  # Check kill switch before running migration
  check_kill_switch(operation: :ui_migration_up, confirmation: params[:confirmation_text])

  target_version = params[:version]&.to_i
  PgSqlTriggers::Migrator.ensure_migrations_table!

  if target_version
    PgSqlTriggers::Migrator.run_up(target_version)
    flash[:success] = "Migration #{target_version} applied successfully."
  else
    pending = PgSqlTriggers::Migrator.pending_migrations
    if pending.any?
      PgSqlTriggers::Migrator.run_up
      count = pending.count
      flash[:success] = "Applied #{count} pending migration(s) successfully."
    else
      flash[:info] = "No pending migrations to apply."
    end
  end
  redirect_to root_path
rescue PgSqlTriggers::KillSwitchError => e
  flash[:error] = e.message
  redirect_to root_path
rescue StandardError => e
  Rails.logger.error("Migration up failed: #{e.message}\n#{e.backtrace.join("\n")}")
  flash[:error] = "Failed to apply migration: #{e.message}"
  redirect_to root_path
end