Method: DBGeni::BaseModules::Code#apply_code

Defined in:
lib/dbgeni/base_code.rb

#apply_code(code_obj, force = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dbgeni/base_code.rb', line 58

def apply_code(code_obj, force=nil)
  ensure_initialized
  begin
    run_plugin(:before_code_apply, code_obj)
    code_obj.apply!(@config, connection, force)
    if code_obj.error_messages
      # Oracle can apply procs that still have errors. This is expected. Other databases
      # have errors raised for invalid procs, except when force is on, so this logic is
      # for when they are being forced through.
      if @config.db_type == 'oracle'
        @logger.info "Applied #{code_obj.to_s} (with errors)\n\n#{code_obj.error_messages}\nFull errors in #{code_obj.logfile}\n\n"
      else
        @logger.error "Failed to apply #{code_obj.filename}. Errors in #{code_obj.logfile}\n\n#{code_obj.error_messages}\n\n"
      end
    else
      @logger.info "Applied #{code_obj.to_s}"
    end
    run_plugin(:after_code_apply, code_obj)
  rescue DBGeni::MigratorCouldNotConnect
    @logger.error "Failed to connect to the database CLI"
    raise DBGeni::CodeApplyFailed
  rescue DBGeni::CodeApplyFailed => e
    # The only real way code can get here is if the user had insufficient privs
    # to create the proc, or there was other bad stuff in the proc file.
    # In this case, dbgeni should stop - but also treat the error like a migration error
    # as the error message will be in the logfile in the format standard SQL errors are.
    @logger.error "Failed to apply #{code_obj.filename}. Errors in #{code_obj.logfile}\n\n#{code_obj.error_messages}\n\n"
    raise DBGeni::CodeApplyFailed, e.to_s
  end
end