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 = Time.now
args = @arguments
opts = @options
[:to, :from].each do |opt|
opts[opt] ||= resolve_source(config[opt.to_s])
end
[: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
|