Class: PgSync::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Client

Returns a new instance of Client.



24
25
26
27
28
29
# File 'lib/pgsync.rb', line 24

def initialize(args)
  $stdout.sync = true
  @exit = false
  @arguments, @options = parse_args(args)
  @mutex = MultiProcessing::Mutex.new
end

Instance Method Details

#performObject

TODO clean up this mess



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pgsync.rb', line 32

def perform
  return if @exit

  start_time = Time.now

  args, opts = @arguments, @options
  [:to, :from, :to_safe, :exclude].each do |opt|
    opts[opt] ||= config[opt.to_s]
  end
  command = args[0]

  # setup hack
  if opts[:setup]
    command = "setup"
    args[1] = args[0]
  end
  if command == "setup"
    setup(db_config_file(args[1]) || config_file || ".pgsync.yml")
  else
    source = parse_source(opts[:from])
    abort "No source" unless source
    source_uri = parse_uri(source)

    destination = parse_source(opts[:to])
    abort "No destination" unless destination
    destination_uri = parse_uri(destination)
    abort "Danger! Add `to_safe: true` to `.pgsync.yml` if the destination is not localhost or 127.0.0.1" unless %(localhost 127.0.0.1).include?(destination_uri.host) || opts[:to_safe]

    print_uri("From", source_uri)
    print_uri("To", destination_uri)

    from_uri = source_uri
    to_uri = destination_uri

    tables = table_list(args, opts, from_uri)

    if args[0] == "schema" || opts[:schema_only]
      time =
        benchmark do
          log "* Dumping schema"
          tables = tables.keys.map { |t| "-t #{t}" }.join(" ")
          dump_command = "pg_dump --verbose --schema-only --no-owner --no-acl --clean #{tables} #{to_url(source_uri)}"
          restore_command = "psql -q -d #{to_url(destination_uri)}"
          system("#{dump_command} | #{restore_command}")
        end

      log "* DONE (#{time.round(1)}s)"
    else
      with_connection(to_uri, timeout: 3) do |conn|
        tables.keys.each do |table|
          unless table_exists?(conn, table, "public")
            abort "Table does not exist in destination: #{table}"
          end
        end
      end

      if opts[:list]
        if args[0] == "groups"
          pretty_list (config["groups"] || {}).keys
        else
          pretty_list tables.keys
        end
      else
        in_parallel(tables) do |table, table_opts|
          sync_table(table, opts.merge(table_opts), from_uri, to_uri)
        end

        time = Time.now - start_time
        log "Completed in #{time.round(1)}s"
      end
    end
  end
  true
end