pgsync 0.6.2 → 0.6.3

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
  SHA256:
3
- metadata.gz: 531d7975b4942abc5670b8c4ad43717da1326eacacc3b95f5d5797236cb82bb3
4
- data.tar.gz: e32dfb82469fa2ebeeea844f0a09e87b8068bd7a201111b90d374eef779381af
3
+ metadata.gz: 0d3ff88829338544bfe3434a16a00640e6496b7b89b1ef5dd43e66f844ea51ce
4
+ data.tar.gz: d5457e8c2596fdda651c9739712fe0934af14028295ea9cd6addfc70438ced16
5
5
  SHA512:
6
- metadata.gz: 4a33a680baaf706ad25fed35eaee3a8c3b191c7a5f6600b4ada42e55143a12226b0e1be189cf4c29d4866982e4f8c7b4915c4517375913083f3210f5fb131598
7
- data.tar.gz: a3825c52c5e96c869ea06ed0ec969e1835aec5614f8bd1eb9255995972bdb3e526dd2996802e4d467b1497cfe371509b8a973cc8e3b61988d55f7fb0824d5293
6
+ metadata.gz: 6f9549d0d85cb502b5b88a3178fe617bf94f032d72787ab6dcf8f6097dc256ca2e969e2c4a434ebf33e4de17a4f11c590c57ef8cae420b20fe4f39bfed795baa
7
+ data.tar.gz: b10ac04af7dc26c3067c2ff73f163c69d73dfb5c1f559585adcf774312ac78ac07dcd790e1706cc571a90f0e4573060cedf01fc6ebd8b1732fbaa16b486213dd
@@ -1,3 +1,8 @@
1
+ ## 0.6.3 (2020-06-09)
2
+
3
+ - Added `--defer-constraints-v2` option
4
+ - Ensure consistent source snapshot with `--disable-integrity`
5
+
1
6
  ## 0.6.2 (2020-06-09)
2
7
 
3
8
  - Added support for `--disable-integrity` on Amazon RDS
data/README.md CHANGED
@@ -198,20 +198,20 @@ Rules starting with `unique_` require the table to have a single column primary
198
198
 
199
199
  Foreign keys can make it difficult to sync data. Three options are:
200
200
 
201
- 1. Manually specify the order of tables
202
- 2. Use deferrable constraints
203
- 3. Disable foreign key triggers, which can silently break referential integrity
201
+ 1. Defer constraints (recommended)
202
+ 2. Manually specify the order of tables
203
+ 3. Disable foreign key triggers, which can silently break referential integrity (not recommended)
204
204
 
205
- When manually specifying the order, use `--jobs 1` so tables are synced one-at-a-time.
205
+ To defer constraints, use:
206
206
 
207
207
  ```sh
208
- pgsync table1,table2,table3 --jobs 1
208
+ pgsync --defer-constraints-v2
209
209
  ```
210
210
 
211
- If your tables have [deferrable constraints](https://begriffs.com/posts/2017-08-27-deferrable-sql-constraints.html), use:
211
+ To manually specify the order of tables, use `--jobs 1` so tables are synced one-at-a-time.
212
212
 
213
213
  ```sh
214
- pgsync --defer-constraints
214
+ pgsync table1,table2,table3 --jobs 1
215
215
  ```
216
216
 
217
217
  To disable foreign key triggers and potentially break referential integrity, use:
@@ -67,7 +67,8 @@ Options:}
67
67
  o.integer "--batch-size", "batch size", default: 10000, help: false
68
68
  o.float "--sleep", "sleep", default: 0, help: false
69
69
  o.boolean "--fail-fast", "stop on the first failed table", default: false
70
- o.boolean "--defer-constraints", "defer constraints", default: false
70
+ o.boolean "--defer-constraints", "defer constraints", default: false, help: false
71
+ o.boolean "--defer-constraints-v2", "defer constraints", default: false
71
72
  o.boolean "--disable-user-triggers", "disable non-system triggers", default: false
72
73
  o.boolean "--disable-integrity", "disable foreign key triggers", default: false
73
74
  # private, for testing
@@ -141,7 +141,7 @@ module PgSync
141
141
  options = {start: start, finish: finish}
142
142
 
143
143
  jobs = opts[:jobs]
144
- if opts[:debug] || opts[:in_batches] || opts[:defer_constraints]
144
+ if opts[:debug] || opts[:in_batches] || opts[:defer_constraints] || opts[:defer_constraints_v2] || opts[:disable_integrity] || opts[:disable_integrity_v2]
145
145
  warning "--jobs ignored" if jobs
146
146
  jobs = 0
147
147
  end
@@ -172,8 +172,23 @@ module PgSync
172
172
  end
173
173
 
174
174
  def maybe_defer_constraints
175
- if opts[:defer_constraints]
175
+ if opts[:disable_integrity] || opts[:disable_integrity_v2]
176
+ # create a transaction on the source
177
+ # to ensure we get a consistent snapshot
178
+ source.transaction do
179
+ yield
180
+ end
181
+ elsif opts[:defer_constraints] || opts[:defer_constraints_v2]
176
182
  destination.transaction do
183
+ if opts[:defer_constraints_v2]
184
+ table_constraints = non_deferrable_constraints(destination)
185
+ table_constraints.each do |table, constraints|
186
+ constraints.each do |constraint|
187
+ destination.execute("ALTER TABLE #{quote_ident_full(table)} ALTER CONSTRAINT #{quote_ident(constraint)} DEFERRABLE")
188
+ end
189
+ end
190
+ end
191
+
177
192
  destination.execute("SET CONSTRAINTS ALL DEFERRED")
178
193
 
179
194
  # create a transaction on the source
@@ -181,6 +196,20 @@ module PgSync
181
196
  source.transaction do
182
197
  yield
183
198
  end
199
+
200
+ # set them back
201
+ # there are 3 modes: DEFERRABLE INITIALLY DEFERRED, DEFERRABLE INITIALLY IMMEDIATE, and NOT DEFERRABLE
202
+ # we only update NOT DEFERRABLE
203
+ # https://www.postgresql.org/docs/current/sql-set-constraints.html
204
+ if opts[:defer_constraints_v2]
205
+ destination.execute("SET CONSTRAINTS ALL IMMEDIATE")
206
+
207
+ table_constraints.each do |table, constraints|
208
+ constraints.each do |constraint|
209
+ destination.execute("ALTER TABLE #{quote_ident_full(table)} ALTER CONSTRAINT #{quote_ident(constraint)} NOT DEFERRABLE")
210
+ end
211
+ end
212
+ end
184
213
  end
185
214
  else
186
215
  yield
@@ -156,7 +156,7 @@ module PgSync
156
156
  destination.execute("INSERT INTO #{quoted_table} (SELECT * FROM #{quote_ident_full(temp_table)}) ON CONFLICT (#{on_conflict}) DO #{action}")
157
157
  else
158
158
  # use delete instead of truncate for foreign keys
159
- if opts[:defer_constraints]
159
+ if opts[:defer_constraints] || opts[:defer_constraints_v2]
160
160
  destination.execute("DELETE FROM #{quoted_table}")
161
161
  else
162
162
  destination.truncate(table)
@@ -1,3 +1,3 @@
1
1
  module PgSync
2
- VERSION = "0.6.2"
2
+ VERSION = "0.6.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgsync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane