Class: PgSync::Sync

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

Constant Summary

Constants included from Utils

Utils::COLOR_CODES

Instance Method Summary collapse

Methods included from Utils

#colorize, #confirm_tables_exist, #db_config_file, #deprecated, #escape, #first_schema, #friendly_name, #log, #monotonic_time, #output, #quote_ident, #quote_ident_full, #quote_string, #task_name, #warning

Constructor Details

#initialize(arguments, options) ⇒ Sync

Returns a new instance of Sync.



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

def initialize(arguments, options)
  @arguments = arguments
  @options = options
end

Instance Method Details

#performObject

Raises:



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
# File 'lib/pgsync/sync.rb', line 10

def perform
  started_at = monotonic_time

  args = @arguments
  opts = @options

  # only resolve commands from config, not CLI arguments
  [:to, :from].each do |opt|
    opts[opt] ||= resolve_source(config[opt.to_s])
  end

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

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

  raise Error, "No source" unless source.exists?
  raise Error, "No destination" unless destination.exists?

  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)

  if (opts[:preserve] || opts[:overwrite]) && destination.server_version_num < 90500
    raise Error, "Postgres 9.5+ is required for --preserve and --overwrite"
  end

  resolver = TaskResolver.new(args: args, opts: opts, source: source, destination: destination, config: config, first_schema: first_schema)
  tasks =
    resolver.tasks.map do |task|
      Task.new(source: source, destination: destination, config: config, table: task[:table], opts: opts.merge(sql: task[:sql]))
    end

  if opts[:in_batches] && tasks.size > 1
    raise Error, "Cannot use --in-batches with multiple tables"
  end

  confirm_tables_exist(source, tasks, "source")

  if opts[:list]
    confirm_tables_exist(destination, tasks, "destination")
    tasks.each do |task|
      log task_name(task)
    end
  else
    if opts[:schema_first] || opts[:schema_only]
      SchemaSync.new(source: source, destination: destination, tasks: tasks, args: args, opts: opts).perform
    end

    unless opts[:schema_only]
      TableSync.new(source: source, destination: destination, tasks: tasks, opts: opts, resolver: resolver).perform
    end

    log_completed(started_at)
  end
end