table_saw 1.3.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce0e71ab653f2467be613e32219a81dbd1c8544b0ffe54dcd300075ef9d85d5f
4
- data.tar.gz: 6758d8eb4b87c61e99425abc727d2e311c84d809ab1ab365cc3ec1c1d44dddfb
3
+ metadata.gz: f87bd795a89acabf42bd2ff9cc75dd2035d024cc877d8cf47f45cf8cc96b3029
4
+ data.tar.gz: dce4ea88bed8c8cf92e68020f528721dbe43eebcfa9adb92f3e0669a90cba6f3
5
5
  SHA512:
6
- metadata.gz: 82c0db05a5e5d2cfcbd6141e78652e84380f65d960ab130b427ebe1da3caefc8f90b5b9f45621620d2d4a204f8b1fded43bc10978bbbec2f551442fddb5f0f4a
7
- data.tar.gz: 748b769670ad9b24abe6a84b402cbbb2e1325e030d180ae9461b299b78af2b1b23611fa06d1a281b4a9cfac43f9842685ff1276d458941200204f47c5107b1cf
6
+ metadata.gz: c9f263214ec9a1d04f7e1c795c7d91b8a476cd9776ce67d16e4bec86a1db93684c9183090ff7cf7926bf29fe13260abafb24f56c71c59d5500e991983f24db61
7
+ data.tar.gz: 2274e86e0cdc654b5635400910c532957db83dc7f7de14f3ee497f8e7fecf31fd7d6dea1c9ec12a9fc2eeb8794bf6e816de9499f37ae1acb4af95e38c4c91443
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- table_saw (1.3.0)
4
+ table_saw (2.0.0)
5
5
  pg
6
6
  thor
7
7
 
@@ -41,7 +41,7 @@ GEM
41
41
  railties (>= 3.0.0)
42
42
  thor (>= 0.14.6)
43
43
  concurrent-ruby (1.1.5)
44
- crass (1.0.4)
44
+ crass (1.0.5)
45
45
  database_cleaner (1.7.0)
46
46
  diff-lcs (1.3)
47
47
  docile (1.3.1)
@@ -50,13 +50,13 @@ GEM
50
50
  concurrent-ruby (~> 1.0)
51
51
  jaro_winkler (1.5.2)
52
52
  json (2.2.0)
53
- loofah (2.2.3)
53
+ loofah (2.3.1)
54
54
  crass (~> 1.0.2)
55
55
  nokogiri (>= 1.5.9)
56
56
  method_source (0.9.2)
57
57
  mini_portile2 (2.4.0)
58
58
  minitest (5.11.3)
59
- nokogiri (1.10.3)
59
+ nokogiri (1.10.5)
60
60
  mini_portile2 (~> 2.4.0)
61
61
  parallel (1.17.0)
62
62
  parser (2.6.3.0)
@@ -13,10 +13,11 @@ module TableSaw
13
13
  def call
14
14
  File.delete(file) if File.exist?(file)
15
15
 
16
+ alter_constraints_to_deferrable
17
+
16
18
  write_to_file <<~SQL
17
19
  BEGIN;
18
20
 
19
- SET session_replication_role = replica;
20
21
  SET statement_timeout = 0;
21
22
  SET lock_timeout = 0;
22
23
  SET client_encoding = 'UTF8';
@@ -28,6 +29,8 @@ module TableSaw
28
29
  SQL
29
30
 
30
31
  records.each do |name, table|
32
+ defer_constraints(name)
33
+
31
34
  write_to_file <<~COMMENT
32
35
  --
33
36
  -- Data for Name: #{name}; Type: TABLE DATA
@@ -60,6 +63,34 @@ module TableSaw
60
63
 
61
64
  private
62
65
 
66
+ def alter_constraints_to_deferrable
67
+ records.each_key do |name|
68
+ write_to_file <<~COMMENT
69
+ --
70
+ -- Alter Constraints for Name: #{name}; Type: DEFERRABLE
71
+ --
72
+
73
+ COMMENT
74
+
75
+ TableSaw.information_schema.constraint_names[name].each do |constraint_name|
76
+ write_to_file "ALTER TABLE #{name} ALTER CONSTRAINT #{constraint_name} DEFERRABLE;"
77
+ end
78
+ end
79
+ end
80
+
81
+ def defer_constraints(name)
82
+ write_to_file <<~COMMENT
83
+ --
84
+ -- Set Constraints for Name: #{name}; Type: DEFERRED
85
+ --
86
+
87
+ COMMENT
88
+
89
+ TableSaw.information_schema.constraint_names[name].each do |constraint_name|
90
+ write_to_file "SET CONSTRAINTS #{constraint_name} DEFERRED;"
91
+ end
92
+ end
93
+
63
94
  def refresh_materialized_views
64
95
  TableSaw::Queries::MaterializedViews.new.call.each do |view|
65
96
  write_to_file "refresh materialized view #{view};"
@@ -2,12 +2,10 @@
2
2
 
3
3
  module TableSaw
4
4
  class InformationSchema
5
- def belongs_to
6
- foreign_key_relationships.belongs_to
7
- end
8
-
9
- def has_many
10
- foreign_key_relationships.has_many
5
+ %i(belongs_to constraint_names has_many).each do |method_name|
6
+ define_method method_name do
7
+ foreign_key_relationships.public_send method_name
8
+ end
11
9
  end
12
10
 
13
11
  def primary_keys
@@ -5,6 +5,7 @@ module TableSaw
5
5
  class ForeignKeyRelationships
6
6
  QUERY = <<~SQL
7
7
  select
8
+ tc.constraint_name,
8
9
  tc.table_name as from_table,
9
10
  kcu.column_name as from_column,
10
11
  ccu.table_name as to_table,
@@ -27,6 +28,12 @@ module TableSaw
27
28
  end
28
29
  end
29
30
 
31
+ def constraint_names
32
+ @constraint_names ||= result.each_with_object(Hash.new { |h, k| h[k] = [] }) do |row, memo|
33
+ memo[row['from_table']].push(row['constraint_name'])
34
+ end
35
+ end
36
+
30
37
  private
31
38
 
32
39
  def result
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TableSaw
4
- VERSION = '1.3.0'
4
+ VERSION = '2.0.0'
5
5
  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: 1.3.0
4
+ version: 2.0.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-08-15 00:00:00.000000000 Z
11
+ date: 2019-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg