updateable_views_inheritance 1.1.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile +2 -0
- data/README.rdoc +8 -3
- data/lib/updateable_views_inheritance/postgresql_adapter.rb +17 -33
- data/lib/updateable_views_inheritance/version.rb +1 -1
- data/test/schema_test.rb +16 -0
- metadata +5 -18
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MmZhMjYwNzIwZjgxZGQwNTEyOWFlN2YwYzQwNjkzYmNhZmJlZTk5Zg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NDQ2YWIwNmNmODRkODZjNzY5N2ZhYzIxN2QwMjk1NmNmMzk5N2IwMw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NGJiODc3MWQ3MTgwMThlZTcwOGM0OWMwNzNhMTk1NzdkZjRiZjg1NTNlN2Mw
|
10
|
+
MDkzYzAyMTVlZWNjODE1YmU1ZWVmMDQ1Y2IzOGE3NGQ0Njc4MjUyNWYwMTRl
|
11
|
+
ODQwODY4NmRkZTcyMGZjMjg0MTkwMjlmZDE3NDk3NWUxZTdhMGU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MTIzMWViYTE2YWEwMmUxNDY2NDc2MDAwZDFiZTI5ZjVjMzI5ZWIyNTJjM2U2
|
14
|
+
NDRkZTE4YzgyMWI2Mzg5NWFlY2I4NThjNDI4YzkzYjMwYWM5Y2MyNTA3NjVk
|
15
|
+
YTFjZmQxYTI1YTFlMTUyNDFmMThmN2NkMmRhZDE5YTg2MDU4NDY=
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.rdoc
CHANGED
@@ -110,7 +110,12 @@ The approach of this gem is completely independent from Rails built-in Single Ta
|
|
110
110
|
If you use fixtures, you must run <tt>rake updateable_views_inheritance:fixture</tt> to generate fixture for the updateable_views_inheritance table after you add/remove
|
111
111
|
classes from the hierarchy or change underlying table or view names. <b>Without it primary key sequence for inheritors' tables won't be bumped to the max and it might not be possible to save objects!</b> If you don't use fixtures for the classes in the hierarchy you don't need to do that.
|
112
112
|
|
113
|
-
This gem re-enables referential integrity on fixture loading
|
114
|
-
example:
|
113
|
+
This gem re-enables referential integrity on fixture loading. This means that
|
115
114
|
|
116
|
-
fixtures
|
115
|
+
fixtures :all
|
116
|
+
|
117
|
+
may fail when there are foreign key constraints on tables. To fix this, explicitly declare fixture load order in <tt>test_helper.rb</tt>:
|
118
|
+
|
119
|
+
fixtures :roots, :trunks, :leafs, ...
|
120
|
+
|
121
|
+
for all fixtures you want to load.
|
@@ -9,15 +9,19 @@ module ActiveRecord #:nodoc:
|
|
9
9
|
# default is <tt>"#{child_view}_data"</tt>
|
10
10
|
def create_child(child_view, options)
|
11
11
|
raise 'Please call me with a parent, for example: create_child(:steam_locomotives, :parent => :locomotives)' unless options[:parent]
|
12
|
+
|
13
|
+
schema, unqualified_child_view_name = Utils.extract_schema_and_table(child_view)
|
14
|
+
|
12
15
|
parent_relation = options[:parent].to_s
|
13
16
|
if tables.include?(parent_relation)
|
14
17
|
parent_table = parent_relation
|
15
18
|
else # view, interpreted as inheritance chain deeper than two levels
|
16
19
|
parent_table = query("SELECT child_relation FROM updateable_views_inheritance WHERE child_aggregate_view = #{quote(parent_relation)}")[0][0]
|
17
20
|
end
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
+
|
22
|
+
child_table = options[:table] || quote_table_name("#{child_view}_data")
|
23
|
+
child_table_pk = "#{unqualified_child_view_name.singularize}_id"
|
24
|
+
|
21
25
|
create_table(child_table, :id => false) do |t|
|
22
26
|
t.integer child_table_pk, :null => false
|
23
27
|
yield t
|
@@ -58,8 +62,8 @@ module ActiveRecord #:nodoc:
|
|
58
62
|
make_child_view_updateable(parent_table, parent_column_names, parent_pk, parent_pk_seq, child_view, child_column_names, child_pk, child_table)
|
59
63
|
|
60
64
|
# assign default values for table columns on the view - it is not automatic in Postgresql 8.1
|
61
|
-
set_defaults(child_view, parent_table
|
62
|
-
set_defaults(child_view, child_table
|
65
|
+
set_defaults(child_view, parent_table)
|
66
|
+
set_defaults(child_view, child_table)
|
63
67
|
create_system_table_records(parent_table, child_view, child_table)
|
64
68
|
end
|
65
69
|
|
@@ -288,8 +292,8 @@ module ActiveRecord #:nodoc:
|
|
288
292
|
# insert
|
289
293
|
# NEW.#{parent_pk} can be explicitly specified and when it is null every call to it increments the sequence.
|
290
294
|
# Setting the sequence to its value (explicitly supplied or the default) covers both cases.
|
291
|
-
execute <<-end_sql
|
292
|
-
CREATE OR REPLACE RULE #{child_view}_insert AS
|
295
|
+
execute <<-end_sql
|
296
|
+
CREATE OR REPLACE RULE #{quote_column_name("#{child_view}_insert")} AS
|
293
297
|
ON INSERT TO #{child_view} DO INSTEAD (
|
294
298
|
SELECT setval('#{parent_pk_seq}', NEW.#{parent_pk});
|
295
299
|
INSERT INTO #{parent_table}
|
@@ -304,14 +308,14 @@ module ActiveRecord #:nodoc:
|
|
304
308
|
|
305
309
|
# delete
|
306
310
|
execute <<-end_sql
|
307
|
-
CREATE OR REPLACE RULE #{child_view}_delete AS
|
311
|
+
CREATE OR REPLACE RULE #{quote_column_name("#{child_view}_delete")} AS
|
308
312
|
ON DELETE TO #{child_view} DO INSTEAD
|
309
313
|
DELETE FROM #{parent_table} WHERE #{parent_pk} = OLD.#{parent_pk}
|
310
314
|
end_sql
|
311
315
|
|
312
316
|
# update
|
313
317
|
execute <<-end_sql
|
314
|
-
CREATE OR REPLACE RULE #{child_view}_update AS
|
318
|
+
CREATE OR REPLACE RULE #{quote_column_name("#{child_view}_update")} AS
|
315
319
|
ON UPDATE TO #{child_view} DO INSTEAD (
|
316
320
|
#{ parent_columns.empty? ? '':
|
317
321
|
"UPDATE #{parent_table}
|
@@ -338,34 +342,14 @@ module ActiveRecord #:nodoc:
|
|
338
342
|
end
|
339
343
|
|
340
344
|
# Set default values from the table columns for a view
|
341
|
-
def set_defaults(view_name, table_name
|
342
|
-
|
343
|
-
if !
|
344
|
-
execute("ALTER TABLE #{view_name} ALTER #{
|
345
|
+
def set_defaults(view_name, table_name)
|
346
|
+
column_definitions(table_name).each do |column_name, type, default, notnull|
|
347
|
+
if !default.nil?
|
348
|
+
execute("ALTER TABLE #{quote_table_name(view_name)} ALTER #{quote_column_name(column_name)} SET DEFAULT #{default}")
|
345
349
|
end
|
346
350
|
end
|
347
351
|
end
|
348
352
|
|
349
|
-
# ActiveRecord::ConnectionAdapters::Column objects have nil default value for serial primary key
|
350
|
-
def get_default_value(table_name, column_name)
|
351
|
-
result = query(<<-end_sql, 'Column default value')[0]
|
352
|
-
SELECT pg_catalog.pg_get_expr(d.adbin, d.adrelid) as constraint
|
353
|
-
FROM pg_catalog.pg_attrdef d, pg_catalog.pg_attribute a, pg_catalog.pg_class c
|
354
|
-
WHERE d.adrelid = a.attrelid
|
355
|
-
AND d.adnum = a.attnum
|
356
|
-
AND a.atthasdef
|
357
|
-
AND c.relname = '#{table_name}'
|
358
|
-
AND a.attrelid = c.oid
|
359
|
-
AND a.attname = '#{column_name}'
|
360
|
-
AND a.attnum > 0 AND NOT a.attisdropped
|
361
|
-
end_sql
|
362
|
-
if !result.nil? && !result.empty?
|
363
|
-
result[0]
|
364
|
-
else
|
365
|
-
nil
|
366
|
-
end
|
367
|
-
end
|
368
|
-
|
369
353
|
def create_system_table_records(parent_relation, child_aggregate_view, child_relation)
|
370
354
|
parent_relation, child_aggregate_view, child_relation = [parent_relation, child_aggregate_view, child_relation].collect{|rel| quote(rel.to_s)}
|
371
355
|
exists = query <<-end_sql
|
data/test/schema_test.rb
CHANGED
@@ -176,4 +176,20 @@ class UpdateableViewsInheritanceSchemaTest < ActiveSupport::TestCase
|
|
176
176
|
def test_table_exists
|
177
177
|
#TODO: test table_exists? monkey patch
|
178
178
|
end
|
179
|
+
|
180
|
+
class CreateChildInSchema < ActiveRecord::Migration
|
181
|
+
def self.up
|
182
|
+
execute "CREATE SCHEMA interrail"
|
183
|
+
create_child("interrail.steam_locomotives", :parent => :locomotives) do |t|
|
184
|
+
t.decimal :interrail_water_consumption, :precision => 6, :scale => 2
|
185
|
+
t.decimal :interrail_coal_consumption, :precision => 6, :scale => 2
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_create_child_in_schema
|
191
|
+
CreateChildInSchema.up
|
192
|
+
assert_equal %w(id interrail_coal_consumption interrail_water_consumption max_speed name type),
|
193
|
+
@connection.columns('interrail.steam_locomotives').map{ |c| c.name }.sort
|
194
|
+
end
|
179
195
|
end
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: updateable_views_inheritance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Sava Chankov
|
@@ -10,12 +9,11 @@ authors:
|
|
10
9
|
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date:
|
12
|
+
date: 2014-08-27 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: activerecord
|
17
16
|
requirement: !ruby/object:Gem::Requirement
|
18
|
-
none: false
|
19
17
|
requirements:
|
20
18
|
- - ~>
|
21
19
|
- !ruby/object:Gem::Version
|
@@ -23,7 +21,6 @@ dependencies:
|
|
23
21
|
type: :runtime
|
24
22
|
prerelease: false
|
25
23
|
version_requirements: !ruby/object:Gem::Requirement
|
26
|
-
none: false
|
27
24
|
requirements:
|
28
25
|
- - ~>
|
29
26
|
- !ruby/object:Gem::Version
|
@@ -31,7 +28,6 @@ dependencies:
|
|
31
28
|
- !ruby/object:Gem::Dependency
|
32
29
|
name: pg
|
33
30
|
requirement: !ruby/object:Gem::Requirement
|
34
|
-
none: false
|
35
31
|
requirements:
|
36
32
|
- - ! '>='
|
37
33
|
- !ruby/object:Gem::Version
|
@@ -39,7 +35,6 @@ dependencies:
|
|
39
35
|
type: :runtime
|
40
36
|
prerelease: false
|
41
37
|
version_requirements: !ruby/object:Gem::Requirement
|
42
|
-
none: false
|
43
38
|
requirements:
|
44
39
|
- - ! '>='
|
45
40
|
- !ruby/object:Gem::Version
|
@@ -47,7 +42,6 @@ dependencies:
|
|
47
42
|
- !ruby/object:Gem::Dependency
|
48
43
|
name: rails
|
49
44
|
requirement: !ruby/object:Gem::Requirement
|
50
|
-
none: false
|
51
45
|
requirements:
|
52
46
|
- - ~>
|
53
47
|
- !ruby/object:Gem::Version
|
@@ -55,7 +49,6 @@ dependencies:
|
|
55
49
|
type: :development
|
56
50
|
prerelease: false
|
57
51
|
version_requirements: !ruby/object:Gem::Requirement
|
58
|
-
none: false
|
59
52
|
requirements:
|
60
53
|
- - ~>
|
61
54
|
- !ruby/object:Gem::Version
|
@@ -63,7 +56,6 @@ dependencies:
|
|
63
56
|
- !ruby/object:Gem::Dependency
|
64
57
|
name: bundler
|
65
58
|
requirement: !ruby/object:Gem::Requirement
|
66
|
-
none: false
|
67
59
|
requirements:
|
68
60
|
- - ~>
|
69
61
|
- !ruby/object:Gem::Version
|
@@ -71,7 +63,6 @@ dependencies:
|
|
71
63
|
type: :development
|
72
64
|
prerelease: false
|
73
65
|
version_requirements: !ruby/object:Gem::Requirement
|
74
|
-
none: false
|
75
66
|
requirements:
|
76
67
|
- - ~>
|
77
68
|
- !ruby/object:Gem::Version
|
@@ -79,7 +70,6 @@ dependencies:
|
|
79
70
|
- !ruby/object:Gem::Dependency
|
80
71
|
name: rake
|
81
72
|
requirement: !ruby/object:Gem::Requirement
|
82
|
-
none: false
|
83
73
|
requirements:
|
84
74
|
- - ! '>='
|
85
75
|
- !ruby/object:Gem::Version
|
@@ -87,7 +77,6 @@ dependencies:
|
|
87
77
|
type: :development
|
88
78
|
prerelease: false
|
89
79
|
version_requirements: !ruby/object:Gem::Requirement
|
90
|
-
none: false
|
91
80
|
requirements:
|
92
81
|
- - ! '>='
|
93
82
|
- !ruby/object:Gem::Version
|
@@ -194,27 +183,26 @@ files:
|
|
194
183
|
homepage: http://github.com/tutuf/updateable_views_inheritance
|
195
184
|
licenses:
|
196
185
|
- MIT
|
186
|
+
metadata: {}
|
197
187
|
post_install_message:
|
198
188
|
rdoc_options: []
|
199
189
|
require_paths:
|
200
190
|
- lib
|
201
191
|
required_ruby_version: !ruby/object:Gem::Requirement
|
202
|
-
none: false
|
203
192
|
requirements:
|
204
193
|
- - ! '>='
|
205
194
|
- !ruby/object:Gem::Version
|
206
195
|
version: '0'
|
207
196
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
208
|
-
none: false
|
209
197
|
requirements:
|
210
198
|
- - ! '>='
|
211
199
|
- !ruby/object:Gem::Version
|
212
200
|
version: '0'
|
213
201
|
requirements: []
|
214
202
|
rubyforge_project:
|
215
|
-
rubygems_version:
|
203
|
+
rubygems_version: 2.4.1
|
216
204
|
signing_key:
|
217
|
-
specification_version:
|
205
|
+
specification_version: 4
|
218
206
|
summary: Class table inheritance for ActiveRecord
|
219
207
|
test_files:
|
220
208
|
- test/config/database.yml
|
@@ -292,4 +280,3 @@ test_files:
|
|
292
280
|
- test/schema_test.rb
|
293
281
|
- test/single_table_inheritance.rb
|
294
282
|
- test/test_helper.rb
|
295
|
-
has_rdoc:
|