pg_graph 0.4.1 → 0.5.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: 9c276d6c75a8880b77b66f8d40346511069a635088070835903b36ae2b000401
4
- data.tar.gz: fc96bb277f909cc6da83566b2151d6a30628cb143e3d2cd9ecc7af1d800dd31d
3
+ metadata.gz: 123eb2c8963ee6c45420315db19a876e76e5322ca7069d74e3d50fa4792aab58
4
+ data.tar.gz: 5be39db46db7775b25a40b02c76c65cedb8bf365128d5c3b602c1e3ca9003552
5
5
  SHA512:
6
- metadata.gz: 6b5a9ef6de526ff63c3930677822eb244e7bc486fe95d4db340592ea363c91427410cd1449cb46f8f99635aa3cff6886b9b30826dcea8163a63c7d36fbad4eaa
7
- data.tar.gz: eae096de222265d3d229bbc755356781bf1c7027cfdd54eec382f08c98c390bb6fe5d43ee3e47df17ef949b5b4c4d4b3e33241ebc3441f882ddcd6129f1bc82c
6
+ metadata.gz: d1c6a0c25c6980031763409d6357df388db14002c6c8c49b6eb92b93ac9cf180d9cfe7eb0789b622e5f2fc078f08a603b33ccb433947d0bd5c308bf16c204a0a
7
+ data.tar.gz: b43cfb4089d4fcffa32f62019c256065b9f3b00c1dddb47edb3c4ca490a68dfb1495dd68fd7204e3d42051aadeca7b737652a129ef6d3bdfd84ff47a2dafa44d
data/TODO CHANGED
@@ -1,4 +1,5 @@
1
1
 
2
+ o Fix that tables are not deleted in dependency order
2
3
  o Propagate super table relations to sub-tables
3
4
  o Provide a way to exclude certain schemas from the model (eg. prick/postspec/etc.)
4
5
  o Move SimpleType to PgCatalog schema
@@ -124,8 +124,8 @@ module PgGraph::Data
124
124
  def to_yaml() @impl.map { |k,v| [k, v.to_yaml] }.to_h end
125
125
 
126
126
  # Note that #to_sql in derived classes should be deleted FIXME
127
- def to_sql(format: :sql, ids: {}, delete: :all, files: [])
128
- render = SqlRender.new(self, format, ids: ids, delete: delete, files: files)
127
+ def to_sql(format: :sql, ids: {}, delete: :all, truncate: :none, files: [])
128
+ render = SqlRender.new(self, format, ids: ids, delete: delete, truncate: truncate, files: files)
129
129
  render.to_s
130
130
  end
131
131
 
@@ -16,7 +16,13 @@ module PgGraph::Data
16
16
  # recursive - delete data for table in the fox file including recursively depending tables
17
17
  # all - delete data from the whole database
18
18
  attr_reader :delete
19
-
19
+
20
+ # Which data to truncate:
21
+ # none - don't delete any data
22
+ # touched - delete data for tables in the fox file
23
+ # all - delete data from the whole database
24
+ attr_reader :truncate
25
+
20
26
  # +ids+ is a map from table UID to ID. Records with larger IDs will
21
27
  # be emitted as insert statements, records with IDs less or equal to the
22
28
  # given ID is emitted as update statements
@@ -25,10 +31,15 @@ module PgGraph::Data
25
31
  # :recursive, :all Only records with an ID greater than the corresponding
26
32
  # ID from +ids+ will be deleted
27
33
  #
34
+ # +truncate+ acts as +delete+ that has the major drawback that it doesn't
35
+ # delete records in dependency order (FIXME This is an error). You can use
36
+ # the SQL truncate statement instead of delete using this option but note
37
+ # that +ids+ should be empty for this to work
38
+ #
28
39
  # +files+ is a list of source file names to be included in the psql SQL
29
40
  # header as documentation. It can be set explicitly when #to_a or #to_h is
30
41
  # called (FIXME: is this used?)
31
- def initialize(database, format, ids: {}, delete: :all, files: [])
42
+ def initialize(database, format, ids: {}, delete: :all, truncate: :none, files: [])
32
43
  # puts "SqlRender#initialize"
33
44
  # puts " format: #{format.inspect}"
34
45
  # puts " ids: #{ids.inspect}"
@@ -36,10 +47,13 @@ module PgGraph::Data
36
47
  # puts " files: #{files.inspect}"
37
48
  constrain database, Database
38
49
  constrain ids, { String => Integer }
50
+ constrain ids.empty? || truncate == :none, true
51
+ constrain delete == :none || truncate == :none, true
39
52
  @database = database
40
53
  self.format = format
41
54
  (@ids = ids.dup).default = 0
42
55
  @delete = delete
56
+ @truncate = truncate
43
57
  @files = files
44
58
 
45
59
  @tables = database.schemas.map(&:tables).flatten.sort
@@ -79,6 +93,7 @@ module PgGraph::Data
79
93
  @to_h ||= {
80
94
  disable: render_triggers(:disable),
81
95
  delete: render_deletes(delete),
96
+ truncate: render_truncates(truncate),
82
97
  update: render_updates,
83
98
  insert: render_inserts,
84
99
  restart: render_restart_sequences,
@@ -90,7 +105,11 @@ module PgGraph::Data
90
105
  protected
91
106
  # Returns a single-element array of array of SQL statements
92
107
  def to_sql
93
- [to_h[:delete] + to_h[:update] + to_h[:insert]]
108
+ if delete != :none
109
+ [to_h[:delete] + to_h[:update] + to_h[:insert]]
110
+ else
111
+ [to_h[:truncate] + to_h[:update] + to_h[:insert]]
112
+ end
94
113
  end
95
114
 
96
115
  # Returns an array of non-empty arrays of SQL statements
@@ -146,6 +165,8 @@ module PgGraph::Data
146
165
  else
147
166
  raise ArgumentError
148
167
  end
168
+
169
+ # FIXME: Not in dependency order
149
170
  table_uids.map { |uid|
150
171
  if !@ids.key?(uid)
151
172
  "delete from #{uid};"
@@ -155,6 +176,18 @@ module PgGraph::Data
155
176
  }
156
177
  end
157
178
 
179
+ def render_truncates(kind)
180
+ table_uids =
181
+ case kind
182
+ when :none; []
183
+ when :touched; @tables.reject(&:empty?).map(&:uid)
184
+ when :all; @tables.map(&:uid)
185
+ else
186
+ raise ArgumentError
187
+ end
188
+ table_uids.map { |uid| "truncate #{uid} cascade;" }
189
+ end
190
+
158
191
  def render_updates
159
192
  @update_records.map { |record|
160
193
  "update #{record.table.uid} set " \
@@ -10,7 +10,7 @@ module PgGraph
10
10
  # Template for 'this' field name. Can be nil
11
11
  attr_reader :this
12
12
 
13
- # Template for 'that' field name or false if the field shouldn't be
13
+ # Template for 'that' field name or false if the field should not be
14
14
  # included in the model. Can be nil
15
15
  attr_reader :that
16
16
 
@@ -1,3 +1,3 @@
1
1
  module PgGraph
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/pg_graph.rb CHANGED
@@ -6,12 +6,12 @@ require "date"
6
6
 
7
7
  require "boolean"
8
8
  require "constrain"
9
+ require "forward_to"; include ForwardTo
9
10
  require "developer_exceptions"
10
11
 
11
12
  require "hash_tree"
12
13
 
13
14
  require "pg_graph/ext/meta.rb"
14
- require "pg_graph/ext/module.rb"
15
15
 
16
16
  require "pg_graph/version.rb"
17
17
  require "pg_graph/inflector.rb"
data/pg_graph.gemspec CHANGED
@@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
32
32
  spec.add_dependency "shellopts"
33
33
  spec.add_dependency "pg_conn"
34
34
  spec.add_dependency "pg_meta"
35
+ spec.add_dependency "forward_to"
35
36
 
36
37
  spec.add_development_dependency "rake"
37
38
  spec.add_development_dependency "rspec"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-24 00:00:00.000000000 Z
11
+ date: 2024-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: boolean
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: forward_to
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
139
153
  - !ruby/object:Gem::Dependency
140
154
  name: rake
141
155
  requirement: !ruby/object:Gem::Requirement
@@ -206,7 +220,6 @@ files:
206
220
  - lib/pg_graph/data/render.rb
207
221
  - lib/pg_graph/data/value.rb
208
222
  - lib/pg_graph/ext/meta.rb
209
- - lib/pg_graph/ext/module.rb
210
223
  - lib/pg_graph/inflector.rb
211
224
  - lib/pg_graph/reflector.rb
212
225
  - lib/pg_graph/timer.rb
@@ -240,7 +253,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
240
253
  - !ruby/object:Gem::Version
241
254
  version: '0'
242
255
  requirements: []
243
- rubygems_version: 3.3.18
256
+ rubygems_version: 3.3.7
244
257
  signing_key:
245
258
  specification_version: 4
246
259
  summary: Create graph type model of database
@@ -1,18 +0,0 @@
1
- class Module
2
- # Forward list of methods to object. The arguments should be strings or symbols
3
- def forward_method(object, *methods)
4
- forward_to(object, *methods)
5
- end
6
-
7
- # Same but with a better name
8
- def forward_to(object, *methods)
9
- for method in Array(methods).flatten
10
- if method =~ /=$/
11
- class_eval("def #{method}(arg) #{object}&.#{method}(arg) end")
12
- else
13
- class_eval("def #{method}(*args, &block) #{object}&.#{method}(*args, &block) end")
14
- end
15
- end
16
- end
17
- end
18
-