class RR::TableSpecResolver

Resolves table specifications as provided e. g. in the command line of rrscan

Attributes

session[RW]

The Session instance from which the table specifications are resolved.

Public Class Methods

new(session) click to toggle source

Creates a resolver that works based on the given Session instance.

# File lib/rubyrep/table_spec_resolver.rb, line 20
def initialize(session)
  self.session = session
end

Public Instance Methods

non_existing_tables(table_pairs) click to toggle source

Returns all those tables from the given table_pairs that do not exist.

Returns: A hash with keys :left and :right, with the value for each key being an array of non-existing tables for the according database. The keys only exist if there are according missing tables.

# File lib/rubyrep/table_spec_resolver.rb, line 31
def non_existing_tables(table_pairs)
  [:left, :right].inject({}) do |memo, database|
    found_tables = table_pairs.inject([]) do |phantom_tables, table_pair|
      phantom_tables << table_pair[database] unless tables(database).include?(table_pair[database])
      phantom_tables
    end
    memo[database] = found_tables unless found_tables.empty?
    memo
  end
end
resolve(included_table_specs, excluded_table_specs = [], verify = true) click to toggle source

Resolves the given array of table specificifications. Table specifications are either

  • strings as produced by BaseRunner#get_options or

  • actual regular expressions

If excluded_table_specs is provided, removes all tables that match it (even if otherwise matching included_table_specs).

If verify is true, raises an exception if any non-existing tables are specified.

Returns an array of table name pairs in Hash form. For example something like

[{:left => 'my_table', :right => 'my_table_backup'}]

Takes care that a table is only returned once.

# File lib/rubyrep/table_spec_resolver.rb, line 57
def resolve(included_table_specs, excluded_table_specs = [], verify = true)
  table_pairs = expand_table_specs(included_table_specs, verify)
  table_pairs = table_pairs_without_duplicates(table_pairs)
  table_pairs = table_pairs_without_excluded(table_pairs, excluded_table_specs)

  if verify
    non_existing_tables = non_existing_tables(table_pairs)
    unless non_existing_tables.empty?
      raise "non-existing tables specified: #{non_existing_tables.inspect}"
    end
  end

  table_pairs
end
tables(database) click to toggle source

Returns the array of tables of the specified database. Caches the table array.

  • database: either :left or :right

# File lib/rubyrep/table_spec_resolver.rb, line 11
def tables(database)
  @table_cache ||= {}
  unless @table_cache[database]
    @table_cache[database] = session.send(database).tables
  end
  @table_cache[database]
end

Private Instance Methods

expand_table_specs(table_specs, verify) click to toggle source

Helper for resolve Expands table specifications into table pairs. Parameters:

  • table_specs: An array of table specifications as described under resolve.

  • verify: If true, table specs in regexp format only resolve if the table exists in left and right database.

Return value: refer to resolve for a detailed description

# File lib/rubyrep/table_spec_resolver.rb, line 81
def expand_table_specs(table_specs, verify)
  table_pairs = []
  table_specs.each do |table_spec|

    # If it is a regexp, convert it in an according string
    table_spec = table_spec.inspect if table_spec.kind_of? Regexp

    case table_spec
    when /^\/.*\/$/ # matches e. g. '/^user/'
      table_spec = table_spec.sub(/^\/(.*)\/$/,'\1') # remove leading and trailing slash
      matching_tables = tables(:left).grep(Regexp.new(table_spec, Regexp::IGNORECASE))
      matching_tables.each do |table|
        if !verify or tables(:right).include? table
          table_pairs << {:left => table, :right => table}
        end
      end
    when /.+,.+/ # matches e. g. 'users,users_backup'
      pair = table_spec.match(/(.*),(.*)/)[1..2].map { |str| str.strip }
      table_pairs << {:left  => pair[0], :right => pair[1]}
    else # everything else: just a normal table
      table_pairs << {:left => table_spec.strip, :right => table_spec.strip}
    end
  end
  table_pairs
end
table_pairs_without_duplicates(table_pairs) click to toggle source

Helper for resolve Takes given table_pairs and removes all duplicates. Returns the result. Both the given and the returned table_pairs is an array of hashes with

  • :left: name of the left table

  • :right: name of the corresponding right table

# File lib/rubyrep/table_spec_resolver.rb, line 129
def table_pairs_without_duplicates(table_pairs)
  processed_left_tables = {}
  resulting_table_pairs = []
  table_pairs.each do |table_pair|
    unless processed_left_tables.include? table_pair[:left]
      resulting_table_pairs << table_pair
      processed_left_tables[table_pair[:left]] = true
    end
  end
  resulting_table_pairs
end
table_pairs_without_excluded(table_pairs, excluded_table_specs) click to toggle source

Helper for resolve Takes given table_pairs and removes all tables that are excluded. Returns the result. Both the given and the returned table_pairs is an array of hashes with

  • :left: name of the left table

  • :right: name of the corresponding right table

excluded_table_specs is the array of table specifications to be excluded.

# File lib/rubyrep/table_spec_resolver.rb, line 115
def table_pairs_without_excluded(table_pairs, excluded_table_specs)
  excluded_tables = expand_table_specs(excluded_table_specs, false).map do |table_pair|
    table_pair[:left]
  end
  table_pairs.select {|table_pair| not excluded_tables.include? table_pair[:left]}
end