pg_graph 0.4.2 → 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: 829bab0e543f48a02760b6a6888967bce94f2d5a4456fc6af81f43a084275532
4
- data.tar.gz: 0ca91b04090f570ef3bac358791e37f911e030c3dc334edb941a739896099057
3
+ metadata.gz: 123eb2c8963ee6c45420315db19a876e76e5322ca7069d74e3d50fa4792aab58
4
+ data.tar.gz: 5be39db46db7775b25a40b02c76c65cedb8bf365128d5c3b602c1e3ca9003552
5
5
  SHA512:
6
- metadata.gz: 5fc04dbdebdf415b9c6e51e5af2a398689c517c7becae479a4f8eee9f5fcafa5bcd78d52e370fbfc8b893d86fa09e2cc14b41b8f161c55f67fed1f0caa749d84
7
- data.tar.gz: dec4aad67d9eba1236688baca32f013889748083ecec306b081dafd9c119c20fc05cdac422e0aa13e40da7417335681d791173c5d24113b0d5f21ab178398435
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.2"
2
+ VERSION = "0.5.0"
3
3
  end
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.2
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-04-20 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