table_saw 2.10.0 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +5 -3
- data/.rubocop.yml +4 -1
- data/.tool-versions +1 -1
- data/Appraisals +9 -4
- data/Gemfile +9 -0
- data/Gemfile.lock +133 -86
- data/README.md +158 -22
- data/gemfiles/activerecord_6.1.0.gemfile +9 -0
- data/gemfiles/activerecord_6.1.0.gemfile.lock +89 -78
- data/gemfiles/activerecord_7.0.0.gemfile +17 -0
- data/gemfiles/activerecord_7.0.0.gemfile.lock +169 -0
- data/gemfiles/activerecord_7.1.0.gemfile +17 -0
- data/gemfiles/activerecord_7.1.0.gemfile.lock +204 -0
- data/lib/table_saw/create_dump_file.rb +1 -1
- data/lib/table_saw/foreign_key.rb +1 -1
- data/lib/table_saw/formats/copy.rb +1 -5
- data/lib/table_saw/manifest.rb +1 -1
- data/lib/table_saw/queries/execute_insert_statement.rb +4 -4
- data/lib/table_saw/queries/prepared_insert_statement.rb +1 -1
- data/lib/table_saw/variable_interpolation.rb +37 -0
- data/lib/table_saw/version.rb +1 -1
- data/lib/table_saw.rb +1 -0
- data/table_saw.gemspec +3 -12
- metadata +13 -137
- data/.ruby-gemset +0 -1
- data/.ruby-version +0 -1
- data/gemfiles/activerecord_6.0.0.gemfile +0 -8
- data/gemfiles/activerecord_6.0.0.gemfile.lock +0 -159
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c486b7b157b263797373c6fcd9346e99e1e8d3d0d42bfd993b309b5f4a96032d
|
4
|
+
data.tar.gz: f6b3ee4493ce2c5b740410db3b5c18e847ab2d7bd663eb0094a5420fd2d83c9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e86bc39752b2ca0a7bd7b7bf7b162a1c90583ac8e43d6c0299fff36bdd106a446b35a45c1e0a08dc7ec4fc730387de569ef830596e59744920e66224c820835
|
7
|
+
data.tar.gz: 2350f1710a38280d71ac8587784dbef023685c40dc5d3c87af840bae94761127c23d0efba7da3efbe57ade1ebd1ea9827ba8d94fea6a8c0d4a08e1ca360d6955
|
data/.github/workflows/ruby.yml
CHANGED
@@ -10,11 +10,13 @@ jobs:
|
|
10
10
|
strategy:
|
11
11
|
matrix:
|
12
12
|
ruby:
|
13
|
-
- '
|
14
|
-
- '3.
|
13
|
+
- '3.0.5'
|
14
|
+
- '3.1.4'
|
15
|
+
- '3.2.2'
|
15
16
|
activerecord:
|
16
|
-
- '6.0.0'
|
17
17
|
- '6.1.0'
|
18
|
+
- '7.0.0'
|
19
|
+
- '7.1.0'
|
18
20
|
|
19
21
|
env:
|
20
22
|
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/activerecord_${{ matrix.activerecord }}.gemfile
|
data/.rubocop.yml
CHANGED
@@ -2,7 +2,7 @@ require: rubocop-rspec
|
|
2
2
|
|
3
3
|
AllCops:
|
4
4
|
NewCops: enable
|
5
|
-
TargetRubyVersion:
|
5
|
+
TargetRubyVersion: 3.0
|
6
6
|
Exclude:
|
7
7
|
- gemfiles/*
|
8
8
|
|
@@ -34,6 +34,9 @@ Style/EmptyMethod:
|
|
34
34
|
Style/ExponentialNotation:
|
35
35
|
Enabled: true
|
36
36
|
|
37
|
+
Style/FetchEnvVar:
|
38
|
+
Enabled: false
|
39
|
+
|
37
40
|
Style/FormatStringToken:
|
38
41
|
EnforcedStyle: template
|
39
42
|
|
data/.tool-versions
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby 3.
|
1
|
+
ruby 3.2.2
|
data/Appraisals
CHANGED
@@ -1,9 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
appraise 'activerecord-6.0.0' do
|
4
|
-
gem 'activerecord', '~> 6.0', '< 6.1'
|
5
|
-
end
|
6
|
-
|
7
3
|
appraise 'activerecord-6.1.0' do
|
8
4
|
gem 'activerecord', '~> 6.1', '< 6.2'
|
9
5
|
end
|
6
|
+
|
7
|
+
appraise 'activerecord-7.0.0' do
|
8
|
+
gem 'activerecord', '~> 7.0', '< 7.1'
|
9
|
+
end
|
10
|
+
|
11
|
+
appraise 'activerecord-7.1.0' do
|
12
|
+
gem 'activerecord', '~> 7.1', '< 7.2'
|
13
|
+
end
|
14
|
+
|
data/Gemfile
CHANGED
@@ -6,3 +6,12 @@ source 'https://rubygems.org'
|
|
6
6
|
gemspec
|
7
7
|
|
8
8
|
gem 'appraisal'
|
9
|
+
gem 'bundler', '~> 2.0'
|
10
|
+
gem 'combustion', '~> 1.3'
|
11
|
+
gem 'database_cleaner', '~> 2'
|
12
|
+
gem 'pry'
|
13
|
+
gem 'rake', '13.0.3'
|
14
|
+
gem 'rspec', '~> 3.0'
|
15
|
+
gem 'rubocop-rspec', '~> 2.3'
|
16
|
+
gem 'scenic', '~> 1.5'
|
17
|
+
gem 'simplecov', '~> 0.16'
|
data/Gemfile.lock
CHANGED
@@ -1,139 +1,186 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
table_saw (
|
5
|
-
activerecord (>=
|
4
|
+
table_saw (3.1.0)
|
5
|
+
activerecord (>= 6.0)
|
6
6
|
pg
|
7
7
|
thor
|
8
8
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
|
-
actionpack (
|
13
|
-
actionview (=
|
14
|
-
activesupport (=
|
15
|
-
|
12
|
+
actionpack (7.1.2)
|
13
|
+
actionview (= 7.1.2)
|
14
|
+
activesupport (= 7.1.2)
|
15
|
+
nokogiri (>= 1.8.5)
|
16
|
+
racc
|
17
|
+
rack (>= 2.2.4)
|
18
|
+
rack-session (>= 1.0.1)
|
16
19
|
rack-test (>= 0.6.3)
|
17
|
-
rails-dom-testing (~> 2.
|
18
|
-
rails-html-sanitizer (~> 1.
|
19
|
-
actionview (
|
20
|
-
activesupport (=
|
20
|
+
rails-dom-testing (~> 2.2)
|
21
|
+
rails-html-sanitizer (~> 1.6)
|
22
|
+
actionview (7.1.2)
|
23
|
+
activesupport (= 7.1.2)
|
21
24
|
builder (~> 3.1)
|
22
|
-
erubi (~> 1.
|
23
|
-
rails-dom-testing (~> 2.
|
24
|
-
rails-html-sanitizer (~> 1.
|
25
|
-
activemodel (
|
26
|
-
activesupport (=
|
27
|
-
activerecord (
|
28
|
-
activemodel (=
|
29
|
-
activesupport (=
|
30
|
-
|
25
|
+
erubi (~> 1.11)
|
26
|
+
rails-dom-testing (~> 2.2)
|
27
|
+
rails-html-sanitizer (~> 1.6)
|
28
|
+
activemodel (7.1.2)
|
29
|
+
activesupport (= 7.1.2)
|
30
|
+
activerecord (7.1.2)
|
31
|
+
activemodel (= 7.1.2)
|
32
|
+
activesupport (= 7.1.2)
|
33
|
+
timeout (>= 0.4.0)
|
34
|
+
activesupport (7.1.2)
|
35
|
+
base64
|
36
|
+
bigdecimal
|
31
37
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
38
|
+
connection_pool (>= 2.2.5)
|
39
|
+
drb
|
32
40
|
i18n (>= 1.6, < 2)
|
33
41
|
minitest (>= 5.1)
|
42
|
+
mutex_m
|
34
43
|
tzinfo (~> 2.0)
|
35
|
-
|
36
|
-
appraisal (2.4.0)
|
44
|
+
appraisal (2.5.0)
|
37
45
|
bundler
|
38
46
|
rake
|
39
47
|
thor (>= 0.14.0)
|
40
48
|
ast (2.4.2)
|
49
|
+
base64 (0.2.0)
|
50
|
+
bigdecimal (3.1.5)
|
41
51
|
builder (3.2.4)
|
42
52
|
coderay (1.1.3)
|
43
|
-
combustion (1.3.
|
53
|
+
combustion (1.3.7)
|
44
54
|
activesupport (>= 3.0.0)
|
45
55
|
railties (>= 3.0.0)
|
46
56
|
thor (>= 0.14.6)
|
47
|
-
concurrent-ruby (1.
|
57
|
+
concurrent-ruby (1.2.2)
|
58
|
+
connection_pool (2.4.1)
|
48
59
|
crass (1.0.6)
|
49
|
-
database_cleaner (2.0.
|
50
|
-
database_cleaner-active_record (
|
51
|
-
database_cleaner-active_record (2.0
|
60
|
+
database_cleaner (2.0.2)
|
61
|
+
database_cleaner-active_record (>= 2, < 3)
|
62
|
+
database_cleaner-active_record (2.1.0)
|
52
63
|
activerecord (>= 5.a)
|
53
64
|
database_cleaner-core (~> 2.0.0)
|
54
65
|
database_cleaner-core (2.0.1)
|
55
|
-
diff-lcs (1.
|
66
|
+
diff-lcs (1.5.0)
|
56
67
|
docile (1.4.0)
|
57
|
-
|
58
|
-
|
68
|
+
drb (2.2.0)
|
69
|
+
ruby2_keywords
|
70
|
+
erubi (1.12.0)
|
71
|
+
i18n (1.14.1)
|
59
72
|
concurrent-ruby (~> 1.0)
|
60
|
-
|
73
|
+
io-console (0.7.1)
|
74
|
+
irb (1.11.0)
|
75
|
+
rdoc
|
76
|
+
reline (>= 0.3.8)
|
77
|
+
json (2.7.1)
|
78
|
+
language_server-protocol (3.17.0.3)
|
79
|
+
loofah (2.22.0)
|
61
80
|
crass (~> 1.0.2)
|
62
|
-
nokogiri (>= 1.
|
81
|
+
nokogiri (>= 1.12.0)
|
63
82
|
method_source (1.0.0)
|
64
|
-
mini_portile2 (2.
|
65
|
-
minitest (5.
|
66
|
-
|
67
|
-
|
83
|
+
mini_portile2 (2.8.5)
|
84
|
+
minitest (5.20.0)
|
85
|
+
mutex_m (0.2.0)
|
86
|
+
nokogiri (1.15.5)
|
87
|
+
mini_portile2 (~> 2.8.2)
|
68
88
|
racc (~> 1.4)
|
69
|
-
parallel (1.
|
70
|
-
parser (3.
|
89
|
+
parallel (1.24.0)
|
90
|
+
parser (3.2.2.4)
|
71
91
|
ast (~> 2.4.1)
|
72
|
-
|
73
|
-
|
92
|
+
racc
|
93
|
+
pg (1.5.4)
|
94
|
+
pry (0.14.2)
|
74
95
|
coderay (~> 1.1)
|
75
96
|
method_source (~> 1.0)
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
97
|
+
psych (5.1.2)
|
98
|
+
stringio
|
99
|
+
racc (1.7.3)
|
100
|
+
rack (3.0.8)
|
101
|
+
rack-session (2.0.0)
|
102
|
+
rack (>= 3.0.0)
|
103
|
+
rack-test (2.1.0)
|
104
|
+
rack (>= 1.3)
|
105
|
+
rackup (2.1.0)
|
106
|
+
rack (>= 3)
|
107
|
+
webrick (~> 1.8)
|
108
|
+
rails-dom-testing (2.2.0)
|
109
|
+
activesupport (>= 5.0.0)
|
110
|
+
minitest
|
82
111
|
nokogiri (>= 1.6)
|
83
|
-
rails-html-sanitizer (1.
|
84
|
-
loofah (~> 2.
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
112
|
+
rails-html-sanitizer (1.6.0)
|
113
|
+
loofah (~> 2.21)
|
114
|
+
nokogiri (~> 1.14)
|
115
|
+
railties (7.1.2)
|
116
|
+
actionpack (= 7.1.2)
|
117
|
+
activesupport (= 7.1.2)
|
118
|
+
irb
|
119
|
+
rackup (>= 1.0.0)
|
120
|
+
rake (>= 12.2)
|
121
|
+
thor (~> 1.0, >= 1.2.2)
|
122
|
+
zeitwerk (~> 2.6)
|
123
|
+
rainbow (3.1.1)
|
92
124
|
rake (13.0.3)
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
rspec
|
100
|
-
rspec-
|
101
|
-
|
125
|
+
rdoc (6.6.2)
|
126
|
+
psych (>= 4.0.0)
|
127
|
+
regexp_parser (2.8.3)
|
128
|
+
reline (0.4.1)
|
129
|
+
io-console (~> 0.5)
|
130
|
+
rexml (3.2.6)
|
131
|
+
rspec (3.12.0)
|
132
|
+
rspec-core (~> 3.12.0)
|
133
|
+
rspec-expectations (~> 3.12.0)
|
134
|
+
rspec-mocks (~> 3.12.0)
|
135
|
+
rspec-core (3.12.2)
|
136
|
+
rspec-support (~> 3.12.0)
|
137
|
+
rspec-expectations (3.12.3)
|
102
138
|
diff-lcs (>= 1.2.0, < 2.0)
|
103
|
-
rspec-support (~> 3.
|
104
|
-
rspec-mocks (3.
|
139
|
+
rspec-support (~> 3.12.0)
|
140
|
+
rspec-mocks (3.12.6)
|
105
141
|
diff-lcs (>= 1.2.0, < 2.0)
|
106
|
-
rspec-support (~> 3.
|
107
|
-
rspec-support (3.
|
108
|
-
rubocop (1.
|
142
|
+
rspec-support (~> 3.12.0)
|
143
|
+
rspec-support (3.12.1)
|
144
|
+
rubocop (1.59.0)
|
145
|
+
json (~> 2.3)
|
146
|
+
language_server-protocol (>= 3.17.0)
|
109
147
|
parallel (~> 1.10)
|
110
|
-
parser (>= 3.
|
148
|
+
parser (>= 3.2.2.4)
|
111
149
|
rainbow (>= 2.2.2, < 4.0)
|
112
150
|
regexp_parser (>= 1.8, < 3.0)
|
113
|
-
rexml
|
114
|
-
rubocop-ast (>= 1.
|
151
|
+
rexml (>= 3.2.5, < 4.0)
|
152
|
+
rubocop-ast (>= 1.30.0, < 2.0)
|
115
153
|
ruby-progressbar (~> 1.7)
|
116
|
-
unicode-display_width (>=
|
117
|
-
rubocop-ast (1.
|
118
|
-
parser (>= 3.
|
119
|
-
rubocop-
|
120
|
-
rubocop (~> 1.
|
121
|
-
|
122
|
-
|
123
|
-
|
154
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
155
|
+
rubocop-ast (1.30.0)
|
156
|
+
parser (>= 3.2.1.0)
|
157
|
+
rubocop-capybara (2.19.0)
|
158
|
+
rubocop (~> 1.41)
|
159
|
+
rubocop-factory_bot (2.24.0)
|
160
|
+
rubocop (~> 1.33)
|
161
|
+
rubocop-rspec (2.25.0)
|
162
|
+
rubocop (~> 1.40)
|
163
|
+
rubocop-capybara (~> 2.17)
|
164
|
+
rubocop-factory_bot (~> 2.22)
|
165
|
+
ruby-progressbar (1.13.0)
|
166
|
+
ruby2_keywords (0.0.5)
|
167
|
+
scenic (1.7.0)
|
124
168
|
activerecord (>= 4.0.0)
|
125
169
|
railties (>= 4.0.0)
|
126
|
-
simplecov (0.
|
170
|
+
simplecov (0.22.0)
|
127
171
|
docile (~> 1.1)
|
128
172
|
simplecov-html (~> 0.11)
|
129
173
|
simplecov_json_formatter (~> 0.1)
|
130
174
|
simplecov-html (0.12.3)
|
131
|
-
simplecov_json_formatter (0.1.
|
132
|
-
|
133
|
-
|
175
|
+
simplecov_json_formatter (0.1.4)
|
176
|
+
stringio (3.1.0)
|
177
|
+
thor (1.3.0)
|
178
|
+
timeout (0.4.1)
|
179
|
+
tzinfo (2.0.6)
|
134
180
|
concurrent-ruby (~> 1.0)
|
135
|
-
unicode-display_width (2.
|
136
|
-
|
181
|
+
unicode-display_width (2.5.0)
|
182
|
+
webrick (1.8.1)
|
183
|
+
zeitwerk (2.6.12)
|
137
184
|
|
138
185
|
PLATFORMS
|
139
186
|
ruby
|
@@ -152,4 +199,4 @@ DEPENDENCIES
|
|
152
199
|
table_saw!
|
153
200
|
|
154
201
|
BUNDLED WITH
|
155
|
-
2.
|
202
|
+
2.4.13
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
[![Maintainability](https://api.codeclimate.com/v1/badges/abd5b5451c764d3249f1/maintainability)](https://codeclimate.com/github/hasghari/table_saw/maintainability)
|
3
3
|
[![Test Coverage](https://api.codeclimate.com/v1/badges/abd5b5451c764d3249f1/test_coverage)](https://codeclimate.com/github/hasghari/table_saw/test_coverage)
|
4
4
|
|
5
|
-
#
|
5
|
+
# table-saw
|
6
6
|
|
7
7
|
This gem creates a PSQL dump file (data only) from a Postgres database by only dumping a subset of data defined by a
|
8
8
|
manifest file.
|
@@ -50,52 +50,188 @@ Options:
|
|
50
50
|
-o, [--output=OUTPUT] # Default value is 'output.dump'
|
51
51
|
```
|
52
52
|
|
53
|
-
|
53
|
+
### Manifest
|
54
|
+
|
55
|
+
The manifest is a YAML file that describes the dataset to be exported to a dump file. At the top level, the manifest
|
56
|
+
file supports 4 nodes:
|
57
|
+
|
58
|
+
- [variables](#variables)
|
59
|
+
- [tables](#tables)
|
60
|
+
- [has_many](#has_many)
|
61
|
+
- [foreign_keys](#foreign_keys)
|
62
|
+
|
63
|
+
The examples that follow assume you have a database set up as follows:
|
64
|
+
```sql
|
65
|
+
CREATE TABLE authors (
|
66
|
+
id bigint NOT NULL,
|
67
|
+
name character varying NOT NULL,
|
68
|
+
PRIMARY KEY (id)
|
69
|
+
);
|
70
|
+
|
71
|
+
CREATE TABLE books (
|
72
|
+
id bigint NOT NULL,
|
73
|
+
author_id bigint NOT NULL,
|
74
|
+
name character varying NOT NULL,
|
75
|
+
votes integer DEFAULT 0 NOT NULL,
|
76
|
+
PRIMARY KEY (id),
|
77
|
+
CONSTRAINT fk_author FOREIGN KEY (author_id) REFERENCES authors(id)
|
78
|
+
);
|
79
|
+
|
80
|
+
INSERT INTO authors (id, name) VALUES (1, 'Dan Brown');
|
81
|
+
INSERT INTO authors (id, name) VALUES (2, 'J. K. Rowling');
|
82
|
+
|
83
|
+
INSERT INTO books (id, author_id, name, votes) VALUES (1, 1, 'Angels and Demons', 10);
|
84
|
+
INSERT INTO books (id, author_id, name, votes) VALUES (2, 1, 'Digital Fortress', 35);
|
85
|
+
INSERT INTO books (id, author_id, name, votes) VALUES (3, 1, 'The Da Vinci Code', 50);
|
86
|
+
INSERT INTO books (id, author_id, name, votes) VALUES (4, 2, 'Philosopher''s Stone', 55);
|
87
|
+
INSERT INTO books (id, author_id, name, votes) VALUES (5, 2, 'Chamber of Secrets', 5);
|
88
|
+
INSERT INTO books (id, author_id, name, votes) VALUES (6, 2, 'Prisoner of Azkaban', 25);
|
89
|
+
```
|
90
|
+
|
91
|
+
#### variables
|
92
|
+
Variables allow you parameterize the queries in the manifest. You can use the `%{variable}` substitution pattern in your
|
93
|
+
query strings:
|
54
94
|
|
55
95
|
```yaml
|
56
96
|
variables:
|
57
|
-
author_id:
|
97
|
+
author_id: 2
|
58
98
|
tables:
|
59
99
|
- table: books
|
60
100
|
query: "select * from books where author_id = %{author_id}"
|
61
101
|
```
|
62
102
|
|
63
|
-
|
64
|
-
`authors` table where `id = 24`.
|
65
|
-
|
66
|
-
Assuming there is a `chapters` table with a foreign key reference of `book_id` to the `books` table, the above manifest
|
67
|
-
file will **not** automatically retrieve those records. If `chapters` records are also desired, there a couple ways to
|
68
|
-
accomplish this:
|
103
|
+
Additionally, you can now use the `%{variable}` substitution pattern in other variables:
|
69
104
|
|
70
105
|
```yaml
|
71
106
|
variables:
|
72
|
-
author_id:
|
107
|
+
author_id: '1,3,4',
|
108
|
+
book_ids: 'select * from books where author_id in (%{author_id})'
|
109
|
+
```
|
110
|
+
|
111
|
+
#### tables
|
112
|
+
This is where you list the specific tables that you want to export. If you only specify the `table` without providing a
|
113
|
+
`query`, then the **entire** table will be exported. However, if you specify a `query`, then only rows matching that
|
114
|
+
query will be exported:
|
115
|
+
|
116
|
+
```yaml
|
73
117
|
tables:
|
74
118
|
- table: books
|
75
|
-
query: "select * from books where author_id =
|
119
|
+
query: "select * from books where author_id = 2"
|
120
|
+
```
|
121
|
+
|
122
|
+
The above manifest will only export rows from the `books` table where `author_id = 2`. In addition, due to the
|
123
|
+
foreign key constraint defined from `books(author_id)` to `authors(id)`, table-saw will automatically export the row
|
124
|
+
from the `authors` table where `id = 2` in order to preserve referential integrity.
|
125
|
+
|
126
|
+
The above manifest can alternatively be written as follows where exactly the same rows would be exported:
|
127
|
+
|
128
|
+
```yaml
|
129
|
+
tables:
|
130
|
+
- table: authors
|
131
|
+
query: "select * from authors where id = 2"
|
132
|
+
has_many:
|
133
|
+
- books
|
134
|
+
```
|
135
|
+
|
136
|
+
Notice we have to explicitly list the `has_many` association to the `books` table. Since `authors` does not have a
|
137
|
+
dependency on `books` to preserve referential integrity, table-saw by design will not export the associated `books`
|
138
|
+
rows in order to keep the output dump file as small as possible. In other words, if we eliminate the `has_many` node
|
139
|
+
from the manifest above, table-saw will only export a single author with `id = 2`.
|
140
|
+
|
141
|
+
#### has_many
|
142
|
+
This is where we define which optional associations we want to export for each table:
|
143
|
+
|
144
|
+
```yaml
|
145
|
+
tables:
|
146
|
+
- table: authors
|
147
|
+
query: "select * from authors where id = 1"
|
148
|
+
- table: books
|
149
|
+
query: "select * from books where id = 6"
|
76
150
|
has_many:
|
77
|
-
|
78
|
-
-
|
151
|
+
authors:
|
152
|
+
- books
|
79
153
|
```
|
80
154
|
|
81
|
-
|
155
|
+
The above manifest would export the following rows:
|
156
|
+
|
157
|
+
* authors:
|
158
|
+
- `id = 1` because of the explicit query in the manifest
|
159
|
+
- `id = 2` because it is the `author_id` for `books` with `id = 6`
|
160
|
+
|
161
|
+
* books:
|
162
|
+
- `id = [1, 2, 3]` due to the `has_many` association for `authors` with `id = 1`
|
163
|
+
- `id = [4, 5, 6]` due to the `has_many` association for `authors` with `id = 2`
|
164
|
+
|
165
|
+
Now, if instead of defining the `has_many` node at the top level, we define it under the `authors` table as follows:
|
82
166
|
|
83
167
|
```yaml
|
84
|
-
variables:
|
85
|
-
author_id: 24
|
86
168
|
tables:
|
87
|
-
- table:
|
88
|
-
query: "select * from
|
169
|
+
- table: authors
|
170
|
+
query: "select * from authors where id = 1"
|
171
|
+
has_many:
|
172
|
+
- books
|
173
|
+
- table: books
|
174
|
+
query: "select * from books where id = 6"
|
175
|
+
```
|
176
|
+
|
177
|
+
The above manifest would export the following rows:
|
178
|
+
|
179
|
+
* authors:
|
180
|
+
- `id = 1` because of the explicit query in the manifest
|
181
|
+
- `id = 2` because it is the `author_id` for `books` with `id = 6`
|
182
|
+
|
183
|
+
* books:
|
184
|
+
- `id = [1, 2, 3]` due to the `has_many` association for `authors` with `id = 1`
|
185
|
+
- `id = 6` due to the explicit query in the manifest
|
186
|
+
|
187
|
+
One potential pitfall with using `has_many` is that you end up pulling in too many associated rows when all you wanted
|
188
|
+
was a limited number. table-saw allows you to specify a `scope` and `limit`:
|
189
|
+
|
190
|
+
```yaml
|
191
|
+
tables:
|
192
|
+
- table: authors
|
193
|
+
query: "select * from authors where id = 1"
|
194
|
+
- table: books
|
195
|
+
query: "select * from books where id = 6"
|
196
|
+
has_many:
|
197
|
+
authors:
|
198
|
+
- books:
|
199
|
+
scope: "votes > 30"
|
200
|
+
limit: 1
|
201
|
+
```
|
202
|
+
|
203
|
+
The above manifest would export the following rows:
|
204
|
+
|
205
|
+
* authors:
|
206
|
+
- `id = 1` because of the explicit query in the manifest
|
207
|
+
- `id = 2` because it is the `author_id` for `books` with `id = 6`
|
208
|
+
|
209
|
+
* books:
|
210
|
+
- Either `id = 2` or `id = 3` for `author_id = 1` since they both have `vote > 30` and the limit of 1 will randomly
|
211
|
+
choose one of them
|
212
|
+
- Only `id = 4` for `author_id = 2` since it's the only book for that author with `vote > 30`
|
213
|
+
|
214
|
+
#### foreign_keys
|
215
|
+
By default, table-saw will query the Postgres `information_schema` to look up the foreign key constraints and determine
|
216
|
+
whether it needs to export associated rows. However, if your database schema does not define foreign key constraints for
|
217
|
+
the tables you would like to export, you can manually define them in the manifest. Assuming we had not defined any
|
218
|
+
foreign key constraints for the `books` table, we could specify it in the manifest as follows:
|
219
|
+
|
220
|
+
```yaml
|
221
|
+
foreign_keys:
|
222
|
+
- from_table: books
|
223
|
+
from_column: author_id
|
224
|
+
to_table: authors
|
225
|
+
to_column: id
|
89
226
|
```
|
90
227
|
|
91
|
-
|
92
|
-
the `authors`, `books` and `chapters` tables.
|
228
|
+
## Dump and Restore
|
93
229
|
|
94
230
|
Once your dump file has been created, you can import the data using `psql`:
|
95
231
|
|
96
232
|
```bash
|
97
|
-
table-saw dump -m manifest.yml -o
|
98
|
-
psql -h localhost -U postgres -d library <
|
233
|
+
table-saw dump -m manifest.yml -o library.dump
|
234
|
+
psql -h localhost -U postgres -d library < library.dump
|
99
235
|
```
|
100
236
|
|
101
237
|
## Development
|
@@ -3,6 +3,15 @@
|
|
3
3
|
source "https://rubygems.org"
|
4
4
|
|
5
5
|
gem "appraisal"
|
6
|
+
gem "bundler", "~> 2.0"
|
7
|
+
gem "combustion", "~> 1.3"
|
8
|
+
gem "database_cleaner", "~> 2"
|
9
|
+
gem "pry"
|
10
|
+
gem "rake", "13.0.3"
|
11
|
+
gem "rspec", "~> 3.0"
|
12
|
+
gem "rubocop-rspec", "~> 2.3"
|
13
|
+
gem "scenic", "~> 1.5"
|
14
|
+
gem "simplecov", "~> 0.16"
|
6
15
|
gem "activerecord", "~> 6.1", "< 6.2"
|
7
16
|
|
8
17
|
gemspec path: "../"
|