Module: DBGeni::BaseModules::Code

Included in:
DBGeni::Base
Defined in:
lib/dbgeni/base_code.rb

Instance Method Summary collapse

Instance Method Details

#apply_all_code(force = nil) ⇒ Object

Applying


34
35
36
37
38
39
40
41
# File 'lib/dbgeni/base_code.rb', line 34

def apply_all_code(force=nil)
  ensure_initialized
  code_files = code
  if code_files.length == 0
    raise DBGeni::NoOutstandingCode
  end
  apply_code_list(code_files, true)
end

#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

#apply_list_of_code(object_names, force = nil) ⇒ Object


52
53
54
55
56
# File 'lib/dbgeni/base_code.rb', line 52

def apply_list_of_code(object_names, force=nil)
  ensure_initialized
  code_files = list_of_code(object_names)
  apply_code_list(code_files, force)
end

#apply_outstanding_code(force = nil) ⇒ Object


43
44
45
46
47
48
49
50
# File 'lib/dbgeni/base_code.rb', line 43

def apply_outstanding_code(force=nil)
  ensure_initialized
  code_files = outstanding_code
  if code_files.length == 0
    raise DBGeni::NoOutstandingCode
  end
  apply_code_list(code_files, force)
end

#codeObject

This module isn’t much good on its own, but it is here purely to break up the code in the Base class. This module should be included into base for code operations to work correctly


9
10
11
12
# File 'lib/dbgeni/base_code.rb', line 9

def code
  @code_list ||= DBGeni::CodeList.new(@config.code_dir)
  @code_list.code
end

#current_codeObject


14
15
16
17
18
# File 'lib/dbgeni/base_code.rb', line 14

def current_code
  ensure_initialized
  code
  @code_list.current(@config, connection)
end

#list_of_code(list) ⇒ Object


26
27
28
29
30
# File 'lib/dbgeni/base_code.rb', line 26

def list_of_code(list)
  ensure_initialized
  code
  @code_list.list(list, @config, connection)
end

#outstanding_codeObject


20
21
22
23
24
# File 'lib/dbgeni/base_code.rb', line 20

def outstanding_code
  ensure_initialized
  code
  @code_list.outstanding(@config, connection)
end

#remove_all_code(force = nil) ⇒ Object


89
90
91
92
93
94
95
96
# File 'lib/dbgeni/base_code.rb', line 89

def remove_all_code(force=nil)
  ensure_initialized
  code_files = code
  if code_files.length == 0
    raise DBGeni::NoCodeFilesExist
  end
  apply_code_list(code_files, force, false)
end

#remove_code(code_obj, force = nil) ⇒ Object


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dbgeni/base_code.rb', line 104

def remove_code(code_obj, force=nil)
  ensure_initialized
  begin
    run_plugin(:before_code_remove, code_obj)
    code_obj.remove!(@config, connection, force)
    @logger.info "Removed #{code_obj.to_s}"
    run_plugin(:after_code_remove, code_obj)
  rescue DBGeni::MigratorCouldNotConnect
    @logger.error "Failed to connect to the database CLI"
    raise DBGeni::CodeRemoveFailed
  rescue DBGeni::CodeRemoveFailed => e
    # Not sure if the code can even get here. Many if timeout waiting for lock on object?
    # In this case, dbgeni should stop - but also treat the error like a migration error

    # TODO - not sure this is even correct - dropping code doesn't create a logfile ...
    @logger.error "Failed to remove #{code_obj.filename}. Errors in #{code_obj.logfile}"
    raise DBGeni::CodeRemoveFailed
  end
end

#remove_list_of_code(object_names, force = nil) ⇒ Object


98
99
100
101
102
# File 'lib/dbgeni/base_code.rb', line 98

def remove_list_of_code(object_names, force=nil)
  ensure_initialized
  code_files = list_of_code(object_names)
  apply_code_list(code_files, force, false)
end