table_saw 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +2 -1
- data/Gemfile.lock +9 -1
- data/README.md +1 -0
- data/lib/table_saw/build_dependency_graph.rb +31 -25
- data/lib/table_saw/create_dump_file.rb +6 -6
- data/lib/table_saw/manifest.rb +0 -1
- data/lib/table_saw/queries/foreign_key_relationships.rb +1 -3
- data/lib/table_saw/version.rb +1 -1
- data/table_saw.gemspec +1 -0
- metadata +19 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50a0284603206abc2575bac6cbbe322d5b2fe846db944865946ca97d01e5ac0b
|
4
|
+
data.tar.gz: 0611b668edaacdbc0a8f69dcebaa9c33be46f02defac7f95c054e3b08490d12f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afb11fe237a477e0dd3b350f92cc22ac4beeb774ea44203df7051cc41fac55a8040b4714ebac471977e204f0069392eb1a89d43bd0e0af79b88b63e3e8a7c820
|
7
|
+
data.tar.gz: 25f021cfa55c5bf09aceab3cd61297697991585dd991c7b45c92d4c0e2961276190ca60895a280d1212ab2d3ea1877faf1f1fc2319e5e51789596652c10b33db
|
data/.gitignore
CHANGED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
table_saw
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.2
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
table_saw (0.
|
4
|
+
table_saw (0.2.0)
|
5
5
|
connection_pool
|
6
6
|
pg
|
7
7
|
thor
|
@@ -44,9 +44,11 @@ GEM
|
|
44
44
|
crass (1.0.4)
|
45
45
|
database_cleaner (1.7.0)
|
46
46
|
diff-lcs (1.3)
|
47
|
+
docile (1.3.1)
|
47
48
|
erubi (1.8.0)
|
48
49
|
i18n (1.6.0)
|
49
50
|
concurrent-ruby (~> 1.0)
|
51
|
+
json (2.2.0)
|
50
52
|
loofah (2.2.3)
|
51
53
|
crass (~> 1.0.2)
|
52
54
|
nokogiri (>= 1.5.9)
|
@@ -84,6 +86,11 @@ GEM
|
|
84
86
|
diff-lcs (>= 1.2.0, < 2.0)
|
85
87
|
rspec-support (~> 3.8.0)
|
86
88
|
rspec-support (3.8.0)
|
89
|
+
simplecov (0.16.1)
|
90
|
+
docile (~> 1.1)
|
91
|
+
json (>= 1.8, < 3)
|
92
|
+
simplecov-html (~> 0.10.0)
|
93
|
+
simplecov-html (0.10.2)
|
87
94
|
thor (0.19.4)
|
88
95
|
thread_safe (0.3.6)
|
89
96
|
tzinfo (1.2.5)
|
@@ -99,6 +106,7 @@ DEPENDENCIES
|
|
99
106
|
database_cleaner (~> 1.7)
|
100
107
|
rake (~> 10.0)
|
101
108
|
rspec (~> 3.0)
|
109
|
+
simplecov (~> 0.16)
|
102
110
|
table_saw!
|
103
111
|
|
104
112
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/hasghari/table_saw.svg?branch=master)](https://travis-ci.org/hasghari/table_saw)
|
1
2
|
# TableSaw
|
2
3
|
|
3
4
|
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/table_saw`. To experiment with that code, run `bin/console` for an interactive prompt.
|
@@ -10,51 +10,57 @@ module TableSaw
|
|
10
10
|
|
11
11
|
def initialize(manifest)
|
12
12
|
@manifest = manifest
|
13
|
-
@records = Hash.new { |h, k| h[k] =
|
13
|
+
@records = Hash.new { |h, k| h[k] = [] }
|
14
14
|
end
|
15
15
|
|
16
16
|
def call
|
17
17
|
manifest.tables.each_value do |table|
|
18
|
-
perform_query(table.query).
|
19
|
-
add(table.name, row['id'])
|
20
|
-
end
|
18
|
+
add(table.name, perform_query(table.query).map { |row| row['id'] })
|
21
19
|
end
|
22
20
|
|
23
21
|
records
|
24
22
|
end
|
25
23
|
|
26
|
-
def add(table_name,
|
27
|
-
return if
|
24
|
+
def add(table_name, ids)
|
25
|
+
return if ids.empty?
|
28
26
|
|
29
|
-
records[table_name]
|
30
|
-
|
31
|
-
|
27
|
+
ids_to_add = ids - records[table_name]
|
28
|
+
return if ids_to_add.empty?
|
29
|
+
|
30
|
+
records[table_name].concat ids_to_add
|
31
|
+
fetch_belongs_to_associations(table_name, ids_to_add)
|
32
|
+
fetch_has_many_associations(table_name, ids_to_add)
|
32
33
|
end
|
33
34
|
|
34
|
-
|
35
|
+
# rubocop:disable Metrics/AbcSize
|
36
|
+
def fetch_belongs_to_associations(table_name, ids)
|
35
37
|
associations = belongs_to[table_name]
|
36
|
-
return if associations.
|
37
|
-
|
38
|
-
perform_query(
|
39
|
-
format('select %{columns} from %{table_name} where id
|
40
|
-
columns: associations.keys.join(','), table_name: table_name,
|
41
|
-
)
|
42
|
-
|
43
|
-
|
44
|
-
|
38
|
+
return if associations.empty?
|
39
|
+
|
40
|
+
rows = perform_query(
|
41
|
+
format('select %{columns} from %{table_name} where id in (%{ids})',
|
42
|
+
columns: associations.keys.join(','), table_name: table_name, ids: ids.join(','))
|
43
|
+
)
|
44
|
+
|
45
|
+
values = rows.each_with_object(Hash.new { |h, k| h[k] = Set.new }) do |row, memo|
|
46
|
+
associations.each_key { |key| memo[key].add row[key] unless row[key].nil? }
|
45
47
|
end
|
48
|
+
|
49
|
+
associations.each { |from_column, to_table| add to_table, values[from_column].to_a }
|
46
50
|
end
|
51
|
+
# rubocop:enable Metrics/AbcSize
|
47
52
|
|
48
|
-
def fetch_has_many_associations(table_name,
|
53
|
+
def fetch_has_many_associations(table_name, ids)
|
49
54
|
has_many.fetch(table_name, []).each do |table, column|
|
50
55
|
next if tables_with_no_ids.include?(table)
|
51
56
|
next unless has_many_mapping.fetch(table_name, []).include?(table)
|
52
57
|
|
53
|
-
perform_query(
|
54
|
-
format('select id from %{table} where %{column}
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
+
rows = perform_query(
|
59
|
+
format('select id from %{table} where %{column} in (%{ids})',
|
60
|
+
table: table, column: column, ids: ids.join(','))
|
61
|
+
)
|
62
|
+
|
63
|
+
add(table, rows.map { |row| row['id'] })
|
58
64
|
end
|
59
65
|
end
|
60
66
|
|
@@ -11,7 +11,7 @@ module TableSaw
|
|
11
11
|
@file = file
|
12
12
|
end
|
13
13
|
|
14
|
-
# rubocop:disable Metrics/MethodLength
|
14
|
+
# rubocop:disable Metrics/MethodLength
|
15
15
|
def call
|
16
16
|
write_to_file <<~SQL
|
17
17
|
BEGIN;
|
@@ -40,20 +40,20 @@ module TableSaw
|
|
40
40
|
SQL
|
41
41
|
|
42
42
|
TableSaw::Connection.with do |conn|
|
43
|
-
conn.copy_data "COPY (select * from #{name} where id in (#{ids.
|
43
|
+
conn.copy_data "COPY (select * from #{name} where id in (#{ids.join(',')})) TO STDOUT" do
|
44
44
|
while (row = conn.get_copy_data)
|
45
|
-
write_to_file
|
45
|
+
write_to_file row
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
write_to_file
|
51
|
-
write_to_file
|
50
|
+
write_to_file '\.'
|
51
|
+
write_to_file "\n"
|
52
52
|
end
|
53
53
|
|
54
54
|
write_to_file 'COMMIT;'
|
55
55
|
end
|
56
|
-
# rubocop:enable Metrics/MethodLength
|
56
|
+
# rubocop:enable Metrics/MethodLength
|
57
57
|
|
58
58
|
private
|
59
59
|
|
data/lib/table_saw/manifest.rb
CHANGED
@@ -7,9 +7,7 @@ module TableSaw
|
|
7
7
|
class ForeignKeyRelationships
|
8
8
|
QUERY = <<~SQL
|
9
9
|
select
|
10
|
-
tc.constraint_name,
|
11
10
|
tc.table_name as from_table,
|
12
|
-
tc.constraint_type,
|
13
11
|
kcu.column_name as from_column,
|
14
12
|
ccu.table_name as to_table,
|
15
13
|
ccu.column_name as to_column
|
@@ -20,7 +18,7 @@ module TableSaw
|
|
20
18
|
SQL
|
21
19
|
|
22
20
|
def belongs_to
|
23
|
-
result.each_with_object(Hash.new { |h, k| h[k] = {} }) do |row, memo|
|
21
|
+
@belongs_to ||= result.each_with_object(Hash.new { |h, k| h[k] = {} }) do |row, memo|
|
24
22
|
memo[row['from_table']][row['from_column']] = row['to_table']
|
25
23
|
end
|
26
24
|
end
|
data/lib/table_saw/version.rb
CHANGED
data/table_saw.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: table_saw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hamed Asghari
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: connection_pool
|
@@ -136,6 +136,20 @@ dependencies:
|
|
136
136
|
- - "~>"
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '3.0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: simplecov
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0.16'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0.16'
|
139
153
|
description:
|
140
154
|
email:
|
141
155
|
- hasghari@gmail.com
|
@@ -147,6 +161,8 @@ files:
|
|
147
161
|
- ".gitignore"
|
148
162
|
- ".rspec"
|
149
163
|
- ".rubocop.yml"
|
164
|
+
- ".ruby-gemset"
|
165
|
+
- ".ruby-version"
|
150
166
|
- ".travis.yml"
|
151
167
|
- CODE_OF_CONDUCT.md
|
152
168
|
- Gemfile
|
@@ -187,8 +203,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
187
203
|
- !ruby/object:Gem::Version
|
188
204
|
version: '0'
|
189
205
|
requirements: []
|
190
|
-
|
191
|
-
rubygems_version: 2.7.7
|
206
|
+
rubygems_version: 3.0.3
|
192
207
|
signing_key:
|
193
208
|
specification_version: 4
|
194
209
|
summary: Create a postgres dump file from a subset of tables
|