Method: PgSync::Sync#perform

Defined in:
lib/pgsync/sync.rb

#perform(options) ⇒ Object

Raises:



5
6
7
8
9
10
11
12
13
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pgsync/sync.rb', line 5

def perform(options)
  args = options.arguments
  opts = options.to_hash
  @options = opts

  # merge config
  [:to, :from, :to_safe, :exclude, :schemas].each do |opt|
    opts[opt] ||= config[opt.to_s]
  end

  # TODO remove deprecations
  map_deprecations(args, opts)

  # start
  start_time = Time.now

  if args.size > 2
    raise Error, "Usage:\n    pgsync [options]"
  end

  source = DataSource.new(opts[:from])
  raise Error, "No source" unless source.exists?

  destination = DataSource.new(opts[:to])
  raise Error, "No destination" unless destination.exists?

  begin
    # start connections
    source.host
    destination.host

    unless opts[:to_safe] || destination.local?
      raise Error, "Danger! Add `to_safe: true` to `.pgsync.yml` if the destination is not localhost or 127.0.0.1"
    end

    print_description("From", source)
    print_description("To", destination)
  ensure
    source.close
    destination.close
  end

  tables = nil
  begin
    tables = TableList.new(args, opts, source, config).tables
  ensure
    source.close
  end

  confirm_tables_exist(source, tables, "source")

  if opts[:list]
    confirm_tables_exist(destination, tables, "destination")

    list_items =
      if args[0] == "groups"
        (config["groups"] || {}).keys
      else
        tables.keys
      end

    pretty_list list_items
  else
    if opts[:schema_first] || opts[:schema_only]
      if opts[:preserve]
        raise Error, "Cannot use --preserve with --schema-first or --schema-only"
      end

      log "* Dumping schema"
      schema_tables = tables if !opts[:all_schemas] || opts[:tables] || opts[:groups] || args[0] || opts[:exclude]
      sync_schema(source, destination, schema_tables)
    end

    unless opts[:schema_only]
      confirm_tables_exist(destination, tables, "destination")

      in_parallel(tables, first_schema: source.search_path.find { |sp| sp != "pg_catalog" }) do |table, table_opts|
        TableSync.new.sync(config, table, opts.merge(table_opts), source.url, destination.url)
      end
    end

    log_completed(start_time)
  end
end