torque-postgresql 2.0.0 → 2.0.5
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/lib/torque/postgresql.rb +0 -1
- data/lib/torque/postgresql/adapter.rb +7 -0
- data/lib/torque/postgresql/adapter/database_statements.rb +2 -0
- data/lib/torque/postgresql/adapter/oid.rb +3 -1
- data/lib/torque/postgresql/adapter/oid/box.rb +2 -0
- data/lib/torque/postgresql/adapter/oid/circle.rb +2 -0
- data/lib/torque/postgresql/adapter/oid/enum.rb +2 -0
- data/lib/torque/postgresql/adapter/oid/enum_set.rb +2 -0
- data/lib/torque/postgresql/adapter/oid/interval.rb +2 -0
- data/lib/torque/postgresql/adapter/oid/line.rb +2 -0
- data/lib/torque/postgresql/adapter/oid/range.rb +2 -0
- data/lib/torque/postgresql/adapter/oid/segment.rb +2 -0
- data/lib/torque/postgresql/adapter/quoting.rb +2 -0
- data/lib/torque/postgresql/adapter/schema_creation.rb +8 -1
- data/lib/torque/postgresql/adapter/schema_definitions.rb +2 -0
- data/lib/torque/postgresql/adapter/schema_dumper.rb +23 -18
- data/lib/torque/postgresql/adapter/schema_statements.rb +2 -0
- data/lib/torque/postgresql/arel/infix_operation.rb +5 -1
- data/lib/torque/postgresql/arel/join_source.rb +2 -0
- data/lib/torque/postgresql/arel/nodes.rb +2 -0
- data/lib/torque/postgresql/arel/operations.rb +2 -0
- data/lib/torque/postgresql/arel/select_manager.rb +2 -0
- data/lib/torque/postgresql/arel/visitors.rb +6 -3
- data/lib/torque/postgresql/associations/association.rb +9 -1
- data/lib/torque/postgresql/associations/association_scope.rb +2 -0
- data/lib/torque/postgresql/associations/belongs_to_many_association.rb +17 -10
- data/lib/torque/postgresql/associations/builder/belongs_to_many.rb +2 -0
- data/lib/torque/postgresql/associations/builder/has_many.rb +2 -0
- data/lib/torque/postgresql/associations/preloader/association.rb +30 -1
- data/lib/torque/postgresql/attributes/builder.rb +3 -1
- data/lib/torque/postgresql/attributes/builder/enum.rb +5 -3
- data/lib/torque/postgresql/attributes/builder/period.rb +6 -4
- data/lib/torque/postgresql/attributes/enum.rb +5 -10
- data/lib/torque/postgresql/attributes/enum_set.rb +2 -0
- data/lib/torque/postgresql/attributes/lazy.rb +3 -1
- data/lib/torque/postgresql/attributes/period.rb +2 -0
- data/lib/torque/postgresql/auxiliary_statement.rb +2 -0
- data/lib/torque/postgresql/auxiliary_statement/settings.rb +2 -0
- data/lib/torque/postgresql/base.rb +2 -0
- data/lib/torque/postgresql/coder.rb +5 -3
- data/lib/torque/postgresql/collector.rb +2 -0
- data/lib/torque/postgresql/config.rb +5 -0
- data/lib/torque/postgresql/geometry_builder.rb +2 -0
- data/lib/torque/postgresql/i18n.rb +2 -0
- data/lib/torque/postgresql/inheritance.rb +2 -0
- data/lib/torque/postgresql/migration/command_recorder.rb +2 -0
- data/lib/torque/postgresql/railtie.rb +2 -0
- data/lib/torque/postgresql/reflection.rb +2 -0
- data/lib/torque/postgresql/reflection/abstract_reflection.rb +13 -5
- data/lib/torque/postgresql/reflection/association_reflection.rb +2 -0
- data/lib/torque/postgresql/reflection/belongs_to_many_reflection.rb +18 -4
- data/lib/torque/postgresql/reflection/has_many_reflection.rb +2 -0
- data/lib/torque/postgresql/reflection/runtime_reflection.rb +2 -0
- data/lib/torque/postgresql/reflection/through_reflection.rb +2 -0
- data/lib/torque/postgresql/relation.rb +15 -11
- data/lib/torque/postgresql/relation/auxiliary_statement.rb +6 -1
- data/lib/torque/postgresql/relation/distinct_on.rb +2 -0
- data/lib/torque/postgresql/relation/inheritance.rb +2 -0
- data/lib/torque/postgresql/relation/merger.rb +2 -0
- data/lib/torque/postgresql/schema_cache.rb +2 -0
- data/lib/torque/postgresql/version.rb +3 -1
- data/spec/schema.rb +2 -2
- data/spec/tests/arel_spec.rb +3 -1
- data/spec/tests/belongs_to_many_spec.rb +6 -0
- data/spec/tests/enum_set_spec.rb +1 -1
- data/spec/tests/enum_spec.rb +7 -0
- data/spec/tests/has_many_spec.rb +11 -1
- data/spec/tests/table_inheritance_spec.rb +14 -1
- metadata +9 -10
- data/lib/torque/postgresql/autosave_association.rb +0 -39
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'builder/enum'
|
2
4
|
require_relative 'builder/period'
|
3
5
|
|
@@ -7,7 +9,7 @@ module Torque
|
|
7
9
|
module Builder
|
8
10
|
def self.include_on(klass, method_name, builder_klass, **extra, &block)
|
9
11
|
klass.define_singleton_method(method_name) do |*args, **options|
|
10
|
-
return unless
|
12
|
+
return unless table_exists?
|
11
13
|
|
12
14
|
args.each do |attribute|
|
13
15
|
begin
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Torque
|
2
4
|
module PostgreSQL
|
3
5
|
module Attributes
|
@@ -153,12 +155,12 @@ module Torque
|
|
153
155
|
cast_type = subtype.name.chomp('[]')
|
154
156
|
klass_module.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
155
157
|
def has_#{attribute.pluralize}(*values) # def has_roles(*values)
|
156
|
-
attr =
|
158
|
+
attr = arel_table['#{attribute}'] # attr = arel_table['role']
|
157
159
|
where(attr.contains(::Arel.array(values, cast: '#{cast_type}'))) # where(attr.contains(::Arel.array(values, cast: 'roles')))
|
158
160
|
end # end
|
159
161
|
|
160
162
|
def has_any_#{attribute.pluralize}(*values) # def has_roles(*values)
|
161
|
-
attr =
|
163
|
+
attr = arel_table['#{attribute}'] # attr = arel_table['role']
|
162
164
|
where(attr.overlaps(::Arel.array(values, cast: '#{cast_type}'))) # where(attr.overlaps(::Arel.array(values, cast: 'roles')))
|
163
165
|
end # end
|
164
166
|
RUBY
|
@@ -184,7 +186,7 @@ module Torque
|
|
184
186
|
values_methods.each do |key, (scope, ask, bang, val)|
|
185
187
|
klass_content += <<-RUBY
|
186
188
|
def #{scope} # def admin
|
187
|
-
attr =
|
189
|
+
attr = arel_table['#{attribute}'] # attr = arel_table['role']
|
188
190
|
where(::#{enum_klass}.scope(attr, '#{val}')) # where(Enum::Roles.scope(attr, 'admin'))
|
189
191
|
end # end
|
190
192
|
RUBY
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Torque
|
2
4
|
module PostgreSQL
|
3
5
|
module Attributes
|
@@ -202,7 +204,7 @@ module Torque
|
|
202
204
|
end
|
203
205
|
|
204
206
|
def arel_attribute
|
205
|
-
@arel_attribute ||= "
|
207
|
+
@arel_attribute ||= "arel_table[#{attribute.inspect}]"
|
206
208
|
end
|
207
209
|
|
208
210
|
def arel_default_sql
|
@@ -245,7 +247,7 @@ module Torque
|
|
245
247
|
def arel_real_start_at
|
246
248
|
return arel_start_at unless threshold.present?
|
247
249
|
@arel_real_start_at ||= begin
|
248
|
-
result = "(#{arel_start_at} - #{arel_threshold_value})"
|
250
|
+
result = +"(#{arel_start_at} - #{arel_threshold_value})"
|
249
251
|
result << '.cast(:date)' if type.eql?(:daterange)
|
250
252
|
result
|
251
253
|
end
|
@@ -255,7 +257,7 @@ module Torque
|
|
255
257
|
def arel_real_finish_at
|
256
258
|
return arel_finish_at unless threshold.present?
|
257
259
|
@arel_real_finish_at ||= begin
|
258
|
-
result = "(#{arel_finish_at} + #{arel_threshold_value})"
|
260
|
+
result = +"(#{arel_finish_at} + #{arel_threshold_value})"
|
259
261
|
result << '.cast(:date)' if type.eql?(:daterange)
|
260
262
|
result
|
261
263
|
end
|
@@ -276,7 +278,7 @@ module Torque
|
|
276
278
|
|
277
279
|
# Create an arel named function
|
278
280
|
def arel_named_function(name, *args)
|
279
|
-
result = "::Arel::Nodes::NamedFunction.new(#{name.to_s.inspect}"
|
281
|
+
result = +"::Arel::Nodes::NamedFunction.new(#{name.to_s.inspect}"
|
280
282
|
result << ', [' << args.join(', ') << ']' if args.present?
|
281
283
|
result << ')'
|
282
284
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Torque
|
2
4
|
module PostgreSQL
|
3
5
|
module Attributes
|
@@ -31,11 +33,6 @@ module Torque
|
|
31
33
|
end
|
32
34
|
end
|
33
35
|
|
34
|
-
# You can specify the connection name for each enum
|
35
|
-
def connection_specification_name
|
36
|
-
return self == Enum ? 'primary' : superclass.connection_specification_name
|
37
|
-
end
|
38
|
-
|
39
36
|
# Overpass new so blank values return only nil
|
40
37
|
def new(value)
|
41
38
|
return Lazy.new(self, LAZY_VALUE) if value.blank?
|
@@ -45,9 +42,7 @@ module Torque
|
|
45
42
|
# Load the list of values in a lazy way
|
46
43
|
def values
|
47
44
|
@values ||= self == Enum ? nil : begin
|
48
|
-
|
49
|
-
conn = connection(conn_name)
|
50
|
-
conn.enum_values(type_name).freeze
|
45
|
+
connection.enum_values(type_name).freeze
|
51
46
|
end
|
52
47
|
end
|
53
48
|
|
@@ -110,8 +105,8 @@ module Torque
|
|
110
105
|
end
|
111
106
|
|
112
107
|
# Get a connection based on its name
|
113
|
-
def connection
|
114
|
-
ActiveRecord::Base.
|
108
|
+
def connection
|
109
|
+
::ActiveRecord::Base.connection
|
115
110
|
end
|
116
111
|
|
117
112
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Torque
|
2
4
|
module PostgreSQL
|
3
5
|
module Coder
|
@@ -8,7 +10,7 @@ module Torque
|
|
8
10
|
class << self
|
9
11
|
|
10
12
|
NEED_QUOTE_FOR = /[\\"(){}, \t\n\r\v\f]/m
|
11
|
-
DELIMITER = ','
|
13
|
+
DELIMITER = ','
|
12
14
|
|
13
15
|
# This method replace the +read_array+ method from PG gem
|
14
16
|
# See https://github.com/ged/ruby-pg/blob/master/ext/pg_text_decoder.c#L177
|
@@ -32,7 +34,7 @@ module Torque
|
|
32
34
|
quoted = 0
|
33
35
|
escaped = false
|
34
36
|
result = []
|
35
|
-
part =
|
37
|
+
part = String.new
|
36
38
|
|
37
39
|
# Always start getting the non-collection character, the second char
|
38
40
|
stream.getc if stream.pos == 0
|
@@ -59,7 +61,7 @@ module Torque
|
|
59
61
|
|
60
62
|
escaped = false
|
61
63
|
quoted = 0
|
62
|
-
part =
|
64
|
+
part = String.new
|
63
65
|
|
64
66
|
when c == '"'
|
65
67
|
quoted = 1
|
@@ -1,7 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Torque
|
2
4
|
module PostgreSQL
|
3
5
|
include ActiveSupport::Configurable
|
4
6
|
|
7
|
+
# Stores a version check for compatibility purposes
|
8
|
+
AR610 = (ActiveRecord.gem_version >= Gem::Version.new('6.1.0'))
|
9
|
+
|
5
10
|
# Use the same logger as the Active Record one
|
6
11
|
def self.logger
|
7
12
|
ActiveRecord::Base.logger
|
@@ -1,11 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Torque
|
2
4
|
module PostgreSQL
|
3
5
|
module Reflection
|
4
6
|
module AbstractReflection
|
5
7
|
AREL_ATTR = ::Arel::Attributes::Attribute
|
6
8
|
|
7
|
-
ARR_NO_CAST = 'bigint'
|
8
|
-
ARR_CAST = 'bigint[]'
|
9
|
+
ARR_NO_CAST = 'bigint'
|
10
|
+
ARR_CAST = 'bigint[]'
|
9
11
|
|
10
12
|
# Check if the foreign key actually exists
|
11
13
|
def connected_through_array?
|
@@ -68,11 +70,18 @@ module Torque
|
|
68
70
|
klass_attr.overlaps(source_attr)
|
69
71
|
end
|
70
72
|
|
73
|
+
if PostgreSQL::AR610
|
74
|
+
# TODO: Deprecate this method
|
75
|
+
def join_keys
|
76
|
+
OpenStruct.new(key: join_primary_key, foreign_key: join_foreign_key)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
71
80
|
private
|
72
81
|
|
73
82
|
def build_id_constraint_between(table, foreign_table)
|
74
|
-
klass_attr = table[
|
75
|
-
source_attr = foreign_table[
|
83
|
+
klass_attr = table[join_primary_key]
|
84
|
+
source_attr = foreign_table[join_foreign_key]
|
76
85
|
|
77
86
|
build_id_constraint(klass_attr, source_attr)
|
78
87
|
end
|
@@ -117,7 +126,6 @@ module Torque
|
|
117
126
|
valid_inverse_reflection?(reflection)
|
118
127
|
end
|
119
128
|
end
|
120
|
-
|
121
129
|
end
|
122
130
|
|
123
131
|
::ActiveRecord::Reflection::AbstractReflection.prepend(AbstractReflection)
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Torque
|
2
4
|
module PostgreSQL
|
3
5
|
module Reflection
|
@@ -10,6 +12,10 @@ module Torque
|
|
10
12
|
true
|
11
13
|
end
|
12
14
|
|
15
|
+
def belongs_to?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
13
19
|
def collection?
|
14
20
|
true
|
15
21
|
end
|
@@ -19,7 +25,7 @@ module Torque
|
|
19
25
|
end
|
20
26
|
|
21
27
|
def foreign_key
|
22
|
-
@foreign_key ||= options[:
|
28
|
+
@foreign_key ||= options[:foreign_key] || derive_foreign_key.freeze
|
23
29
|
end
|
24
30
|
|
25
31
|
def association_foreign_key
|
@@ -27,16 +33,24 @@ module Torque
|
|
27
33
|
end
|
28
34
|
|
29
35
|
def active_record_primary_key
|
30
|
-
@active_record_primary_key ||= options[:
|
36
|
+
@active_record_primary_key ||= options[:primary_key] || derive_primary_key
|
37
|
+
end
|
38
|
+
|
39
|
+
def join_primary_key(*)
|
40
|
+
active_record_primary_key
|
41
|
+
end
|
42
|
+
|
43
|
+
def join_foreign_key
|
44
|
+
foreign_key
|
31
45
|
end
|
32
46
|
|
33
47
|
private
|
34
48
|
|
35
|
-
def
|
49
|
+
def derive_primary_key
|
36
50
|
klass.primary_key
|
37
51
|
end
|
38
52
|
|
39
|
-
def
|
53
|
+
def derive_foreign_key
|
40
54
|
"#{name.to_s.singularize}_ids"
|
41
55
|
end
|
42
56
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'relation/distinct_on'
|
2
4
|
require_relative 'relation/auxiliary_statement'
|
3
5
|
require_relative 'relation/inheritance'
|
@@ -48,12 +50,12 @@ module Torque
|
|
48
50
|
when String
|
49
51
|
::Arel.sql(klass.send(:sanitize_sql, item.to_s))
|
50
52
|
when Symbol
|
51
|
-
base ? base.
|
53
|
+
base ? base.arel_table[item] : klass.arel_table[item]
|
52
54
|
when Array
|
53
55
|
resolve_column(item, base)
|
54
56
|
when Hash
|
55
57
|
raise ArgumentError, 'Unsupported Hash for attributes on third level' if base
|
56
|
-
item.map { |key, other_list| resolve_column(
|
58
|
+
item.map { |key, other_list| resolve_column(other_list, key) }
|
57
59
|
else
|
58
60
|
raise ArgumentError, "Unsupported argument type: #{value} (#{value.class})"
|
59
61
|
end
|
@@ -65,8 +67,8 @@ module Torque
|
|
65
67
|
return unless relation
|
66
68
|
|
67
69
|
table = predicate_builder.send(:table)
|
68
|
-
if table.associated_with?(relation)
|
69
|
-
table.associated_table(relation).send(:klass)
|
70
|
+
if table.associated_with?(relation.to_s)
|
71
|
+
table.associated_table(relation.to_s).send(:klass)
|
70
72
|
else
|
71
73
|
raise ArgumentError, "Relation for #{relation} not found on #{klass}"
|
72
74
|
end
|
@@ -138,14 +140,16 @@ module Torque
|
|
138
140
|
ActiveRecord::QueryMethods::VALID_UNSCOPING_VALUES += %i[cast_records itself_only
|
139
141
|
distinct_on auxiliary_statements]
|
140
142
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
143
|
+
unless AR610
|
144
|
+
Relation::SINGLE_VALUE_METHODS.each do |value|
|
145
|
+
ActiveRecord::QueryMethods::DEFAULT_VALUES[value] = nil \
|
146
|
+
if ActiveRecord::QueryMethods::DEFAULT_VALUES[value].nil?
|
147
|
+
end
|
145
148
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
+
Relation::MULTI_VALUE_METHODS.each do |value|
|
150
|
+
ActiveRecord::QueryMethods::DEFAULT_VALUES[value] ||= \
|
151
|
+
ActiveRecord::QueryMethods::FROZEN_EMPTY_ARRAY
|
152
|
+
end
|
149
153
|
end
|
150
154
|
|
151
155
|
$VERBOSE = warn_level
|