pgsync 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of pgsync might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: be53714690e45e12a419c524673bddecbb151fa1
4
- data.tar.gz: 6c4a70c4296a2bc96121fbfc7b0512dc9e47ab1b
3
+ metadata.gz: c8bd6a2e877c863a693c0447ac9dc8a621a4c2c5
4
+ data.tar.gz: a5322f143b1daaa5cf60b55dbd664b22d31f7b7c
5
5
  SHA512:
6
- metadata.gz: 13542ba5e8ac5f81be20154f51ba9ea45fd9fc2bfb8d9e3abcbd32eee620f4a3fbb1c71493909ef44664982240ea59e05f0ed829032d8c6ec06f0a4c4b53cf7c
7
- data.tar.gz: fe69eac5b5acdfa879664ed5bc88ae99abe853f5cbf4af093b43849be98315acb2695f2fd92ca49b883322a03d541f2da7e79c7914040bbf4ab0943ff68db5c5
6
+ metadata.gz: 9370577fcad8039401eb4d19dcad7ae5d24af1c0a1dfb0904e940ff8ab802a33218390dbceda4e214ea0f2591c507b14099a09819f1b9df75463ea64b16dc2b7
7
+ data.tar.gz: cd474765176aea15d99175ae302d47eae50bb3e507ab7cfe7c8d605cf22699fce49c4fbdf8793116e7a7b629b15292ee732c804c67090d06ded178bf73d78933
@@ -1,3 +1,7 @@
1
+ # 0.3.4
2
+
3
+ - Added `--in-batches` mode for production transfers with `--batch-size` and `--sleep`
4
+
1
5
  # 0.3.3
2
6
 
3
7
  - Added `-d` option as an alias for `--db`
@@ -176,7 +176,48 @@ module PgSync
176
176
  end
177
177
 
178
178
  copy_to_command = "COPY (SELECT #{copy_fields} FROM #{table}#{sql_clause}) TO STDOUT"
179
- if !opts[:truncate] && (opts[:overwrite] || opts[:preserve] || !sql_clause.empty?)
179
+ if opts[:in_batches]
180
+ primary_key = self.primary_key(from_connection, table, "public")
181
+ abort "No primary key" unless primary_key
182
+
183
+ from_max_id = max_id(from_connection, table, primary_key, sql_clause)
184
+ to_max_id = max_id(to_connection, table, primary_key, sql_clause) + 1
185
+
186
+ if to_max_id == 1
187
+ from_min_id = min_id(from_connection, table, primary_key, sql_clause)
188
+ to_max_id = from_min_id if from_min_id > 0
189
+ end
190
+
191
+ starting_id = to_max_id
192
+ batch_size = opts[:batch_size]
193
+
194
+ i = 1
195
+ batch_count = ((from_max_id - starting_id + 1) / batch_size.to_f).ceil
196
+
197
+ while starting_id <= from_max_id
198
+ where = "#{primary_key} >= #{starting_id} AND #{primary_key} < #{starting_id + batch_size}"
199
+ log " #{i}/#{batch_count}: #{where}"
200
+
201
+ # TODO be smarter for advance sql clauses
202
+ batch_sql_clause = " #{sql_clause.length > 0 ? "#{sql_clause} AND" : "WHERE"} #{where}"
203
+
204
+ batch_copy_to_command = "COPY (SELECT #{copy_fields} FROM #{table}#{batch_sql_clause}) TO STDOUT"
205
+ to_connection.copy_data "COPY #{table} (#{fields}) FROM STDIN" do
206
+ from_connection.copy_data batch_copy_to_command do
207
+ while row = from_connection.get_copy_data
208
+ to_connection.put_copy_data(row)
209
+ end
210
+ end
211
+ end
212
+
213
+ starting_id += batch_size
214
+ i += 1
215
+
216
+ if opts[:sleep] && starting_id <= from_max_id
217
+ sleep(opts[:sleep])
218
+ end
219
+ end
220
+ elsif !opts[:truncate] && (opts[:overwrite] || opts[:preserve] || !sql_clause.empty?)
180
221
  primary_key = self.primary_key(from_connection, table, "public")
181
222
  abort "No primary key" unless primary_key
182
223
 
@@ -264,6 +305,9 @@ Options:}
264
305
  o.boolean "--schema-only", "schema only", default: false
265
306
  o.boolean "--no-rules", "do not apply data rules", default: false
266
307
  o.boolean "--setup", "setup", default: false
308
+ o.boolean "--in-batches", "in batches", default: false, help: false
309
+ o.integer "--batch-size", "batch size", default: 10000, help: false
310
+ o.float "--sleep", "sleep", default: 0, help: false
267
311
  o.on "-v", "--version", "print the version" do
268
312
  log PgSync::VERSION
269
313
  @exit = true
@@ -505,7 +549,7 @@ Options:}
505
549
  end
506
550
 
507
551
  def in_parallel(tables, &block)
508
- if @options[:debug]
552
+ if @options[:debug] || @options[:in_batches]
509
553
  tables.each(&block)
510
554
  else
511
555
  Parallel.each(tables, &block)
@@ -592,6 +636,14 @@ Options:}
592
636
  tables
593
637
  end
594
638
 
639
+ def max_id(conn, table, primary_key, sql_clause = nil)
640
+ conn.exec("SELECT MAX(#{escape_identifier(primary_key)}) FROM #{escape_identifier(table)}#{sql_clause}").to_a[0]["max"].to_i
641
+ end
642
+
643
+ def min_id(conn, table, primary_key, sql_clause = nil)
644
+ conn.exec("SELECT MIN(#{escape_identifier(primary_key)}) FROM #{escape_identifier(table)}#{sql_clause}").to_a[0]["min"].to_i
645
+ end
646
+
595
647
  def cast(value)
596
648
  value.to_s
597
649
  end
@@ -1,3 +1,3 @@
1
1
  module PgSync
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgsync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-26 00:00:00.000000000 Z
11
+ date: 2016-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slop