Module: PtOnlineSchemaChange::ErrorHandler

Defined in:
app/my_lib/pt_online_schema_change/error_handler.rb

Defined Under Namespace

Classes: PtOscError

Class Method Summary collapse

Class Method Details

.cleanup_orphaned_triggers(table_name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/my_lib/pt_online_schema_change/error_handler.rb', line 44

def self.cleanup_orphaned_triggers(table_name)
  HH_LOGGER.info "Cleaning up orphaned triggers for table #{table_name}", { table_name: table_name }

  # Find pt-osc triggers
  triggers = ActiveRecord::Base.connection.exec_query(
    'SHOW TRIGGERS LIKE ?', 'show_triggers', [table_name]
  )

  pt_osc_triggers = triggers.select { |t| t['Trigger'].start_with?('pt_osc_') }

  pt_osc_triggers.each do |trigger|
    ActiveRecord::Base.connection.execute("DROP TRIGGER IF EXISTS `#{trigger['Trigger']}`")
    HH_LOGGER.info "Dropped trigger: #{trigger['Trigger']}",
                   { trigger_name: trigger['Trigger'], table_name: table_name }
  rescue StandardError => e
    HH_LOGGER.error "Failed to drop trigger #{trigger['Trigger']}: #{e.message}", {
      trigger_name: trigger['Trigger'],
      table_name: table_name,
      error: e.message,
    }
    APMErrorHandler.report(e)
  end
end

.handle_failure(table_name, error_output, exit_code) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/my_lib/pt_online_schema_change/error_handler.rb', line 14

def self.handle_failure(table_name, error_output, exit_code)
  HH_LOGGER.error "PT-OSC failed for table #{table_name}:", {
    table_name: table_name,
    exit_code: exit_code,
    error_output: error_output,
  }

  # Check for common errors and provide recovery suggestions
  case error_output
  when /has triggers/
    cleanup_orphaned_triggers(table_name)
    raise PtOscError.new(
      "Table #{table_name} has existing triggers. Cleaned up orphaned triggers. Please retry.",
      exit_code: exit_code,
      table_name: table_name,
    )
  when /Cannot connect to MySQL/
    raise PtOscError.new(
      'Database connection failed. Check MySQL server status and credentials.',
      exit_code: exit_code,
    )
  when /Foreign key/
    raise PtOscError.new(
      'Foreign key constraint issue. Consider using --alter-foreign-keys-method=rebuild_constraints or handling foreign keys manually.',
      exit_code: exit_code,
      table_name: table_name,
    )
  end
end