pgslice 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e344dce1fde075ab0621622d3f929e2c6677026
4
- data.tar.gz: 9fe6e7825852fb37692ff019908dfb82e5cf4095
3
+ metadata.gz: c5697462268c630c5fd8e72567fe4ac9b903d157
4
+ data.tar.gz: 686af5d5c45edff42d491f7378f1338299aa6a14
5
5
  SHA512:
6
- metadata.gz: a684bbbbc65be40c4a42850f0b54af657371649efef72f9f89337a8fae4ac046bd1fe758f1632ff2fc15c3a3d1e2c6feec7f2eaa70e13d9f815565993ff0e85a
7
- data.tar.gz: 6b5e6fe5958f81085c8ec9a3d0d0161e35aa29f616b2db32dca993b520e14178face97dbe43133dc919b750b24a220cfe5819cd1732f6c64307cf81f6ee22363
6
+ metadata.gz: e09e05ebe4cad9c34fae30ca04f6c6fe5ff6a1f3bb175ea55af9e29add720e9d6319939dd92be320d3360957c03cc5756862c8ef1918ff2d5277c7c47e315a88
7
+ data.tar.gz: cac51539b16171d3a54f4a93d3ee4539ae50b34267727fa883614c341693b49894951537fd45e0e4432ba78bbe8e9f21b1d7a932cf0f3f2ab4967b0c2c0ca2c7
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.2.2
2
+
3
+ - Set `lock_timeout` on `swap` to prevent bad things from happening
4
+ - Friendlier error messages
5
+
1
6
  ## 0.2.1
2
7
 
3
8
  - Added `--where` option to `fill`
data/README.md CHANGED
@@ -182,6 +182,8 @@ pgslice swap visits
182
182
  ```sql
183
183
  BEGIN;
184
184
 
185
+ SET LOCAL lock_timeout = '5s';
186
+
185
187
  ALTER TABLE visits RENAME TO visits_retired;
186
188
 
187
189
  ALTER TABLE visits_intermediate RENAME TO visits;
data/lib/pgslice.rb CHANGED
@@ -249,7 +249,7 @@ CREATE OR REPLACE FUNCTION #{trigger_name}()
249
249
  if options[:start]
250
250
  max_dest_id = options[:start]
251
251
  else
252
- min_source_id = min_id(source_table, primary_key, field, starting_time)
252
+ min_source_id = min_id(source_table, primary_key, field, starting_time, options[:where])
253
253
  max_dest_id = min_source_id - 1 if min_source_id
254
254
  end
255
255
  end
@@ -308,6 +308,8 @@ INSERT INTO #{dest_table} (#{fields})
308
308
  queries << "ALTER SEQUENCE #{sequence["sequence_name"]} OWNED BY #{table}.#{sequence["related_column"]};"
309
309
  end
310
310
 
311
+ queries.unshift("SET LOCAL lock_timeout = '#{options[:lock_timeout]}';") if server_version_num >= 90300
312
+
311
313
  run_queries(queries)
312
314
  end
313
315
 
@@ -349,6 +351,7 @@ INSERT INTO #{dest_table} (#{fields})
349
351
  o.string "--url"
350
352
  o.string "--source-table"
351
353
  o.string "--where"
354
+ o.string "--lock-timeout", default: "5s"
352
355
  o.on "-v", "--version", "print the version" do
353
356
  log PgSlice::VERSION
354
357
  @exit = true
@@ -401,18 +404,28 @@ INSERT INTO #{dest_table} (#{fields})
401
404
 
402
405
  def run_queries(queries)
403
406
  connection.transaction do
404
- execute("SET client_min_messages TO warning") unless options[:dry_run]
407
+ execute("SET LOCAL client_min_messages TO warning") unless options[:dry_run]
405
408
  log_sql "BEGIN;"
406
409
  log_sql
407
410
  queries.each do |query|
408
411
  log_sql query
409
412
  log_sql
410
- execute(query) unless options[:dry_run]
413
+ unless options[:dry_run]
414
+ begin
415
+ execute(query)
416
+ rescue PG::ServerError => e
417
+ abort("#{e.class.name}: #{e.message}")
418
+ end
419
+ end
411
420
  end
412
421
  log_sql "COMMIT;"
413
422
  end
414
423
  end
415
424
 
425
+ def server_version_num
426
+ execute("SHOW server_version_num")[0]["server_version_num"].to_i
427
+ end
428
+
416
429
  def existing_tables(like:)
417
430
  query = "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = $1 AND tablename LIKE $2"
418
431
  execute(query, ["public", like]).map { |r| r["tablename"] }.sort
@@ -453,9 +466,12 @@ INSERT INTO #{dest_table} (#{fields})
453
466
  execute(query)[0]["max"].to_i
454
467
  end
455
468
 
456
- def min_id(table, primary_key, column, starting_time)
469
+ def min_id(table, primary_key, column, starting_time, where)
457
470
  query = "SELECT MIN(#{primary_key}) FROM #{table}"
458
- query << " WHERE #{column} >= #{sql_date(starting_time)}" if starting_time
471
+ conditions = []
472
+ conditions << "#{column} >= #{sql_date(starting_time)}" if starting_time
473
+ conditions << where if where
474
+ query << " WHERE #{conditions.join(" AND ")}" if conditions.any?
459
475
  (execute(query)[0]["min"] || 1).to_i
460
476
  end
461
477
 
@@ -1,3 +1,3 @@
1
1
  module PgSlice
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgslice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
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-09-29 00:00:00.000000000 Z
11
+ date: 2016-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slop