active_record-mti 0.4.0.pre.1 → 0.4.0.pre.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -13
- data/lib/active_record/mti.rb +0 -1
- data/lib/active_record/mti/connection_adapters/postgresql/schema_statements.rb +25 -19
- data/lib/active_record/mti/relation.rb +2 -41
- data/lib/active_record/mti/version.rb +1 -1
- metadata +4 -6
- data/lib/active_record/mti/config.rb +0 -26
- data/lib/active_record/mti/table_oid.rb +0 -6
- data/lib/core_ext/array.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c8aad5e387fec209700a57d5366938eaa5149b5436d10b3c6a859f75b7c7716
|
4
|
+
data.tar.gz: b060126c06310a40843a64652eb11ff821ac4aaadce5a48f203cc801146f905a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d055ffae44af8954bd1eeb9ddc60f7611b9dd2596e44bc96096858a68731eb42030269db6e3e5b3f1a5ead71acbbf6ec1a17500c7ae7410225febeb7af285d96
|
7
|
+
data.tar.gz: '04865157eb1a894b3867a3c2b536838a56f600779178b799e2fb034dfd60e3476a5cf8e5a9d6aa6c8723f02a8a48feaf2b489c59375d946257b589ac301fb6ef'
|
data/README.md
CHANGED
@@ -73,19 +73,6 @@ Conventionally—to indicate the nature of inheritance—`ActiveRecord::MTI` exp
|
|
73
73
|
|
74
74
|
Note, `ActiveRecord::MTI` will fall back on the unnested `table_name` if it's unable to find the nested form, and short of that, it will use the superclass's `table_name`.
|
75
75
|
|
76
|
-
### Configuration
|
77
|
-
`ActiveRecord::MTI` can be configured using a configure block.
|
78
|
-
|
79
|
-
```ruby
|
80
|
-
# config/initializers/active_record_mti.rb
|
81
|
-
|
82
|
-
ActiveRecord::MTI.configure do |config|
|
83
|
-
config.table_name_nesting = true
|
84
|
-
config.nesting_seperator = '/'
|
85
|
-
config.singular_parent = true
|
86
|
-
end
|
87
|
-
```
|
88
|
-
|
89
76
|
### Migrations
|
90
77
|
|
91
78
|
In your migrations define a table to inherit from another table:
|
data/lib/active_record/mti.rb
CHANGED
@@ -18,37 +18,43 @@ module ActiveRecord
|
|
18
18
|
# we manually add it. Lastly we also create indexes on the child table to match those
|
19
19
|
# on the parent table since indexes are also not inherited.
|
20
20
|
def create_table(table_name, options = {})
|
21
|
-
if options
|
21
|
+
if (inherited_table = options.delete(:inherits))
|
22
22
|
options[:id] = false
|
23
23
|
options.delete(:primary_key)
|
24
|
-
end
|
25
|
-
|
26
|
-
if (inherited_table = options.delete(:inherits))
|
27
|
-
# options[:options] = options[:options].sub("INHERITS", "() INHERITS") if td.columns.empty?
|
28
24
|
options[:options] = [%(INHERITS ("#{inherited_table}")), options[:options]].compact.join
|
29
25
|
end
|
30
26
|
|
31
|
-
|
27
|
+
super(table_name, options) do |td|
|
28
|
+
yield(td) if block_given?
|
32
29
|
|
33
|
-
|
34
|
-
|
35
|
-
|
30
|
+
fix_inherits_statement(td) if inherited_table
|
31
|
+
end.tap do |result|
|
32
|
+
inherit_indexes(table_name, inherited_table) if inherited_table
|
33
|
+
end
|
34
|
+
end
|
36
35
|
|
37
|
-
|
38
|
-
|
36
|
+
def fix_inherits_statement(td)
|
37
|
+
if td.columns.empty? && ActiveRecord.version >= Gem::Version.new('5.0')
|
38
|
+
td.options.gsub!('INHERITS', '() INHERITS')
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def inherit_indexes(table_name, inherited_table)
|
43
|
+
inherited_table_primary_key = primary_key(inherited_table)
|
44
|
+
execute %(ALTER TABLE "#{table_name}" ADD PRIMARY KEY ("#{inherited_table_primary_key}"))
|
39
45
|
|
40
|
-
|
41
|
-
|
46
|
+
indexes(inherited_table).each do |index|
|
47
|
+
attributes = index_attributes(index)
|
42
48
|
|
43
|
-
|
44
|
-
|
45
|
-
end
|
49
|
+
# Why rails insists on being inconsistant with itself is beyond me.
|
50
|
+
attributes[:order] = attributes.delete(:orders)
|
46
51
|
|
47
|
-
|
52
|
+
if (index_name = build_index_name(attributes.delete(:name), inherited_table, table_name))
|
53
|
+
attributes[:name] = index_name
|
48
54
|
end
|
49
|
-
end
|
50
55
|
|
51
|
-
|
56
|
+
add_index table_name, index.columns, attributes
|
57
|
+
end
|
52
58
|
end
|
53
59
|
|
54
60
|
def index_attributes(index)
|
@@ -4,37 +4,6 @@ module ActiveRecord
|
|
4
4
|
module MTI
|
5
5
|
module Relation
|
6
6
|
|
7
|
-
# Consider using a join to avoid complicated selects?
|
8
|
-
# Maybe each "mti" belongs_to :mti_table foreign_key: :tableoid?
|
9
|
-
# SELECT p.relname, c.name, c.altitude
|
10
|
-
# FROM cities c, pg_class p
|
11
|
-
# WHERE c.altitude > 500 AND c.tableoid = p.oid;
|
12
|
-
|
13
|
-
|
14
|
-
# TODO: Introduce natural joins
|
15
|
-
# https://dba.stackexchange.com/questions/94050/query-parent-table-and-get-child-tables-columns
|
16
|
-
# EXPLAIN ANALYSE SELECT * FROM ONLY listeners NATURAL FULL JOIN "listeners/bridge";
|
17
|
-
|
18
|
-
# EXPLAIN ANALYSE SELECT * FROM "listeners/bridge";
|
19
|
-
|
20
|
-
# EXPLAIN ANALYSE SELECT * FROM listeners;
|
21
|
-
|
22
|
-
# EXPLAIN ANALYSE SELECT * FROM ONLY listeners
|
23
|
-
# NATURAL FULL JOIN "listeners/bridge"
|
24
|
-
# NATURAL FULL JOIN "listeners/integration"
|
25
|
-
# NATURAL FULL JOIN "listeners/nest_thermostat"
|
26
|
-
# NATURAL FULL JOIN "listeners/sensor"
|
27
|
-
# NATURAL FULL JOIN "listeners/system"
|
28
|
-
# NATURAL FULL JOIN "listeners/system_users"
|
29
|
-
# NATURAL FULL JOIN "listeners/user";
|
30
|
-
|
31
|
-
# table_name = mti_table.name
|
32
|
-
# scope :naturally, -> {
|
33
|
-
# descendants.inject(only) { |ar|
|
34
|
-
# ar.join
|
35
|
-
# }
|
36
|
-
# }
|
37
|
-
|
38
7
|
def build_arel(*)
|
39
8
|
super.tap do |ar|
|
40
9
|
build_mti(ar)
|
@@ -58,8 +27,8 @@ module ActiveRecord
|
|
58
27
|
case projection
|
59
28
|
when Arel::Attributes::Attribute
|
60
29
|
projection.relation.name == klass.table_name && projection.name.to_s == 'tableoid'
|
61
|
-
|
62
|
-
|
30
|
+
when Arel::Nodes::As
|
31
|
+
tableoid_projection?(projection.left)
|
63
32
|
when Arel::Nodes::SqlLiteral
|
64
33
|
projection == 'tableoid'
|
65
34
|
else
|
@@ -67,14 +36,6 @@ module ActiveRecord
|
|
67
36
|
end
|
68
37
|
end
|
69
38
|
|
70
|
-
# def exclusively(tables=[klass])
|
71
|
-
# @table = table.dup.tap do |t|
|
72
|
-
# t.only!
|
73
|
-
# end
|
74
|
-
# binding.pry
|
75
|
-
# self
|
76
|
-
# end
|
77
|
-
|
78
39
|
private
|
79
40
|
|
80
41
|
def perform_calculation(*)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record-mti
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.0.pre.
|
4
|
+
version: 0.4.0.pre.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dale Stevens
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -161,7 +161,6 @@ files:
|
|
161
161
|
- LICENSE
|
162
162
|
- README.md
|
163
163
|
- lib/active_record/mti.rb
|
164
|
-
- lib/active_record/mti/config.rb
|
165
164
|
- lib/active_record/mti/connection_adapters/postgresql/adapter.rb
|
166
165
|
- lib/active_record/mti/connection_adapters/postgresql/schema_statements.rb
|
167
166
|
- lib/active_record/mti/core_extension.rb
|
@@ -169,9 +168,7 @@ files:
|
|
169
168
|
- lib/active_record/mti/relation.rb
|
170
169
|
- lib/active_record/mti/schema_dumper.rb
|
171
170
|
- lib/active_record/mti/table.rb
|
172
|
-
- lib/active_record/mti/table_oid.rb
|
173
171
|
- lib/active_record/mti/version.rb
|
174
|
-
- lib/core_ext/array.rb
|
175
172
|
- lib/core_ext/hash.rb
|
176
173
|
- lib/core_ext/thread.rb
|
177
174
|
homepage: https://github.com/twilightcoders/active_record-mti
|
@@ -195,7 +192,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
195
192
|
- !ruby/object:Gem::Version
|
196
193
|
version: 1.3.1
|
197
194
|
requirements: []
|
198
|
-
|
195
|
+
rubyforge_project:
|
196
|
+
rubygems_version: 2.7.6.2
|
199
197
|
signing_key:
|
200
198
|
specification_version: 4
|
201
199
|
summary: Multi Table Inheritance for PostgreSQL in Rails
|
@@ -1,26 +0,0 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module MTI
|
3
|
-
class << self
|
4
|
-
attr_accessor :configuration
|
5
|
-
end
|
6
|
-
|
7
|
-
DEFAULT_CONFIG = {
|
8
|
-
table_name_nesting: true,
|
9
|
-
nesting_seperator: '/',
|
10
|
-
singular_parent: true,
|
11
|
-
prefix_parent: true,
|
12
|
-
suffix_parent: false,
|
13
|
-
namespace_depth: 0 # -1 for all
|
14
|
-
}
|
15
|
-
|
16
|
-
def self.reset_configuration
|
17
|
-
self.configuration = OpenStruct.new(DEFAULT_CONFIG)
|
18
|
-
end
|
19
|
-
|
20
|
-
self.reset_configuration
|
21
|
-
|
22
|
-
def self.configure
|
23
|
-
yield(configuration)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
data/lib/core_ext/array.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
Array.class_eval do
|
2
|
-
|
3
|
-
alias_method :join_original, :join
|
4
|
-
def join(separator=$,)
|
5
|
-
if block_given?
|
6
|
-
inject(String.new) do |collector, item|
|
7
|
-
collector << yield(item).to_s + separator
|
8
|
-
end.chomp(separator)
|
9
|
-
else
|
10
|
-
join_original(separator)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
end
|