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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 023e631a9a6fcad3f3ea21aada1e8cc8431414a0e3b94b4d550e74e2ece0bfcf
4
- data.tar.gz: e24b92a73712e5e23293c3d1d7386469ffe63efbd80a6090a9c8ddd7df38ac8c
3
+ metadata.gz: 50a0284603206abc2575bac6cbbe322d5b2fe846db944865946ca97d01e5ac0b
4
+ data.tar.gz: 0611b668edaacdbc0a8f69dcebaa9c33be46f02defac7f95c054e3b08490d12f
5
5
  SHA512:
6
- metadata.gz: 3c91ce9ff2760cb5594e301044a150168761255aac47e6790e01b91fdd8dd1d0797d1415e46391bb12645f9b3bce20bffb0b3c7684b67678cb59d9251a62f91e
7
- data.tar.gz: 19d3d436ab6dce541123d7fabed4f8a9e05516e1277bf0859e7944e867e7e6120fdea91af34bb5815f8591d3017156d79a460db38f64de7061d9544e4151f34e
6
+ metadata.gz: afb11fe237a477e0dd3b350f92cc22ac4beeb774ea44203df7051cc41fac55a8040b4714ebac471977e204f0069392eb1a89d43bd0e0af79b88b63e3e8a7c820
7
+ data.tar.gz: 25f021cfa55c5bf09aceab3cd61297697991585dd991c7b45c92d4c0e2961276190ca60895a280d1212ab2d3ea1877faf1f1fc2319e5e51789596652c10b33db
data/.gitignore CHANGED
@@ -7,6 +7,7 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
  /.idea
10
+ *.log
10
11
 
11
12
  # rspec failure tracking
12
13
  .rspec_status
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
@@ -3,5 +3,6 @@ sudo: false
3
3
  language: ruby
4
4
  cache: bundler
5
5
  rvm:
6
- - 2.5.1
6
+ - 2.5.5
7
+ - 2.6.3
7
8
  before_install: gem install bundler -v 2.0.1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- table_saw (0.1.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] = Set.new }
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).each do |row|
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, id)
27
- return if id.nil? || records[table_name].include?(id)
24
+ def add(table_name, ids)
25
+ return if ids.empty?
28
26
 
29
- records[table_name].add id
30
- fetch_belongs_to_associations(table_name, id)
31
- fetch_has_many_associations(table_name, id)
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
- def fetch_belongs_to_associations(table_name, id)
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.nil? || associations.empty?
37
-
38
- perform_query(
39
- format('select %{columns} from %{table_name} where id = %{id}',
40
- columns: associations.keys.join(','), table_name: table_name, id: id)
41
- ).each do |row|
42
- associations.each do |from_column, to_table|
43
- add(to_table, row[from_column])
44
- end
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, id)
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} = %{id}', table: table, column: column, id: id)
55
- ).each do |row|
56
- add(table, row['id'])
57
- end
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,Metrics/AbcSize
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.to_a.join(',')})) TO STDOUT" do
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(row)
45
+ write_to_file row
46
46
  end
47
47
  end
48
48
  end
49
49
 
50
- write_to_file('\.')
51
- write_to_file("\n")
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,Metrics/AbcSize
56
+ # rubocop:enable Metrics/MethodLength
57
57
 
58
58
  private
59
59
 
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'yaml'
4
- require 'table_saw/connection'
5
4
 
6
5
  module TableSaw
7
6
  class Manifest
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TableSaw
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
data/table_saw.gemspec CHANGED
@@ -33,4 +33,5 @@ Gem::Specification.new do |spec|
33
33
  spec.add_development_dependency 'database_cleaner', '~> 1.7'
34
34
  spec.add_development_dependency 'rake', '~> 10.0'
35
35
  spec.add_development_dependency 'rspec', '~> 3.0'
36
+ spec.add_development_dependency 'simplecov', '~> 0.16'
36
37
  end
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.1.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-08 00:00:00.000000000 Z
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
- rubyforge_project:
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