module XMigra

Constants

BASE_32_ENCODING
DBOBJ_NAME_SPLITTER
DBQUOTE_STRIPPER
DatabaseSupportModules
FORMALIZATIONS
NULL_FILE
PLATFORM
VERSION
VersionControlSupportModules

Public Class Methods

access_artifact(info) click to toggle source
# File lib/xmigra.rb, line 224
def access_artifact(info)
  case info["define"]
  when "stored procedure" then StoredProcedure.new(info)
  when "view" then View.new(info)
  when "function" then Function.new(info)
  else
    raise SchemaError, "'define' not specified for access artifact '#{info['name']}'"
  end
end
base32encoding(bytes) click to toggle source
# File lib/xmigra.rb, line 271
def base32encoding(bytes)
  carry = 0
  carry_bits = 0
  ''.tap do |result|
    bytes.each_byte do |b|
      # From b we need the (5 - carry_bits) most significant bits
      needed_bits = 5 - carry_bits
      code_unit = (carry << needed_bits) | b >> (8 - needed_bits)
      result << BASE_32_ENCODING[code_unit]
      
      if needed_bits <= 3
        # Extra character out of this byte
        code_unit = (b >> (3 - needed_bits)) & 0x1F
        result << BASE_32_ENCODING[code_unit]
        carry_bits = (3 - needed_bits)
      else
        carry_bits = 8 - needed_bits
      end
      carry = b & ((1 << carry_bits) - 1)
    end
    
    if carry_bits > 0
      code_unit = carry << (5 - carry_bits)
      result << BASE_32_ENCODING[code_unit]
    end
    
    result << '=' * (7 - ((result.length + 7) % 8))
  end
end
canonize_path_case(s) click to toggle source
# File lib/xmigra.rb, line 191
def self.canonize_path_case(s)
  case PLATFORM
  when :mswin then s.downcase
  else s
  end
end
command_line_program() click to toggle source
# File lib/xmigra.rb, line 341
def self.command_line_program
  XMigra::Program.run(
    ARGV,
    :error=>proc do |e|
      STDERR.puts("#{e} (#{e.class})") unless e.is_a?(XMigra::Program::QuietError)
      if e.class.const_defined? :COMMAND_LINE_HELP
        STDERR.puts(XMigra.program_message(e.class::COMMAND_LINE_HELP))
      end
      log_error(e)
      exit(2) if e.is_a?(OptionParser::ParseError)
      exit(2) if e.is_a?(XMigra::Program::ArgumentError)
      exit(1)
    end
  )
end
dedent(s, prefix='') click to toggle source
# File lib/xmigra/utils.rb, line 2
def self.dedent(s, prefix='')
  margin = nil
  s.lines.map do |l|
    case 
    when margin.nil? && l =~ /^ *$/
      l
    when margin.nil?
      margin = /^ */.match(l)[0].length
      l[margin..-1]
    else
      /^(?: {0,#{margin}})(.*)/m.match(l)[1]
    end
  end.tap do |lines|
    lines.shift if lines.first == "\n"
  end.map do |l|
    prefix + l
  end.join('')
end
each_access_artifact(path) { |artifact)| ... } click to toggle source
# File lib/xmigra.rb, line 242
def each_access_artifact(path)
  Dir.glob(File.join(path, '*.yaml')).each do |fpath|
    artifact = load_access_artifact(fpath)
    (yield artifact) if artifact
  end
end
formalize(s) click to toggle source
# File lib/xmigra.rb, line 198
def self.formalize(s)
  FORMALIZATIONS.each_pair do |pattern, result|
    return result if pattern === s
  end
  return s
end
load_access_artifact(path) click to toggle source
# File lib/xmigra.rb, line 234
def load_access_artifact(path)
  info = YAML.load_file(path)
  info['name'] = File.basename(path, '.yaml')
  artifact = access_artifact(info)
  artifact.file_path = File.expand_path(path)
  return artifact
end
log_error(e) click to toggle source
# File lib/xmigra.rb, line 357
def self.log_error(e)
  if log_file = ENV['XMIGRA_LOG_FILE']
    Pathname(log_file).open('a') do |log|
      log.puts "#{Time.now}: #{e} (#{e.class})"
      e.backtrace.each do |frame|
        log.puts "    " + frame
      end
      
      while e.respond_to?(:cause) && e = e.cause
        log.puts "  Caused by: #{e} (#{e.class})"
        e.backtrace.each do |frame|
          log.puts "    " + frame
        end
      end
    end
  end
end
program_message(message, options={}) click to toggle source
# File lib/xmigra.rb, line 205
def self.program_message(message, options={})
  prog_pattern = options[:prog] || /%prog\b/
  
  steps = [$0]
  steps << (program = self.canonize_path_case(File.basename(steps[-1])))
  steps << (prog_name = self.formalize(File.basename(steps[-2], '.rb')))
  steps << message.to_s
  steps << steps[-1].gsub(prog_pattern, program)
  steps << steps[-1].gsub(/%program_name\b/, prog_name)
  steps << steps[-1].gsub(/%cmd\b/, options[:cmd] || '<cmd>')
  return steps[-1]
rescue
  STDERR.puts "steps: " + steps.inspect
  raise
end
secure_digest(s, options={:encoding=>:base64}) click to toggle source
# File lib/xmigra.rb, line 258
def secure_digest(s, options={:encoding=>:base64})
  digest_value = Digest::MD5.digest(s)
  case options[:encoding]
  when nil
    digest_value
  when :base64
    [digest_value].pack('m0').chomp
  when :base32
    base32encoding(digest_value)
  end
end
yaml_path(path) click to toggle source
# File lib/xmigra.rb, line 249
def yaml_path(path)
  path_s = path.to_s
  if path_s.end_with?('.yaml')
    return path
  else
    return path.class.new(path_s + '.yaml')
  end
end