pgsync 0.5.5 → 0.6.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 +4 -4
- data/CHANGELOG.md +38 -3
- data/LICENSE.txt +1 -1
- data/README.md +87 -32
- data/config.yml +4 -0
- data/lib/pgsync.rb +6 -1
- data/lib/pgsync/client.rb +55 -57
- data/lib/pgsync/data_source.rb +55 -104
- data/lib/pgsync/init.rb +50 -6
- data/lib/pgsync/schema_sync.rb +83 -0
- data/lib/pgsync/sequence.rb +29 -0
- data/lib/pgsync/sync.rb +82 -199
- data/lib/pgsync/table.rb +28 -0
- data/lib/pgsync/table_sync.rb +254 -207
- data/lib/pgsync/task.rb +325 -0
- data/lib/pgsync/task_resolver.rb +237 -0
- data/lib/pgsync/utils.rb +55 -14
- data/lib/pgsync/version.rb +1 -1
- metadata +7 -3
- data/lib/pgsync/table_list.rb +0 -141
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ecb467f4d3112edfcfebe19913e29daeddfe1e4cc10e18bd9e8850fbceced57
|
4
|
+
data.tar.gz: 4d8309dec9e8238074267baf583fe333c7b1d3300ad80398748e15905eb1e539
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 145eb31e2565257a5adedc05c1308692dffb1e0785728ecd38230bb23c34c4ba6ecf08512201f707f457b136e0b7a22ca511b46fb7834f2045c041ff43dd6ccc
|
7
|
+
data.tar.gz: cde51dee36149f9f8c21e26eecd504de68d8a47c2524f084b74962d7ceae1a01b59841261fc6c112cf94b31a89f55b9a991c05d1af0db41c46ed615fc56e888f
|
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,44 @@
|
|
1
|
+
## 0.6.4 (2020-06-10)
|
2
|
+
|
3
|
+
- Log SQL with `--debug` option
|
4
|
+
- Improved sequence queries
|
5
|
+
|
6
|
+
## 0.6.3 (2020-06-09)
|
7
|
+
|
8
|
+
- Added `--defer-constraints-v2` option
|
9
|
+
- Ensure consistent source snapshot with `--disable-integrity`
|
10
|
+
|
11
|
+
## 0.6.2 (2020-06-09)
|
12
|
+
|
13
|
+
- Added support for `--disable-integrity` on Amazon RDS
|
14
|
+
- Fixed error when excluded table not found in source
|
15
|
+
|
16
|
+
## 0.6.1 (2020-06-07)
|
17
|
+
|
18
|
+
- Added Django and Laravel integrations
|
19
|
+
|
20
|
+
## 0.6.0 (2020-06-07)
|
21
|
+
|
22
|
+
- Added messages for different column types and non-deferrable constraints
|
23
|
+
- Added support for wildcards to `--exclude`
|
24
|
+
- Improved `--overwrite` and `--preserve` options for foreign keys
|
25
|
+
- Improved output for schema sync
|
26
|
+
- Fixed `--overwrite` and `--preserve` options for multicolumn primary keys
|
27
|
+
- Fixed output for notices
|
28
|
+
|
29
|
+
Breaking
|
30
|
+
|
31
|
+
- Syncs shared tables instead of raising an error when tables missing in destination
|
32
|
+
- Raise an error when `--config` or `--db` option provided and config not found
|
33
|
+
- Removed deprecated options
|
34
|
+
- Dropped support for Postgres < 9.5
|
35
|
+
|
1
36
|
## 0.5.5 (2020-05-13)
|
2
37
|
|
3
38
|
- Added `--jobs` option
|
4
|
-
- Added
|
5
|
-
- Added
|
6
|
-
- Added
|
39
|
+
- Added `--defer-constraints` option
|
40
|
+
- Added `--disable-user-triggers` option
|
41
|
+
- Added `--disable-integrity` option
|
7
42
|
- Improved error message for older libpq
|
8
43
|
|
9
44
|
## 0.5.4 (2020-05-09)
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -33,20 +33,26 @@ This creates `.pgsync.yml` for you to customize. We recommend checking this into
|
|
33
33
|
|
34
34
|
## How to Use
|
35
35
|
|
36
|
-
|
36
|
+
First, make sure your schema is set up in both databases. We recommend using a schema migration tool for this, but pgsync also provides a few [convenience methods](#schema). Once that’s done, you’re ready to sync data.
|
37
|
+
|
38
|
+
Sync tables
|
37
39
|
|
38
40
|
```sh
|
39
41
|
pgsync
|
40
42
|
```
|
41
43
|
|
42
|
-
**Note:** pgsync assumes your schema is setup in your `to` database. See the [schema section](#schema) if that’s not the case.
|
43
|
-
|
44
44
|
Sync specific tables
|
45
45
|
|
46
46
|
```sh
|
47
47
|
pgsync table1,table2
|
48
48
|
```
|
49
49
|
|
50
|
+
Works with wildcards as well
|
51
|
+
|
52
|
+
```sh
|
53
|
+
pgsync "table*"
|
54
|
+
```
|
55
|
+
|
50
56
|
Sync specific rows (existing rows are overwritten)
|
51
57
|
|
52
58
|
```sh
|
@@ -65,13 +71,15 @@ Or truncate them
|
|
65
71
|
pgsync products "where store_id = 1" --truncate
|
66
72
|
```
|
67
73
|
|
68
|
-
##
|
74
|
+
## Tables
|
75
|
+
|
76
|
+
Exclude specific tables
|
69
77
|
|
70
78
|
```sh
|
71
|
-
pgsync --exclude
|
79
|
+
pgsync --exclude table1,table2
|
72
80
|
```
|
73
81
|
|
74
|
-
|
82
|
+
Add to `.pgsync.yml` to exclude by default
|
75
83
|
|
76
84
|
```yml
|
77
85
|
exclude:
|
@@ -79,12 +87,14 @@ exclude:
|
|
79
87
|
- table2
|
80
88
|
```
|
81
89
|
|
82
|
-
|
90
|
+
Sync tables from all schemas or specific schemas (by default, only the search path is synced)
|
83
91
|
|
84
|
-
```
|
85
|
-
|
86
|
-
|
87
|
-
|
92
|
+
```sh
|
93
|
+
pgsync --all-schemas
|
94
|
+
# or
|
95
|
+
pgsync --schemas public,other
|
96
|
+
# or
|
97
|
+
pgsync public.table1,other.table2
|
88
98
|
```
|
89
99
|
|
90
100
|
## Groups
|
@@ -104,6 +114,8 @@ And run:
|
|
104
114
|
pgsync group1
|
105
115
|
```
|
106
116
|
|
117
|
+
## Variables
|
118
|
+
|
107
119
|
You can also use groups to sync a specific record and associated records in other tables.
|
108
120
|
|
109
121
|
To get product `123` with its reviews, last 10 coupons, and store, use:
|
@@ -125,16 +137,12 @@ pgsync product:123
|
|
125
137
|
|
126
138
|
## Schema
|
127
139
|
|
128
|
-
|
129
|
-
|
130
|
-
Sync schema before the data
|
140
|
+
Sync schema before the data (this wipes out existing data)
|
131
141
|
|
132
142
|
```sh
|
133
143
|
pgsync --schema-first
|
134
144
|
```
|
135
145
|
|
136
|
-
**Note:** This wipes out existing data
|
137
|
-
|
138
146
|
Specify tables
|
139
147
|
|
140
148
|
```sh
|
@@ -149,9 +157,9 @@ pgsync --schema-only
|
|
149
157
|
|
150
158
|
pgsync does not try to sync Postgres extensions.
|
151
159
|
|
152
|
-
## Sensitive
|
160
|
+
## Sensitive Data
|
153
161
|
|
154
|
-
Prevent sensitive
|
162
|
+
Prevent sensitive data like email addresses from leaving the remote server.
|
155
163
|
|
156
164
|
Define rules in `.pgsync.yml`:
|
157
165
|
|
@@ -184,37 +192,35 @@ Options for replacement are:
|
|
184
192
|
- `null`
|
185
193
|
- `untouched`
|
186
194
|
|
187
|
-
Rules starting with `unique_` require the table to have a primary key. `unique_phone` requires a numeric primary key.
|
195
|
+
Rules starting with `unique_` require the table to have a single column primary key. `unique_phone` requires a numeric primary key.
|
188
196
|
|
189
197
|
## Foreign Keys
|
190
198
|
|
191
199
|
Foreign keys can make it difficult to sync data. Three options are:
|
192
200
|
|
193
|
-
1.
|
194
|
-
2.
|
195
|
-
3. Disable 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)
|
196
204
|
|
197
|
-
|
205
|
+
To defer constraints, use:
|
198
206
|
|
199
207
|
```sh
|
200
|
-
pgsync
|
208
|
+
pgsync --defer-constraints-v2
|
201
209
|
```
|
202
210
|
|
203
|
-
|
211
|
+
To manually specify the order of tables, use `--jobs 1` so tables are synced one-at-a-time.
|
204
212
|
|
205
213
|
```sh
|
206
|
-
pgsync --
|
214
|
+
pgsync table1,table2,table3 --jobs 1
|
207
215
|
```
|
208
216
|
|
209
|
-
|
210
|
-
|
211
|
-
To disable triggers and potentially break referential integrity, use:
|
217
|
+
To disable foreign key triggers and potentially break referential integrity, use:
|
212
218
|
|
213
219
|
```sh
|
214
220
|
pgsync --disable-integrity
|
215
221
|
```
|
216
222
|
|
217
|
-
|
223
|
+
This requires superuser privileges on the `to` database. If syncing to (not from) Amazon RDS, use the `rds_superuser` role. If syncing to (not from) Heroku, there doesn’t appear to be a way to disable integrity.
|
218
224
|
|
219
225
|
## Triggers
|
220
226
|
|
@@ -224,8 +230,6 @@ Disable user triggers with:
|
|
224
230
|
pgsync --disable-user-triggers
|
225
231
|
```
|
226
232
|
|
227
|
-
**Note:** This feature is currently experimental.
|
228
|
-
|
229
233
|
## Append-Only Tables
|
230
234
|
|
231
235
|
For extremely large, append-only tables, sync in batches.
|
@@ -260,6 +264,57 @@ This creates `.pgsync-db2.yml` for you to edit. Specify a database in commands w
|
|
260
264
|
pgsync --db db2
|
261
265
|
```
|
262
266
|
|
267
|
+
## Integrations
|
268
|
+
|
269
|
+
- [Django](#django)
|
270
|
+
- [Heroku](#heroku)
|
271
|
+
- [Laravel](#laravel)
|
272
|
+
- [Rails](#rails)
|
273
|
+
|
274
|
+
### Django
|
275
|
+
|
276
|
+
If you run `pgsync --init` in a Django project, migrations will be excluded in `.pgsync.yml`.
|
277
|
+
|
278
|
+
```yml
|
279
|
+
exclude:
|
280
|
+
- django_migrations
|
281
|
+
```
|
282
|
+
|
283
|
+
### Heroku
|
284
|
+
|
285
|
+
If you run `pgsync --init` in a Heroku project, the `from` database will be set in `.pgsync.yml`.
|
286
|
+
|
287
|
+
```yml
|
288
|
+
from: $(heroku config:get DATABASE_URL)?sslmode=require
|
289
|
+
```
|
290
|
+
|
291
|
+
### Laravel
|
292
|
+
|
293
|
+
If you run `pgsync --init` in a Laravel project, migrations will be excluded in `.pgsync.yml`.
|
294
|
+
|
295
|
+
```yml
|
296
|
+
exclude:
|
297
|
+
- migrations
|
298
|
+
```
|
299
|
+
|
300
|
+
### Rails
|
301
|
+
|
302
|
+
If you run `pgsync --init` in a Rails project, Active Record metadata and schema migrations will be excluded in `.pgsync.yml`.
|
303
|
+
|
304
|
+
```yml
|
305
|
+
exclude:
|
306
|
+
- ar_internal_metadata
|
307
|
+
- schema_migrations
|
308
|
+
```
|
309
|
+
|
310
|
+
## Debugging
|
311
|
+
|
312
|
+
To view the SQL that’s run, use:
|
313
|
+
|
314
|
+
```sh
|
315
|
+
pgsync --debug
|
316
|
+
```
|
317
|
+
|
263
318
|
## Other Commands
|
264
319
|
|
265
320
|
Help
|
data/config.yml
CHANGED
data/lib/pgsync.rb
CHANGED
@@ -10,15 +10,20 @@ require "shellwords"
|
|
10
10
|
require "tempfile"
|
11
11
|
require "uri"
|
12
12
|
require "yaml"
|
13
|
+
require "open3"
|
13
14
|
|
14
15
|
# modules
|
15
16
|
require "pgsync/utils"
|
16
17
|
require "pgsync/client"
|
17
18
|
require "pgsync/data_source"
|
18
19
|
require "pgsync/init"
|
20
|
+
require "pgsync/schema_sync"
|
21
|
+
require "pgsync/sequence"
|
19
22
|
require "pgsync/sync"
|
20
|
-
require "pgsync/
|
23
|
+
require "pgsync/table"
|
21
24
|
require "pgsync/table_sync"
|
25
|
+
require "pgsync/task"
|
26
|
+
require "pgsync/task_resolver"
|
22
27
|
require "pgsync/version"
|
23
28
|
|
24
29
|
module PgSync
|
data/lib/pgsync/client.rb
CHANGED
@@ -7,77 +7,75 @@ module PgSync
|
|
7
7
|
output.sync = true
|
8
8
|
end
|
9
9
|
|
10
|
-
def perform
|
11
|
-
|
10
|
+
def perform
|
11
|
+
result = Slop::Parser.new(slop_options).parse(@args)
|
12
|
+
arguments = result.arguments
|
13
|
+
options = result.to_h
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
+
raise Error, "Specify either --db or --config, not both" if options[:db] && options[:config]
|
16
|
+
raise Error, "Cannot use --overwrite with --in-batches" if options[:overwrite] && options[:in_batches]
|
15
17
|
|
16
|
-
if
|
18
|
+
if options[:version]
|
17
19
|
log VERSION
|
18
|
-
elsif
|
19
|
-
log
|
20
|
-
|
21
|
-
|
22
|
-
Init.new.perform(opts)
|
20
|
+
elsif options[:help]
|
21
|
+
log slop_options
|
22
|
+
elsif options[:init]
|
23
|
+
Init.new(arguments, options).perform
|
23
24
|
else
|
24
|
-
Sync.new.perform
|
25
|
+
Sync.new(arguments, options).perform
|
25
26
|
end
|
26
|
-
rescue
|
27
|
-
|
28
|
-
|
27
|
+
rescue => e
|
28
|
+
# Error, PG::ConnectionBad, Slop::Error
|
29
|
+
raise e if options && options[:debug]
|
30
|
+
abort colorize(e.message.strip, :red)
|
29
31
|
end
|
30
32
|
|
31
33
|
def self.start
|
32
|
-
new(ARGV).perform
|
34
|
+
new(ARGV).perform
|
33
35
|
end
|
34
36
|
|
35
37
|
protected
|
36
38
|
|
37
|
-
def
|
38
|
-
Slop.
|
39
|
-
|
40
|
-
|
39
|
+
def slop_options
|
40
|
+
o = Slop::Options.new
|
41
|
+
o.banner = %{Usage:
|
42
|
+
pgsync [options]
|
41
43
|
|
42
44
|
Options:}
|
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
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
o.boolean "-h", "--help", "prints help"
|
78
|
-
end
|
79
|
-
rescue Slop::Error => e
|
80
|
-
raise Error, e.message
|
45
|
+
o.string "-d", "--db", "database"
|
46
|
+
o.string "-t", "--tables", "tables to sync"
|
47
|
+
o.string "-g", "--groups", "groups to sync"
|
48
|
+
o.integer "-j", "--jobs", "number of tables to sync at a time"
|
49
|
+
o.string "--schemas", "schemas to sync"
|
50
|
+
o.string "--from", "source"
|
51
|
+
o.string "--to", "destination"
|
52
|
+
o.string "--exclude", "exclude tables"
|
53
|
+
o.string "--config", "config file"
|
54
|
+
o.boolean "--to-safe", "accept danger", default: false
|
55
|
+
o.boolean "--debug", "debug", default: false
|
56
|
+
o.boolean "--list", "list", default: false
|
57
|
+
o.boolean "--overwrite", "overwrite existing rows", default: false, help: false
|
58
|
+
o.boolean "--preserve", "preserve existing rows", default: false
|
59
|
+
o.boolean "--truncate", "truncate existing rows", default: false
|
60
|
+
o.boolean "--schema-first", "schema first", default: false
|
61
|
+
o.boolean "--schema-only", "schema only", default: false
|
62
|
+
o.boolean "--all-schemas", "all schemas", default: false
|
63
|
+
o.boolean "--no-rules", "do not apply data rules", default: false
|
64
|
+
o.boolean "--no-sequences", "do not sync sequences", default: false
|
65
|
+
o.boolean "--init", "init", default: false
|
66
|
+
o.boolean "--in-batches", "in batches", default: false, help: false
|
67
|
+
o.integer "--batch-size", "batch size", default: 10000, help: false
|
68
|
+
o.float "--sleep", "sleep", default: 0, help: false
|
69
|
+
o.boolean "--fail-fast", "stop on the first failed table", default: false
|
70
|
+
o.boolean "--defer-constraints", "defer constraints", default: false, help: false
|
71
|
+
o.boolean "--defer-constraints-v2", "defer constraints", default: false
|
72
|
+
o.boolean "--disable-user-triggers", "disable non-system triggers", default: false
|
73
|
+
o.boolean "--disable-integrity", "disable foreign key triggers", default: false
|
74
|
+
# private, for testing
|
75
|
+
o.boolean "--disable-integrity-v2", "disable foreign key triggers", default: false, help: false
|
76
|
+
o.boolean "-v", "--version", "print the version"
|
77
|
+
o.boolean "-h", "--help", "prints help"
|
78
|
+
o
|
81
79
|
end
|
82
80
|
end
|
83
81
|
end
|