updateable_views_inheritance 1.4.6 → 1.4.7
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 +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +15 -1
- data/lib/updateable_views_inheritance/postgresql_adapter.rb +16 -17
- data/lib/updateable_views_inheritance/version.rb +1 -1
- data/test/instantiation_test.rb +1 -3
- data/test/schema_test.rb +29 -2
- data/test/{single_table_inheritance.rb → single_table_inheritance_test.rb} +21 -14
- data/test/test_helper.rb +11 -9
- data/updateable_views_inheritance.gemspec +1 -0
- metadata +18 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 661b8ded59885a6e482cac9d0dfec5f9e1ab10050e3944b5f176e543958f8a33
|
4
|
+
data.tar.gz: fa8e6f63a74a28f9a09d1574ec6e3ae788df2ebdf3eead5789ef691c40337aed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1aec9cfeeb1aab4761e2efffc73219d7d9db7b605a8575fa470b5491783882415e51afdb8d7d48536242f0df4d2a448e8e3777c00ef9a65ec089001fb3e5a9e3
|
7
|
+
data.tar.gz: 46fb1b1a91344c01d55a48f47ef96e02f0d058a7f7781462c22cda18fa7d2a6eaa90f1e8cb94e7c1a03523c6d83d2aa500b43d4a83d011db0549bc65cfa4d9b6
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
## 1.4.7 (06 November 2024)
|
2
|
+
|
3
|
+
Bugfixes:
|
4
|
+
- Fix create_child so that it can be used idempotently
|
5
|
+
with :skip_creating_child_table.
|
6
|
+
|
1
7
|
## 1.4.6 (22 October 2024)
|
2
8
|
|
3
9
|
Features:
|
@@ -6,6 +12,9 @@ Features:
|
|
6
12
|
|
7
13
|
## 1.4.5 (09 October 2024)
|
8
14
|
|
15
|
+
Bugfixes:
|
16
|
+
## 1.4.5 (09 October 2024)
|
17
|
+
|
9
18
|
Bugfixes:
|
10
19
|
|
11
20
|
- Quote table and column names.
|
data/README.md
CHANGED
@@ -84,8 +84,10 @@ Note that models of children classes must specify table name explicitly.
|
|
84
84
|
|
85
85
|
### Changing Columns in Underlying Tables
|
86
86
|
|
87
|
+
#### In the parent
|
88
|
+
|
87
89
|
```ruby
|
88
|
-
class
|
90
|
+
class ChangeColumnsInParentTable < ActiveRecord::Migration
|
89
91
|
def self.up
|
90
92
|
remove_parent_and_children_views(:locomotives)
|
91
93
|
remove_column(:locomotives, :max_speed)
|
@@ -95,6 +97,18 @@ class RemoveColumnInParentTable < ActiveRecord::Migration
|
|
95
97
|
end
|
96
98
|
```
|
97
99
|
|
100
|
+
#### In a child
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
class ChangeColumnInChildTable < ActiveRecord::Migration
|
104
|
+
def self.up
|
105
|
+
drop_view(:steam_locomotives)
|
106
|
+
rename_column(:steam_locomotives_data, :coal_consumption, :fuel_consumption)
|
107
|
+
create_child_view(:locomotives, :steam_locomotives)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
98
112
|
### Renaming Underlying Tables
|
99
113
|
|
100
114
|
```ruby
|
@@ -31,22 +31,21 @@ module ActiveRecord #:nodoc:
|
|
31
31
|
parent_relation
|
32
32
|
end
|
33
33
|
|
34
|
-
child_table = options[:child_table] || options[:table] ||
|
34
|
+
child_table = options[:child_table] || options[:table] || "#{child_view}_data"
|
35
|
+
child_table_pk = options[:child_table_pk].to_s if options[:child_table_pk]
|
35
36
|
|
36
37
|
unless options.key?(:skip_creating_child_table)
|
37
|
-
|
38
|
-
|
38
|
+
unqualified_child_view_name = Utils.extract_schema_qualified_name(child_view).identifier
|
39
|
+
child_table_pk ||= "#{unqualified_child_view_name.singularize}_id"
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
41
|
+
create_table(child_table, id: false) do |t|
|
42
|
+
t.integer child_table_pk, null: false
|
43
|
+
yield t
|
44
|
+
end
|
45
|
+
execute "ALTER TABLE #{child_table} ADD PRIMARY KEY (#{child_table_pk})"
|
46
|
+
execute "ALTER TABLE #{child_table} ADD FOREIGN KEY (#{child_table_pk})
|
47
|
+
REFERENCES #{parent_table} ON DELETE CASCADE ON UPDATE CASCADE"
|
47
48
|
end
|
48
|
-
|
49
|
-
child_table_pk ||= options[:child_table_pk].to_s if options[:child_table_pk]
|
50
49
|
|
51
50
|
create_child_view(parent_relation, child_view, child_table, child_table_pk)
|
52
51
|
end
|
@@ -61,14 +60,14 @@ module ActiveRecord #:nodoc:
|
|
61
60
|
|
62
61
|
# Creates aggregate updateable view of parent and child relations. The convention for naming child tables is
|
63
62
|
# <tt>"#{child_view}_data"</tt>. If you don't follow it, supply +child_table_name+ as third argument.
|
64
|
-
def create_child_view(parent_table, child_view, child_table=nil, child_table_pk=nil)
|
65
|
-
child_table ||= child_view
|
63
|
+
def create_child_view(parent_table, child_view, child_table = nil, child_table_pk = nil)
|
64
|
+
child_table ||= "#{child_view}_data"
|
66
65
|
|
67
66
|
parent_columns = columns(parent_table)
|
68
67
|
child_columns = columns(child_table)
|
69
68
|
|
70
|
-
child_column_names = child_columns.
|
71
|
-
parent_column_names = parent_columns.
|
69
|
+
child_column_names = child_columns.map(&:name)
|
70
|
+
parent_column_names = parent_columns.map(&:name)
|
72
71
|
|
73
72
|
child_pk = child_table_pk || pk_and_sequence_for(child_table)[0]
|
74
73
|
child_column_names.delete(child_pk)
|
@@ -138,7 +137,7 @@ module ActiveRecord #:nodoc:
|
|
138
137
|
|
139
138
|
# Drops a view from the database.
|
140
139
|
def drop_view(name)
|
141
|
-
execute "DROP VIEW #{name}"
|
140
|
+
execute "DROP VIEW #{quote_table_name(name)}"
|
142
141
|
end
|
143
142
|
|
144
143
|
# Return the list of all views in the schema search path.
|
data/test/instantiation_test.rb
CHANGED
@@ -18,10 +18,8 @@ class InstantiationTest < ActiveSupport::TestCase
|
|
18
18
|
ActiveRecord::FixtureSet.reset_cache
|
19
19
|
end
|
20
20
|
|
21
|
+
# FIXME: flaky test_setting_disable_inheritance_instantiation_does_not_load_child_columns
|
21
22
|
def test_setting_disable_inheritance_instantiation_does_not_load_child_columns
|
22
|
-
unless Locomotive.first
|
23
|
-
puts "num locomotives:", @connection.select_one("SELECT count(id) FROM locomotives")
|
24
|
-
end
|
25
23
|
assert_equal %w[id max_speed name type],
|
26
24
|
Locomotive.first.attributes.keys.sort
|
27
25
|
end
|
data/test/schema_test.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require_relative 'test_helper'
|
2
2
|
|
3
|
-
class
|
3
|
+
class SchemaTest < ActiveSupport::TestCase
|
4
4
|
def setup
|
5
5
|
ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 5)
|
6
6
|
@connection = ActiveRecord::Base.connection
|
@@ -31,12 +31,19 @@ class UpdateableViewsInheritanceSchemaTest < ActiveSupport::TestCase
|
|
31
31
|
end
|
32
32
|
create_child_view :parent_pk_only, :child
|
33
33
|
end
|
34
|
+
|
35
|
+
def self.down
|
36
|
+
drop_child :child
|
37
|
+
drop_table :parent_pk_only
|
38
|
+
end
|
34
39
|
end
|
35
40
|
|
36
41
|
def test_parent_table_with_only_one_column
|
37
42
|
ParentTableWithOnlyOneColumn.up
|
38
43
|
assert @connection.views.include?('child')
|
39
44
|
assert_equal %w(id name), @connection.columns(:child).map{|c| c.name}.sort
|
45
|
+
ensure
|
46
|
+
ParentTableWithOnlyOneColumn.down
|
40
47
|
end
|
41
48
|
|
42
49
|
class ChildTableWithOnlyOneColumn < ActiveRecord::Migration
|
@@ -176,6 +183,20 @@ class UpdateableViewsInheritanceSchemaTest < ActiveSupport::TestCase
|
|
176
183
|
@connection.columns(:electric_locomotives).map{ |c| c.name }.sort
|
177
184
|
end
|
178
185
|
|
186
|
+
class ChangeColumnInChildTable < ActiveRecord::Migration
|
187
|
+
def self.up
|
188
|
+
drop_view(:steam_locomotives)
|
189
|
+
rename_column(:steam_locomotives_data, :coal_consumption, :fuel_consumption)
|
190
|
+
create_child_view(:locomotives, :steam_locomotives)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_change_column_in_child_table
|
195
|
+
ChangeColumnInChildTable.up
|
196
|
+
assert_equal %w(fuel_consumption id max_speed name type water_consumption),
|
197
|
+
@connection.columns(:steam_locomotives).map(&:name).sort
|
198
|
+
end
|
199
|
+
|
179
200
|
def test_table_exists
|
180
201
|
#TODO: test table_exists? monkey patch
|
181
202
|
end
|
@@ -249,11 +270,16 @@ class UpdateableViewsInheritanceSchemaTest < ActiveSupport::TestCase
|
|
249
270
|
t.integer :column
|
250
271
|
end
|
251
272
|
end
|
273
|
+
def self.down
|
274
|
+
drop_child :table
|
275
|
+
end
|
252
276
|
end
|
253
277
|
|
254
278
|
def test_reserved_words_in_tables_and_columns
|
255
279
|
ReservedSQLWords.up
|
256
280
|
assert @connection.columns(:table).map(&:name).include?("column")
|
281
|
+
ensure
|
282
|
+
ReservedSQLWords.down
|
257
283
|
end
|
258
284
|
|
259
285
|
class ChildTableIsActuallyView < ActiveRecord::Migration
|
@@ -280,6 +306,7 @@ class UpdateableViewsInheritanceSchemaTest < ActiveSupport::TestCase
|
|
280
306
|
|
281
307
|
def test_child_table_is_view
|
282
308
|
ChildTableIsActuallyView.up
|
283
|
-
|
309
|
+
assert_equal @connection.columns(:punk_locomotives).map(&:name).sort,
|
310
|
+
%w(coal electro id max_speed name type)
|
284
311
|
end
|
285
312
|
end
|
@@ -124,18 +124,25 @@ class SingleTableInheritanceAggregateViewTest < ActiveSupport::TestCase
|
|
124
124
|
end
|
125
125
|
end
|
126
126
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
127
|
+
# FIXME: flaky test_single_table_inheritance_view_conflict_columns_with_values
|
128
|
+
# def test_single_table_inheritance_view_conflict_columns_with_values
|
129
|
+
# ConflictColumnsWithValues.up
|
130
|
+
# ::SteamLocomotive.reset_column_information
|
131
|
+
# ::ElectricLocomotive.reset_column_information
|
132
|
+
# assert_equal %w(coal_consumption id max_speed name type water_consumption electricity_consumption bidirectional narrow_gauge magnetic_field rail_system number_of_engines).sort,
|
133
|
+
# @connection.columns(:all_locomotives).map(&:name).sort
|
134
|
+
# assert_equal 'text', @connection.columns(:all_locomotives).detect{|c| c.name == "number_of_engines"}.sql_type
|
135
|
+
# assert_equal 'one', @connection.query("SELECT number_of_engines FROM all_locomotives WHERE id=#{SteamLocomotive.first.id}").first.first
|
136
|
+
# assert_equal '2', @connection.query("SELECT number_of_engines FROM all_locomotives WHERE id=#{ElectricLocomotive.first.id}").first.first
|
137
|
+
# end
|
138
|
+
|
139
|
+
# FIXME: flaky test_respond_to_missing_attributes
|
140
|
+
# def test_respond_to_missing_attributes
|
141
|
+
# Locomotive.table_name = :all_locomotives
|
142
|
+
# ::MaglevLocomotive.reset_column_information
|
143
|
+
# assert !MaglevLocomotive.new.respond_to?(:non_existing_attribute_in_the_hierarchy),
|
144
|
+
# "Active Record is gone haywire - responds to attributes that are never defined"
|
145
|
+
# assert !MaglevLocomotive.new.respond_to?(:coal_consumption),
|
146
|
+
# "Responds to an attribute defined only in the STI view, not in the class view"
|
147
|
+
# end
|
141
148
|
end
|
data/test/test_helper.rb
CHANGED
@@ -26,18 +26,20 @@ require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
|
26
26
|
require 'rails/test_help'
|
27
27
|
require 'updateable_views_inheritance'
|
28
28
|
|
29
|
-
|
30
|
-
if RUBY_VERSION > "2"
|
31
|
-
require 'byebug'
|
32
|
-
else
|
33
|
-
require 'debugger'
|
34
|
-
end
|
35
|
-
rescue LoadError
|
36
|
-
# no debugger available
|
37
|
-
end
|
29
|
+
require 'byebug'
|
38
30
|
|
39
31
|
class ActiveSupport::TestCase #:nodoc:
|
40
32
|
include ActiveRecord::TestFixtures
|
41
33
|
self.fixture_path = "#{File.dirname(__FILE__)}/fixtures/"
|
42
34
|
ActiveRecord::Migration.verbose = false
|
35
|
+
# self.use_transactional_fixtures = true
|
36
|
+
|
37
|
+
# def teardown
|
38
|
+
# ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
|
39
|
+
# ActiveRecord::FixtureSet.reset_cache
|
40
|
+
# end
|
43
41
|
end
|
42
|
+
|
43
|
+
# # Useful for debugging flaky tests that depend on order of other tests
|
44
|
+
# require "minitest/reporters"
|
45
|
+
# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
|
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
|
|
24
24
|
s.add_development_dependency 'bigdecimal', '1.3.5'
|
25
25
|
s.add_development_dependency "bundler"
|
26
26
|
s.add_development_dependency "minitest"
|
27
|
+
s.add_development_dependency "minitest-reporters"
|
27
28
|
s.add_development_dependency "rails", '= 4.2.11.1'
|
28
29
|
s.add_development_dependency "rake"
|
29
30
|
s.add_development_dependency "simplecov"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: updateable_views_inheritance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sava Chankov
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2024-
|
12
|
+
date: 2024-11-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -81,6 +81,20 @@ dependencies:
|
|
81
81
|
- - ">="
|
82
82
|
- !ruby/object:Gem::Version
|
83
83
|
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: minitest-reporters
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
84
98
|
- !ruby/object:Gem::Dependency
|
85
99
|
name: rails
|
86
100
|
requirement: !ruby/object:Gem::Requirement
|
@@ -249,7 +263,7 @@ files:
|
|
249
263
|
- test/migration_test.rb
|
250
264
|
- test/pg_insert_returning_with_rules_spec.rb
|
251
265
|
- test/schema_test.rb
|
252
|
-
- test/
|
266
|
+
- test/single_table_inheritance_test.rb
|
253
267
|
- test/test_helper.rb
|
254
268
|
- updateable_views_inheritance.gemspec
|
255
269
|
homepage: http://github.com/tutuf/updateable_views_inheritance
|
@@ -348,5 +362,5 @@ test_files:
|
|
348
362
|
- test/migration_test.rb
|
349
363
|
- test/pg_insert_returning_with_rules_spec.rb
|
350
364
|
- test/schema_test.rb
|
351
|
-
- test/
|
365
|
+
- test/single_table_inheritance_test.rb
|
352
366
|
- test/test_helper.rb
|