pgslice 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dca58e320a519d76827d4e80a0ac06f6de27c6a4
4
- data.tar.gz: 42c56e058c53df60248f805215cae4b42ce71246
3
+ metadata.gz: 24b5596e6ea1c788ff5b367fb6ca05b0c76e626f
4
+ data.tar.gz: a6776fcdf658bd08f4a6adbeee3239b602eba91d
5
5
  SHA512:
6
- metadata.gz: f25931d431807f8e8a6155042f4a87a2bd76812d4d28b09b6f283e9445996e9a2a86f3991eff1c2f274b9b8afddd1e1ea9c31e6794a8c623f5aebee3fa20c1a6
7
- data.tar.gz: fc9fe5a59c005c348ff19ec3e1ecf7ff946a9af2fca577ca6566360367b0b1ae3b810fce17375c69973d6720db54eebd2f2737de4c6c414f91ce6e6aec66abac
6
+ metadata.gz: 5bb36f0e164418e57d54ebf9bb0192d75bf20977261fc6e83ff613d15afe3199aace4a0af343f8784518e6b4e460b245ba6029e61e427c5abd0405fff8a70acf
7
+ data.tar.gz: da11ed42838cd526441469108892379aadbee43bfcbaa657568b8d7c0721b19e5bc275af330bfd1e924c1bb54ae2109751ab62b207dc440332231b2277b7fa32
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.3.4
2
+
3
+ - Added `analyze` method
4
+ - Fixed `fill` with `--dry-run` option
5
+ - Better error message for tables without primary key
6
+
1
7
  ## 0.3.3
2
8
 
3
9
  - Fixed error when creating partitions
data/README.md CHANGED
@@ -54,7 +54,13 @@ This will give you the `pgslice` command.
54
54
 
55
55
  To sync data across different databases, check out [pgsync](https://github.com/ankane/pgsync).
56
56
 
57
- 6. Swap the intermediate table with the original table
57
+ 6. Analyze tables
58
+
59
+ ```sh
60
+ pgslice analyze <table>
61
+ ```
62
+
63
+ 7. Swap the intermediate table with the original table
58
64
 
59
65
  ```sh
60
66
  pgslice swap <table>
@@ -62,13 +68,13 @@ This will give you the `pgslice` command.
62
68
 
63
69
  The original table is renamed `<table>_retired` and the intermediate table is renamed `<table>`.
64
70
 
65
- 7. Fill the rest (rows inserted between the first fill and the swap)
71
+ 8. Fill the rest (rows inserted between the first fill and the swap)
66
72
 
67
73
  ```sh
68
74
  pgslice fill <table> --swapped
69
75
  ```
70
76
 
71
- 8. Back up the retired table with a tool like [pg_dump](https://www.postgresql.org/docs/current/static/app-pgdump.html) and drop it
77
+ 9. Back up the retired table with a tool like [pg_dump](https://www.postgresql.org/docs/current/static/app-pgdump.html) and drop it
72
78
 
73
79
  ```sql
74
80
  pg_dump -c -Fc -t <table>_retired $PGSLICE_URL > <table>_retired.dump
data/Rakefile CHANGED
@@ -5,6 +5,7 @@ Rake::TestTask.new(:test) do |t|
5
5
  t.libs << "test"
6
6
  t.libs << "lib"
7
7
  t.test_files = FileList["test/**/*_test.rb"]
8
+ t.warning = false
8
9
  end
9
10
 
10
- task default: :spec
11
+ task default: :test
data/lib/pgslice.rb CHANGED
@@ -37,8 +37,10 @@ module PgSlice
37
37
  unswap
38
38
  when "unprep"
39
39
  unprep
40
+ when "analyze"
41
+ analyze
40
42
  when nil
41
- log "Commands: add_partitions, fill, prep, swap, unprep, unswap"
43
+ log "Commands: add_partitions, analyze, fill, prep, swap, unprep, unswap"
42
44
  else
43
45
  abort "Unknown command: #{@command}"
44
46
  end
@@ -242,6 +244,7 @@ CREATE OR REPLACE FUNCTION #{trigger_name}()
242
244
  end
243
245
 
244
246
  primary_key = self.primary_key(table)
247
+ abort "No primary key" unless primary_key
245
248
  max_source_id = max_id(source_table, primary_key)
246
249
 
247
250
  max_dest_id =
@@ -264,6 +267,11 @@ CREATE OR REPLACE FUNCTION #{trigger_name}()
264
267
 
265
268
  i = 1
266
269
  batch_count = ((max_source_id - starting_id) / batch_size.to_f).ceil
270
+
271
+ if batch_count == 0
272
+ log_sql "/* nothing to fill */"
273
+ end
274
+
267
275
  while starting_id < max_source_id
268
276
  where = "#{primary_key} > #{starting_id} AND #{primary_key} <= #{starting_id + batch_size}"
269
277
  if starting_time
@@ -280,9 +288,7 @@ INSERT INTO #{dest_table} (#{fields})
280
288
  WHERE #{where}
281
289
  SQL
282
290
 
283
- log_sql(query)
284
- log_sql
285
- execute(query)
291
+ run_query(query)
286
292
 
287
293
  starting_id += batch_size
288
294
  i += 1
@@ -339,6 +345,17 @@ INSERT INTO #{dest_table} (#{fields})
339
345
  run_queries(queries)
340
346
  end
341
347
 
348
+ def analyze
349
+ table = arguments.first
350
+ parent_table = options[:swapped] ? table : intermediate_name(table)
351
+
352
+ abort "Usage: pgslice analyze <table>" if arguments.length != 1
353
+
354
+ existing_tables = self.existing_tables(like: "#{table}_%").select { |t| /\A#{Regexp.escape("#{table}_")}\d{6,8}\z/.match(t) }
355
+ analyze_list = existing_tables + [parent_table]
356
+ run_queries_without_transaction analyze_list.map { |t| "ANALYZE VERBOSE #{t};" }
357
+ end
358
+
342
359
  # arguments
343
360
 
344
361
  def parse_args(args)
@@ -420,21 +437,29 @@ INSERT INTO #{dest_table} (#{fields})
420
437
  execute("SET LOCAL client_min_messages TO warning") unless options[:dry_run]
421
438
  log_sql "BEGIN;"
422
439
  log_sql
423
- queries.each do |query|
424
- log_sql query
425
- log_sql
426
- unless options[:dry_run]
427
- begin
428
- execute(query)
429
- rescue PG::ServerError => e
430
- abort("#{e.class.name}: #{e.message}")
431
- end
432
- end
433
- end
440
+ run_queries_without_transaction(queries)
434
441
  log_sql "COMMIT;"
435
442
  end
436
443
  end
437
444
 
445
+ def run_query(query)
446
+ log_sql query
447
+ unless options[:dry_run]
448
+ begin
449
+ execute(query)
450
+ rescue PG::ServerError => e
451
+ abort("#{e.class.name}: #{e.message}")
452
+ end
453
+ end
454
+ log_sql
455
+ end
456
+
457
+ def run_queries_without_transaction(queries)
458
+ queries.each do |query|
459
+ run_query(query)
460
+ end
461
+ end
462
+
438
463
  def server_version_num
439
464
  execute("SHOW server_version_num")[0]["server_version_num"].to_i
440
465
  end
@@ -1,3 +1,3 @@
1
1
  module PgSlice
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
data/pgslice.gemspec CHANGED
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency "bundler"
24
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "minitest"
25
26
  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.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: 2017-03-23 00:00:00.000000000 Z
11
+ date: 2017-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slop
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description:
70
84
  email:
71
85
  - andrew@chartkick.com
@@ -103,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
117
  version: '0'
104
118
  requirements: []
105
119
  rubyforge_project:
106
- rubygems_version: 2.6.8
120
+ rubygems_version: 2.6.11
107
121
  signing_key:
108
122
  specification_version: 4
109
123
  summary: Postgres partitioning as easy as pie