Class: PgSync::TableList

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/pgsync/table_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#colorize, #config_file, #db_config_file, #log, #search_tree

Constructor Details

#initialize(args, options, source, config) ⇒ TableList

Returns a new instance of TableList.



7
8
9
10
11
12
# File 'lib/pgsync/table_list.rb', line 7

def initialize(args, options, source, config)
  @args = args
  @opts = options
  @source = source
  @config = config
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



5
6
7
# File 'lib/pgsync/table_list.rb', line 5

def args
  @args
end

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/pgsync/table_list.rb', line 5

def config
  @config
end

#optsObject (readonly)

Returns the value of attribute opts.



5
6
7
# File 'lib/pgsync/table_list.rb', line 5

def opts
  @opts
end

#sourceObject (readonly)

Returns the value of attribute source.



5
6
7
# File 'lib/pgsync/table_list.rb', line 5

def source
  @source
end

Instance Method Details

#tablesObject



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pgsync/table_list.rb', line 14

def tables
  tables = nil

  if opts[:groups]
    tables ||= Hash.new { |hash, key| hash[key] = {} }
    specified_groups = to_arr(opts[:groups])
    specified_groups.map do |tag|
      group, id = tag.split(":", 2)
      if (t = (config["groups"] || {})[group])
        add_tables(tables, t, id, args[1])
      else
        raise Error, "Group not found: #{group}"
      end
    end
  end

  if opts[:tables]
    tables ||= Hash.new { |hash, key| hash[key] = {} }
    to_arr(opts[:tables]).each do |tag|
      table, id = tag.split(":", 2)
      raise Error, "Cannot use parameters with tables" if id
      add_table(tables, table, id, args[1])
    end
  end

  if args[0]
    # could be a group, table, or mix
    tables ||= Hash.new { |hash, key| hash[key] = {} }
    specified_groups = to_arr(args[0])
    specified_groups.map do |tag|
      group, id = tag.split(":", 2)
      if (t = (config["groups"] || {})[group])
        add_tables(tables, t, id, args[1])
      else
        raise Error, "Cannot use parameters with tables" if id
        add_table(tables, group, id, args[1])
      end
    end
  end

  tables ||= begin
    exclude = to_arr(opts[:exclude])
    exclude = source.fully_resolve_tables(exclude).keys if exclude.any?

    tabs = source.tables
    unless opts[:all_schemas]
      schemas = Set.new(opts[:schemas] ? to_arr(opts[:schemas]) : source.search_path)
      tabs.select! { |t| schemas.include?(t.split(".", 2)[0]) }
    end

    Hash[(tabs - exclude).map { |k| [k, {}] }]
  end

  source.fully_resolve_tables(tables)
end