torque-postgresql 2.0.0 → 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/lib/torque/postgresql.rb +0 -1
  3. data/lib/torque/postgresql/adapter.rb +7 -0
  4. data/lib/torque/postgresql/adapter/database_statements.rb +2 -0
  5. data/lib/torque/postgresql/adapter/oid.rb +3 -1
  6. data/lib/torque/postgresql/adapter/oid/box.rb +2 -0
  7. data/lib/torque/postgresql/adapter/oid/circle.rb +2 -0
  8. data/lib/torque/postgresql/adapter/oid/enum.rb +2 -0
  9. data/lib/torque/postgresql/adapter/oid/enum_set.rb +2 -0
  10. data/lib/torque/postgresql/adapter/oid/interval.rb +2 -0
  11. data/lib/torque/postgresql/adapter/oid/line.rb +2 -0
  12. data/lib/torque/postgresql/adapter/oid/range.rb +2 -0
  13. data/lib/torque/postgresql/adapter/oid/segment.rb +2 -0
  14. data/lib/torque/postgresql/adapter/quoting.rb +2 -0
  15. data/lib/torque/postgresql/adapter/schema_creation.rb +8 -1
  16. data/lib/torque/postgresql/adapter/schema_definitions.rb +2 -0
  17. data/lib/torque/postgresql/adapter/schema_dumper.rb +23 -18
  18. data/lib/torque/postgresql/adapter/schema_statements.rb +2 -0
  19. data/lib/torque/postgresql/arel/infix_operation.rb +5 -1
  20. data/lib/torque/postgresql/arel/join_source.rb +2 -0
  21. data/lib/torque/postgresql/arel/nodes.rb +2 -0
  22. data/lib/torque/postgresql/arel/operations.rb +2 -0
  23. data/lib/torque/postgresql/arel/select_manager.rb +2 -0
  24. data/lib/torque/postgresql/arel/visitors.rb +6 -3
  25. data/lib/torque/postgresql/associations/association.rb +9 -1
  26. data/lib/torque/postgresql/associations/association_scope.rb +2 -0
  27. data/lib/torque/postgresql/associations/belongs_to_many_association.rb +17 -10
  28. data/lib/torque/postgresql/associations/builder/belongs_to_many.rb +2 -0
  29. data/lib/torque/postgresql/associations/builder/has_many.rb +2 -0
  30. data/lib/torque/postgresql/associations/preloader/association.rb +30 -1
  31. data/lib/torque/postgresql/attributes/builder.rb +3 -1
  32. data/lib/torque/postgresql/attributes/builder/enum.rb +5 -3
  33. data/lib/torque/postgresql/attributes/builder/period.rb +6 -4
  34. data/lib/torque/postgresql/attributes/enum.rb +5 -10
  35. data/lib/torque/postgresql/attributes/enum_set.rb +2 -0
  36. data/lib/torque/postgresql/attributes/lazy.rb +3 -1
  37. data/lib/torque/postgresql/attributes/period.rb +2 -0
  38. data/lib/torque/postgresql/auxiliary_statement.rb +2 -0
  39. data/lib/torque/postgresql/auxiliary_statement/settings.rb +2 -0
  40. data/lib/torque/postgresql/base.rb +2 -0
  41. data/lib/torque/postgresql/coder.rb +5 -3
  42. data/lib/torque/postgresql/collector.rb +2 -0
  43. data/lib/torque/postgresql/config.rb +5 -0
  44. data/lib/torque/postgresql/geometry_builder.rb +2 -0
  45. data/lib/torque/postgresql/i18n.rb +2 -0
  46. data/lib/torque/postgresql/inheritance.rb +2 -0
  47. data/lib/torque/postgresql/migration/command_recorder.rb +2 -0
  48. data/lib/torque/postgresql/railtie.rb +2 -0
  49. data/lib/torque/postgresql/reflection.rb +2 -0
  50. data/lib/torque/postgresql/reflection/abstract_reflection.rb +13 -5
  51. data/lib/torque/postgresql/reflection/association_reflection.rb +2 -0
  52. data/lib/torque/postgresql/reflection/belongs_to_many_reflection.rb +18 -4
  53. data/lib/torque/postgresql/reflection/has_many_reflection.rb +2 -0
  54. data/lib/torque/postgresql/reflection/runtime_reflection.rb +2 -0
  55. data/lib/torque/postgresql/reflection/through_reflection.rb +2 -0
  56. data/lib/torque/postgresql/relation.rb +15 -11
  57. data/lib/torque/postgresql/relation/auxiliary_statement.rb +6 -1
  58. data/lib/torque/postgresql/relation/distinct_on.rb +2 -0
  59. data/lib/torque/postgresql/relation/inheritance.rb +2 -0
  60. data/lib/torque/postgresql/relation/merger.rb +2 -0
  61. data/lib/torque/postgresql/schema_cache.rb +2 -0
  62. data/lib/torque/postgresql/version.rb +3 -1
  63. data/spec/schema.rb +2 -2
  64. data/spec/tests/arel_spec.rb +3 -1
  65. data/spec/tests/belongs_to_many_spec.rb +6 -0
  66. data/spec/tests/enum_set_spec.rb +1 -1
  67. data/spec/tests/enum_spec.rb +7 -0
  68. data/spec/tests/has_many_spec.rb +11 -1
  69. data/spec/tests/table_inheritance_spec.rb +14 -1
  70. metadata +9 -10
  71. 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 connection.table_exists?(table_name)
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 = arel_attribute('#{attribute}') # attr = arel_attribute('role')
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 = arel_attribute('#{attribute}') # attr = arel_attribute('role')
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 = arel_attribute('#{attribute}') # attr = arel_attribute('role')
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 ||= "arel_attribute(#{attribute.inspect})"
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
- conn_name = connection_specification_name
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(name)
114
- ActiveRecord::Base.connection_handler.retrieve_connection(name)
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 Attributes
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Attributes
@@ -16,7 +18,7 @@ module Torque
16
18
  end
17
19
 
18
20
  def inspect
19
- 'nil'.freeze
21
+ 'nil'
20
22
  end
21
23
 
22
24
  def __class__
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Attributes
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'auxiliary_statement/settings'
2
4
 
3
5
  module Torque
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  class AuxiliaryStatement
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Base
@@ -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 = ','.freeze
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,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Collector
@@ -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,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  class GeometryBuilder < ActiveModel::Type::Value
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module I18n
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  InheritanceError = Class.new(ArgumentError)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Migration
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  # = Torque PostgreSQL Railtie
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'reflection/abstract_reflection'
2
4
  require_relative 'reflection/association_reflection'
3
5
  require_relative 'reflection/belongs_to_many_reflection'
@@ -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'.freeze
8
- ARR_CAST = 'bigint[]'.freeze
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[join_keys.key.to_s]
75
- source_attr = foreign_table[join_keys.foreign_key.to_s]
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
@@ -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[:primary_key] || derive_foreign_key.freeze
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[:foreign_key] || derive_primary_key
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 derive_foreign_key
49
+ def derive_primary_key
36
50
  klass.primary_key
37
51
  end
38
52
 
39
- def derive_primary_key
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
  module Torque
2
4
  module PostgreSQL
3
5
  module Reflection
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Reflection
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Reflection
@@ -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.arel_attribute(item) : klass.arel_attribute(item)
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(Array.wrap(other_list), key) }
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
- Relation::SINGLE_VALUE_METHODS.each do |value|
142
- ActiveRecord::QueryMethods::DEFAULT_VALUES[value] = nil \
143
- if ActiveRecord::QueryMethods::DEFAULT_VALUES[value].nil?
144
- end
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
- Relation::MULTI_VALUE_METHODS.each do |value|
147
- ActiveRecord::QueryMethods::DEFAULT_VALUES[value] ||= \
148
- ActiveRecord::QueryMethods::FROZEN_EMPTY_ARRAY
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