updateable_views_inheritance 1.4.2 → 1.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.deepsource.toml +8 -0
- data/.github/workflows/build.yml +66 -0
- data/.gitignore +4 -1
- data/.vscode/settings.json +6 -0
- data/CHANGELOG.md +24 -8
- data/README.md +166 -0
- data/Rakefile +3 -3
- data/lib/updateable_views_inheritance/active_record.rb +3 -1
- data/lib/updateable_views_inheritance/postgresql_adapter.rb +205 -191
- data/lib/updateable_views_inheritance/version.rb +1 -1
- data/test/content_test.rb +2 -1
- data/test/dummy/app/assets/config/manifest.js +0 -0
- data/test/dummy/config/database.yml +1 -0
- data/test/install_generator_test.rb +2 -2
- data/test/instantiation_test.rb +38 -0
- data/test/migration_test.rb +3 -3
- data/test/schema_test.rb +32 -2
- data/test/single_table_inheritance.rb +1 -1
- data/test/test_helper.rb +21 -0
- data/updateable_views_inheritance.gemspec +8 -4
- metadata +81 -19
- data/README.rdoc +0 -121
data/test/schema_test.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require_relative 'test_helper'
|
2
2
|
|
3
3
|
class UpdateableViewsInheritanceSchemaTest < ActiveSupport::TestCase
|
4
4
|
def setup
|
@@ -14,7 +14,6 @@ class UpdateableViewsInheritanceSchemaTest < ActiveSupport::TestCase
|
|
14
14
|
assert_equal 'id', @connection.primary_key(:maglev_locomotives), "Wrong or no primary key for child aggregate view"
|
15
15
|
end
|
16
16
|
|
17
|
-
|
18
17
|
def test_content_columns
|
19
18
|
assert !SteamLocomotive.content_columns.map(&:name).include?("id")
|
20
19
|
end
|
@@ -221,4 +220,35 @@ class UpdateableViewsInheritanceSchemaTest < ActiveSupport::TestCase
|
|
221
220
|
assert @connection.columns(:bicycles).map{ |c| c.name }.include?('wheel_size'),
|
222
221
|
"Newly added column not present in view after rebuild for 2. hierarchy"
|
223
222
|
end
|
223
|
+
|
224
|
+
class UseExistingTable < ActiveRecord::Migration
|
225
|
+
def self.up
|
226
|
+
create_table :tbl_diesel_locomotives do |t|
|
227
|
+
t.belongs_to :locomotives
|
228
|
+
t.integer :num_cylinders
|
229
|
+
end
|
230
|
+
create_child(:diesel_locomotives,
|
231
|
+
table: :tbl_diesel_locomotives,
|
232
|
+
parent: :locomotives,
|
233
|
+
skip_creating_child_table: true)
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
def test_skip_creating_child_table
|
238
|
+
UseExistingTable.up
|
239
|
+
assert @connection.columns(:diesel_locomotives).map(&:name).include?("num_cylinders")
|
240
|
+
end
|
241
|
+
|
242
|
+
class ReservedSQLWords < ActiveRecord::Migration
|
243
|
+
def self.up
|
244
|
+
create_child(:table, parent: :locomotives) do |t|
|
245
|
+
t.integer :column
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
def test_reserved_words_in_tables_and_columns
|
251
|
+
ReservedSQLWords.up
|
252
|
+
assert @connection.columns(:table).map(&:name).include?("column")
|
253
|
+
end
|
224
254
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,6 +1,27 @@
|
|
1
1
|
# Configure Rails Environment
|
2
2
|
ENV["RAILS_ENV"] = "test"
|
3
3
|
|
4
|
+
# test coverage
|
5
|
+
require 'simplecov'
|
6
|
+
require 'simplecov_json_formatter'
|
7
|
+
|
8
|
+
SimpleCov.start 'rails' do
|
9
|
+
if ENV['CI']
|
10
|
+
formatter SimpleCov::Formatter::JSONFormatter
|
11
|
+
else
|
12
|
+
formatter SimpleCov::Formatter::MultiFormatter.new([
|
13
|
+
SimpleCov::Formatter::SimpleFormatter,
|
14
|
+
SimpleCov::Formatter::HTMLFormatter
|
15
|
+
])
|
16
|
+
end
|
17
|
+
|
18
|
+
track_files "lib/**/*.rb"
|
19
|
+
# repeat the add_filter values in sonar-project.properties file
|
20
|
+
# otherwise sonarcloud does not calculate properly the coverage ratio
|
21
|
+
add_filter "lib/generators/updateable_views_inheritance/templates/create_updateable_views_inheritance.rb"
|
22
|
+
add_filter "lib/updateable_views_inheritance/version.rb"
|
23
|
+
end
|
24
|
+
|
4
25
|
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
5
26
|
require 'rails/test_help'
|
6
27
|
require 'updateable_views_inheritance'
|
@@ -19,10 +19,14 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
21
|
s.add_dependency "activerecord", "~> 4.2.8"
|
22
|
-
s.add_dependency "pg"
|
22
|
+
s.add_dependency "pg", "~> 0.21"
|
23
23
|
|
24
|
-
s.add_development_dependency '
|
25
|
-
s.add_development_dependency "
|
26
|
-
s.add_development_dependency "
|
24
|
+
s.add_development_dependency 'bigdecimal', '1.3.5'
|
25
|
+
s.add_development_dependency "bundler"
|
26
|
+
s.add_development_dependency "minitest"
|
27
|
+
s.add_development_dependency "rails", '= 4.2.11.1'
|
27
28
|
s.add_development_dependency "rake"
|
29
|
+
s.add_development_dependency "simplecov"
|
30
|
+
s.add_development_dependency "simplecov_json_formatter"
|
31
|
+
s.add_development_dependency "solargraph"
|
28
32
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sava Chankov
|
8
8
|
- Denitsa Belogusheva
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2024-10-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -27,12 +27,40 @@ dependencies:
|
|
27
27
|
version: 4.2.8
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: pg
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0.21'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0.21'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: bigdecimal
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 1.3.5
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 1.3.5
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: bundler
|
30
58
|
requirement: !ruby/object:Gem::Requirement
|
31
59
|
requirements:
|
32
60
|
- - ">="
|
33
61
|
- !ruby/object:Gem::Version
|
34
62
|
version: '0'
|
35
|
-
type: :
|
63
|
+
type: :development
|
36
64
|
prerelease: false
|
37
65
|
version_requirements: !ruby/object:Gem::Requirement
|
38
66
|
requirements:
|
@@ -57,32 +85,60 @@ dependencies:
|
|
57
85
|
name: rails
|
58
86
|
requirement: !ruby/object:Gem::Requirement
|
59
87
|
requirements:
|
60
|
-
- -
|
88
|
+
- - '='
|
61
89
|
- !ruby/object:Gem::Version
|
62
|
-
version: 4.2.
|
90
|
+
version: 4.2.11.1
|
63
91
|
type: :development
|
64
92
|
prerelease: false
|
65
93
|
version_requirements: !ruby/object:Gem::Requirement
|
66
94
|
requirements:
|
67
|
-
- -
|
95
|
+
- - '='
|
68
96
|
- !ruby/object:Gem::Version
|
69
|
-
version: 4.2.
|
97
|
+
version: 4.2.11.1
|
70
98
|
- !ruby/object:Gem::Dependency
|
71
|
-
name:
|
99
|
+
name: rake
|
72
100
|
requirement: !ruby/object:Gem::Requirement
|
73
101
|
requirements:
|
74
|
-
- - "
|
102
|
+
- - ">="
|
75
103
|
- !ruby/object:Gem::Version
|
76
|
-
version: '
|
104
|
+
version: '0'
|
77
105
|
type: :development
|
78
106
|
prerelease: false
|
79
107
|
version_requirements: !ruby/object:Gem::Requirement
|
80
108
|
requirements:
|
81
|
-
- - "
|
109
|
+
- - ">="
|
82
110
|
- !ruby/object:Gem::Version
|
83
|
-
version: '
|
111
|
+
version: '0'
|
84
112
|
- !ruby/object:Gem::Dependency
|
85
|
-
name:
|
113
|
+
name: simplecov
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
type: :development
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: simplecov_json_formatter
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
type: :development
|
134
|
+
prerelease: false
|
135
|
+
version_requirements: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
- !ruby/object:Gem::Dependency
|
141
|
+
name: solargraph
|
86
142
|
requirement: !ruby/object:Gem::Requirement
|
87
143
|
requirements:
|
88
144
|
- - ">="
|
@@ -104,11 +160,14 @@ executables: []
|
|
104
160
|
extensions: []
|
105
161
|
extra_rdoc_files: []
|
106
162
|
files:
|
163
|
+
- ".deepsource.toml"
|
164
|
+
- ".github/workflows/build.yml"
|
107
165
|
- ".gitignore"
|
166
|
+
- ".vscode/settings.json"
|
108
167
|
- CHANGELOG.md
|
109
168
|
- Gemfile
|
110
169
|
- MIT-LICENSE
|
111
|
-
- README.
|
170
|
+
- README.md
|
112
171
|
- Rakefile
|
113
172
|
- doc/template/horo.rb
|
114
173
|
- lib/generators/updateable_views_inheritance/install_generator.rb
|
@@ -121,6 +180,7 @@ files:
|
|
121
180
|
- test/content_test.rb
|
122
181
|
- test/deep_hierarchy_test.rb
|
123
182
|
- test/dummy/Rakefile
|
183
|
+
- test/dummy/app/assets/config/manifest.js
|
124
184
|
- test/dummy/app/assets/javascripts/application.js
|
125
185
|
- test/dummy/app/assets/stylesheets/application.css
|
126
186
|
- test/dummy/app/controllers/application_controller.rb
|
@@ -185,6 +245,7 @@ files:
|
|
185
245
|
- test/fixtures/steam_locomotives.yml
|
186
246
|
- test/fixtures/steam_trains.yml
|
187
247
|
- test/install_generator_test.rb
|
248
|
+
- test/instantiation_test.rb
|
188
249
|
- test/migration_test.rb
|
189
250
|
- test/pg_insert_returning_with_rules_spec.rb
|
190
251
|
- test/schema_test.rb
|
@@ -195,7 +256,7 @@ homepage: http://github.com/tutuf/updateable_views_inheritance
|
|
195
256
|
licenses:
|
196
257
|
- MIT
|
197
258
|
metadata: {}
|
198
|
-
post_install_message:
|
259
|
+
post_install_message:
|
199
260
|
rdoc_options: []
|
200
261
|
require_paths:
|
201
262
|
- lib
|
@@ -210,15 +271,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
210
271
|
- !ruby/object:Gem::Version
|
211
272
|
version: '0'
|
212
273
|
requirements: []
|
213
|
-
|
214
|
-
|
215
|
-
signing_key:
|
274
|
+
rubygems_version: 3.4.12
|
275
|
+
signing_key:
|
216
276
|
specification_version: 4
|
217
277
|
summary: Class table inheritance for ActiveRecord
|
218
278
|
test_files:
|
219
279
|
- test/content_test.rb
|
220
280
|
- test/deep_hierarchy_test.rb
|
221
281
|
- test/dummy/Rakefile
|
282
|
+
- test/dummy/app/assets/config/manifest.js
|
222
283
|
- test/dummy/app/assets/javascripts/application.js
|
223
284
|
- test/dummy/app/assets/stylesheets/application.css
|
224
285
|
- test/dummy/app/controllers/application_controller.rb
|
@@ -283,6 +344,7 @@ test_files:
|
|
283
344
|
- test/fixtures/steam_locomotives.yml
|
284
345
|
- test/fixtures/steam_trains.yml
|
285
346
|
- test/install_generator_test.rb
|
347
|
+
- test/instantiation_test.rb
|
286
348
|
- test/migration_test.rb
|
287
349
|
- test/pg_insert_returning_with_rules_spec.rb
|
288
350
|
- test/schema_test.rb
|
data/README.rdoc
DELETED
@@ -1,121 +0,0 @@
|
|
1
|
-
==Class Table Inheritance
|
2
|
-
|
3
|
-
Class Table Inheritance for ActiveRecord using updateable views.
|
4
|
-
|
5
|
-
More about the pattern on http://www.martinfowler.com/eaaCatalog/classTableInheritance.html. This gem messes very little with Rails inheritance mechanism.
|
6
|
-
Instead it relies on updatable views in the database to represent classes in the inheritance chain. The approach was {first suggested by John
|
7
|
-
Wilger}[http://web.archive.org/web/20060408145717/johnwilger.com/articles/2005/09/29/class-table-inheritance-in-rails-with-postgresql].
|
8
|
-
|
9
|
-
|
10
|
-
==Requirements
|
11
|
-
|
12
|
-
Rails: 4.x
|
13
|
-
|
14
|
-
Ruby: 1.9.3+
|
15
|
-
|
16
|
-
Database: PostgreSQL 8.1+ only. Patches for other DBMS are welcome. Note that you are not required to use updateable views, children relations can be tables (with some triggers involved) or materialized views.
|
17
|
-
|
18
|
-
==Install
|
19
|
-
Run
|
20
|
-
gem install updateable_views_inheritance
|
21
|
-
|
22
|
-
==Usage
|
23
|
-
|
24
|
-
===Setup
|
25
|
-
|
26
|
-
* In <tt>Gemfile</tt> add <tt>gem 'updateable_views_inheritance'</tt>
|
27
|
-
* Run <tt>rails generate updateable_views_inheritance:install && rake db:migrate</tt>
|
28
|
-
* In <tt>config/environment.rb</tt> set <tt>config.active_record.schema_format = :sql</tt>
|
29
|
-
* In case you're using fixtures, don't forget to run
|
30
|
-
|
31
|
-
rake updateable_views_inheritance:fixture
|
32
|
-
|
33
|
-
after every change to the class hierarchy. Otherwise tests may fail.
|
34
|
-
|
35
|
-
===Example
|
36
|
-
|
37
|
-
class CtiExample < ActiveRecord::Migration
|
38
|
-
def self.up
|
39
|
-
create_table :locomotives do |t|
|
40
|
-
t.column :name, :string
|
41
|
-
t.column :max_speed, :integer
|
42
|
-
t.column :type, :string
|
43
|
-
end
|
44
|
-
|
45
|
-
create_child(:steam_locomotives, :parent => :locomotives) do |t|
|
46
|
-
t.decimal :water_consumption, :precision => 6, :scale => 2
|
47
|
-
t.decimal :coal_consumption, :precision => 6, :scale => 2
|
48
|
-
end
|
49
|
-
|
50
|
-
create_child(:electric_locomotives, :table => :raw_electric_locomotives, :parent => :locomotives) do |t|
|
51
|
-
t.decimal :electricity_consumption, :precision => 6, :scale => 2
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def self.down
|
56
|
-
drop_child :steam_locomotives
|
57
|
-
drop_child :electric_locomotives
|
58
|
-
drop_table :locomotives
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
And the models:
|
63
|
-
class Locomotive
|
64
|
-
end
|
65
|
-
|
66
|
-
class SteamLocomotive < Locomotive
|
67
|
-
self.table_name = :steam_locomotives
|
68
|
-
end
|
69
|
-
|
70
|
-
class ElectricLocomotive < Locomotive
|
71
|
-
self.table_name = :electric_locomotives
|
72
|
-
end
|
73
|
-
|
74
|
-
Note that models of children classes must specify table name explicitly.
|
75
|
-
|
76
|
-
===Changing Columns in Underlying Tables
|
77
|
-
|
78
|
-
class RemoveColumnInParentTable < ActiveRecord::Migration
|
79
|
-
def self.up
|
80
|
-
remove_parent_and_children_views(:locomotives)
|
81
|
-
remove_column(:locomotives, :max_speed)
|
82
|
-
rename_column(:name, :title)
|
83
|
-
rebuild_parent_and_children_views(:locomotives)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
===Renaming Underlying Tables
|
88
|
-
|
89
|
-
remove_parent_and_children_views(:old_name)
|
90
|
-
rename_table(:old_name,:new_name)
|
91
|
-
execute "UPDATE updateable_views_inheritance SET child_aggregate_view = 'new_name' WHERE child_aggregate_view = 'old_name'"
|
92
|
-
execute "UPDATE updateable_views_inheritance SET parent_relation = 'new_name' WHERE parent_relation = 'old_name'"
|
93
|
-
rebuild_parent_and_children_views(:new_name)
|
94
|
-
|
95
|
-
===Removing Classes
|
96
|
-
|
97
|
-
Note that you should remove only leaf classes (i.e. those that do not have descendants). If you want to erase a whole chain or part of chain you have to remove first the leaves and then their ancestors. Use <tt>drop_child(child_view)</tt> in migrations.
|
98
|
-
|
99
|
-
==Compatibility with Single Table Inheritance
|
100
|
-
|
101
|
-
The approach of this gem is completely independent from Rails built-in Single Table Inheritance. STI and CLTI can safely be mixed in one inheritance chain.
|
102
|
-
|
103
|
-
==Testing Your App
|
104
|
-
|
105
|
-
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
|
106
|
-
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.
|
107
|
-
|
108
|
-
This gem re-enables referential integrity on fixture loading. This means that
|
109
|
-
|
110
|
-
fixtures :all
|
111
|
-
|
112
|
-
may fail when there are foreign key constraints on tables. To fix this, explicitly declare fixture load order in <tt>test_helper.rb</tt>:
|
113
|
-
|
114
|
-
fixtures :roots, :trunks, :leafs, ...
|
115
|
-
|
116
|
-
for all fixtures you want to load.
|
117
|
-
|
118
|
-
==Gem Development & Testing
|
119
|
-
|
120
|
-
The gem has a comprehensive test suite. In order to run it, your user must be a superuser in PostgreSQL.
|
121
|
-
If this is not the case, run <tt>createuser -s pesho</tt> (assuming your Unix account is <tt>pesho</tt>).
|