torque-postgresql 2.0.3 → 2.0.4

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.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/lib/torque/postgresql/adapter.rb +7 -0
  3. data/lib/torque/postgresql/adapter/database_statements.rb +2 -0
  4. data/lib/torque/postgresql/adapter/oid.rb +3 -1
  5. data/lib/torque/postgresql/adapter/oid/box.rb +2 -0
  6. data/lib/torque/postgresql/adapter/oid/circle.rb +2 -0
  7. data/lib/torque/postgresql/adapter/oid/enum.rb +2 -0
  8. data/lib/torque/postgresql/adapter/oid/enum_set.rb +2 -0
  9. data/lib/torque/postgresql/adapter/oid/interval.rb +2 -0
  10. data/lib/torque/postgresql/adapter/oid/line.rb +2 -0
  11. data/lib/torque/postgresql/adapter/oid/range.rb +2 -0
  12. data/lib/torque/postgresql/adapter/oid/segment.rb +2 -0
  13. data/lib/torque/postgresql/adapter/quoting.rb +2 -0
  14. data/lib/torque/postgresql/adapter/schema_creation.rb +8 -1
  15. data/lib/torque/postgresql/adapter/schema_definitions.rb +2 -0
  16. data/lib/torque/postgresql/adapter/schema_dumper.rb +3 -1
  17. data/lib/torque/postgresql/adapter/schema_statements.rb +2 -0
  18. data/lib/torque/postgresql/arel/infix_operation.rb +5 -1
  19. data/lib/torque/postgresql/arel/join_source.rb +2 -0
  20. data/lib/torque/postgresql/arel/nodes.rb +2 -0
  21. data/lib/torque/postgresql/arel/operations.rb +2 -0
  22. data/lib/torque/postgresql/arel/select_manager.rb +2 -0
  23. data/lib/torque/postgresql/arel/visitors.rb +6 -3
  24. data/lib/torque/postgresql/associations/association.rb +5 -1
  25. data/lib/torque/postgresql/associations/association_scope.rb +2 -0
  26. data/lib/torque/postgresql/associations/belongs_to_many_association.rb +3 -0
  27. data/lib/torque/postgresql/associations/builder/belongs_to_many.rb +2 -0
  28. data/lib/torque/postgresql/associations/builder/has_many.rb +2 -0
  29. data/lib/torque/postgresql/associations/preloader/association.rb +29 -1
  30. data/lib/torque/postgresql/attributes/builder.rb +2 -0
  31. data/lib/torque/postgresql/attributes/builder/enum.rb +5 -3
  32. data/lib/torque/postgresql/attributes/builder/period.rb +6 -4
  33. data/lib/torque/postgresql/attributes/enum.rb +5 -10
  34. data/lib/torque/postgresql/attributes/enum_set.rb +2 -0
  35. data/lib/torque/postgresql/attributes/lazy.rb +3 -1
  36. data/lib/torque/postgresql/attributes/period.rb +2 -0
  37. data/lib/torque/postgresql/autosave_association.rb +9 -3
  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 +2 -0
  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/enum_set_spec.rb +1 -1
  66. data/spec/tests/has_many_spec.rb +1 -1
  67. data/spec/tests/table_inheritance_spec.rb +1 -1
  68. metadata +6 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9e426686cf04fcf990d84b945abe3b3435168237f1ff24c6f23ac3b4941bbc4e
4
- data.tar.gz: 2f936f5676f80804e4766bc9bf64b81d70ddbd08e9409b373b0d59d7d20e8bf1
3
+ metadata.gz: f108bfb672743d7edd701b77eb102aa6f9164b4981e1f366d2354d9e8cf3360b
4
+ data.tar.gz: 60d7208f0798acbde69aee78fca7a92d4824c75dcecc44acd16dcf59568f0e04
5
5
  SHA512:
6
- metadata.gz: 373d085090e02e76eb2523f7c2d5a32434c002f712cf892aa38f81bc9935a51f2426054c97bb5df534c47e23fd1800d84b670b9443266908e2f076f20928e7a2
7
- data.tar.gz: f0441cc66de2ed015d0b8dbb13ae88dc4074a54c8477fc7a31fc1d51e2bfd7cb57ab0e3de529dcbd4feb3fa72dd1fc8894738868e8bfa36ad9490badc3d1c990
6
+ metadata.gz: 0a463f94c5a09f74bbb2ba57fe9fb2f7fe649f243c239f7709180c29988f2b5424bffec68d0ec1d2c4ae8ad1191d6156e96dd00c2a66a2a473e02411c563e21f
7
+ data.tar.gz: 1568915cd13e2b93b77a702cfd346326aa9e07605aac3deca0f3dacbe82b24762a84f3007a4dffc493888c94802724defbfc01037d0bad984e3735783a790f35
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'adapter/database_statements'
2
4
  require_relative 'adapter/oid'
3
5
  require_relative 'adapter/quoting'
@@ -19,6 +21,11 @@ module Torque
19
21
  select_value('SELECT version()').match(/#{Adapter::ADAPTER_NAME} ([\d\.]+)/)[1]
20
22
  )
21
23
  end
24
+
25
+ # Add `inherits` to the list of extracted table options
26
+ def extract_table_options!(options)
27
+ super.merge(options.extract!(:inherits))
28
+ end
22
29
  end
23
30
 
24
31
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Adapter
@@ -17,9 +17,11 @@ module Torque
17
17
  ActiveRecord::Type.register(:circle, OID::Circle, adapter: :postgresql)
18
18
  ActiveRecord::Type.register(:enum, OID::Enum, adapter: :postgresql)
19
19
  ActiveRecord::Type.register(:enum_set, OID::EnumSet, adapter: :postgresql)
20
- ActiveRecord::Type.register(:interval, OID::Interval, adapter: :postgresql)
21
20
  ActiveRecord::Type.register(:line, OID::Line, adapter: :postgresql)
22
21
  ActiveRecord::Type.register(:segment, OID::Segment, adapter: :postgresql)
22
+
23
+ ActiveRecord::Type.register(:interval, OID::Interval, adapter: :postgresql) \
24
+ unless PostgreSQL::AR610
23
25
  end
24
26
  end
25
27
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  class Box < Struct.new(:x1, :y1, :x2, :y2)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  class Circle < Struct.new(:x, :y, :r)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  class Line < Struct.new(:slope, :intercept)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  class Segment < Struct.new(:point0, :point1)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Adapter
@@ -24,10 +24,17 @@ module Torque
24
24
  end)
25
25
  end
26
26
 
27
+ if respond_to?(:supports_check_constraints?) && supports_check_constraints?
28
+ statements.concat(o.check_constraints.map do |expression, options|
29
+ check_constraint_in_create(o.name, expression, options)
30
+ end)
31
+ end
32
+
27
33
  create_sql << "(#{statements.join(', ')})" \
28
34
  if statements.present? || o.inherits.present?
29
35
 
30
- add_table_options!(create_sql, table_options(o))
36
+ options = PostgreSQL::AR610 ? o : table_options(o)
37
+ add_table_options!(create_sql, options)
31
38
 
32
39
  if o.inherits.present?
33
40
  tables = o.inherits.map(&method(:quote_table_name))
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Adapter
@@ -39,7 +41,7 @@ module Torque
39
41
 
40
42
  def tables(stream) # :nodoc:
41
43
  inherited_tables = @connection.inherited_tables
42
- sorted_tables = @connection.data_sources.sort - @connection.views
44
+ sorted_tables = @connection.tables.sort - @connection.views
43
45
 
44
46
  stream.puts " # These are the common tables managed"
45
47
  (sorted_tables - inherited_tables.keys).each do |table_name|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Arel
@@ -22,6 +24,8 @@ module Torque
22
24
  }.freeze
23
25
 
24
26
  INFLIX_OPERATION.each do |operator_name, operator|
27
+ next if nodes.const_defined?(operator_name)
28
+
25
29
  klass = Class.new(inflix)
26
30
  klass.send(:define_method, :initialize) { |*args| super(operator, *args) }
27
31
 
@@ -31,7 +35,7 @@ module Torque
31
35
  # Don't worry about quoting here, if the right side is something that
32
36
  # doesn't need quoting, it will leave it as it is
33
37
  Math.send(:define_method, operator_name.underscore) do |other|
34
- klass.new(self, nodes.build_quoted(other, self))
38
+ klass.new(self, other)
35
39
  end
36
40
  end
37
41
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Arel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Arel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Arel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Arel
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Arel
4
6
  module Visitors
5
7
  # Enclose select manager with parenthesis
6
8
  # :TODO: Remove when checking the new version of Arel
7
- def visit_Arel_SelectManager o, collector
9
+ def visit_Arel_SelectManager(o, collector)
8
10
  collector << '('
9
11
  visit(o.ast, collector) << ')'
10
12
  end
@@ -23,8 +25,9 @@ module Torque
23
25
 
24
26
  # Allow quoted arrays to get here
25
27
  def visit_Arel_Nodes_Casted(o, collector)
26
- return super unless o.val.is_a?(::Enumerable)
27
- quote_array(o.val, collector)
28
+ value = o.respond_to?(:val) ? o.val : o.value
29
+ return super unless value.is_a?(::Enumerable)
30
+ quote_array(value, collector)
28
31
  end
29
32
 
30
33
  ## TORQUE VISITORS
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Associations
@@ -19,12 +21,14 @@ module Torque
19
21
  add_id = owner[reflection.active_record_primary_key]
20
22
  record_fk = reflection.foreign_key
21
23
 
22
- record[record_fk].push(add_id) unless (record[record_fk] ||= []).include?(add_id)
24
+ list = record[record_fk] ||= []
25
+ list.push(add_id) unless list.include?(add_id)
23
26
  end
24
27
 
25
28
  end
26
29
 
27
30
  ::ActiveRecord::Associations::Association.prepend(Association)
31
+ ::ActiveRecord::Associations::HasManyAssociation.prepend(Association)
28
32
  end
29
33
  end
30
34
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Associations
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_record/associations/collection_association'
4
+
2
5
  # FIXME: build, create
3
6
  module Torque
4
7
  module PostgreSQL
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Associations
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Associations
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Associations
@@ -38,11 +40,37 @@ module Torque
38
40
  end
39
41
  end
40
42
 
43
+ if PostgreSQL::AR610
44
+ # This is how Rails 6.1 now load the records
45
+ def load_records
46
+ return super unless connected_through_array?
47
+
48
+ @records_by_owner = {}.compare_by_identity
49
+ raw_records = owner_keys.empty? ? [] : records_for(owner_keys)
50
+
51
+ @preloaded_records = raw_records.select do |record|
52
+ assignments = false
53
+
54
+ ids = convert_key(record[association_key_name])
55
+ owners_by_key.values_at(*ids).flat_map do |owner|
56
+ entries = (@records_by_owner[owner] ||= [])
57
+
58
+ if reflection.collection? || entries.empty?
59
+ entries << record
60
+ assignments = true
61
+ end
62
+ end
63
+
64
+ assignments
65
+ end
66
+ end
67
+ end
68
+
41
69
  # Build correctly the constraint condition in order to get the
42
70
  # associated ids
43
71
  def records_for(ids, &block)
44
72
  return super unless connected_through_array?
45
- condition = scope.arel_attribute(association_key_name)
73
+ condition = scope.arel_table[association_key_name]
46
74
  condition = reflection.build_id_constraint(condition, ids.flatten.uniq)
47
75
  scope.where(condition).load(&block)
48
76
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'builder/enum'
2
4
  require_relative 'builder/period'
3
5
 
@@ -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
  module Torque
2
4
  module PostgreSQL
3
5
  module AutosaveAssociation
@@ -8,9 +10,13 @@ module Torque
8
10
  save_method = :"autosave_associated_records_for_#{reflection.name}"
9
11
  define_non_cyclic_method(save_method) { save_belongs_to_many_array(reflection) }
10
12
 
11
- before_save(:before_save_collection_association)
12
- after_save(:after_save_collection_association) if ::ActiveRecord::Base
13
- .instance_methods.include?(:after_save_collection_association)
13
+ if PostgreSQL::AR610
14
+ around_save(:around_save_collection_association)
15
+ else
16
+ before_save(:before_save_collection_association)
17
+ after_save(:after_save_collection_association) if ::ActiveRecord::Base
18
+ .instance_methods.include?(:after_save_collection_association)
19
+ end
14
20
 
15
21
  before_create(save_method)
16
22
  before_update(save_method)
@@ -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
@@ -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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Relation
@@ -34,7 +36,10 @@ module Torque
34
36
  # attributes as well
35
37
  def bound_attributes
36
38
  visitor = ::Arel::Visitors::PostgreSQL.new(ActiveRecord::Base.connection)
37
- visitor.accept(self.arel.ast, ::Arel::Collectors::Bind.new).value
39
+ visitor.accept(self.arel.ast, ::Arel::Collectors::Composite.new(
40
+ ::Arel::Collectors::SQLString.new,
41
+ ::Arel::Collectors::Bind.new,
42
+ )).value.last
38
43
  end
39
44
 
40
45
  private
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Relation
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Relation
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  module Relation
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
5
  LookupError = Class.new(ArgumentError)
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Torque
2
4
  module PostgreSQL
3
- VERSION = '2.0.3'
5
+ VERSION = '2.0.4'
4
6
  end
5
7
  end
@@ -11,7 +11,7 @@
11
11
  # It's strongly recommended that you check this file into your version control system.
12
12
 
13
13
  begin
14
- version = 61
14
+ version = 62
15
15
 
16
16
  raise SystemExit if ActiveRecord::Migrator.current_version == version
17
17
  ActiveRecord::Schema.define(version: version) do
@@ -83,7 +83,7 @@ begin
83
83
  create_table "courses", force: :cascade do |t|
84
84
  t.string "title", null: false
85
85
  t.interval "duration"
86
- t.enum "types", subtype: :types, array: true, default: [:A, :B]
86
+ t.enum "types", subtype: :types, array: true
87
87
  t.datetime "created_at", null: false
88
88
  t.datetime "updated_at", null: false
89
89
  end
@@ -25,7 +25,9 @@ RSpec.describe 'Arel' do
25
25
  klass_name = operator.to_s.camelize
26
26
 
27
27
  context "##{operator}" do
28
- let(:instance) { attribute.public_send(operator, value) }
28
+ let(:instance) do
29
+ attribute.public_send(operator, value.is_a?(Array) ? ::Arel.array(value) : value)
30
+ end
29
31
 
30
32
  context 'for attribute' do
31
33
  let(:klass) { ::Arel::Nodes.const_get(klass_name) }
@@ -48,7 +48,7 @@ RSpec.describe 'Enum' do
48
48
  expect(dump_io.string).to match checker
49
49
  end
50
50
 
51
- it 'can have a default value as an array of symbols' do
51
+ xit 'can have a default value as an array of symbols' do
52
52
  dump_io = StringIO.new
53
53
  checker = /t\.enum +"types", +default: \[:A, :B\], +array: true, +subtype: :types/
54
54
  ActiveRecord::SchemaDumper.dump(connection, dump_io)
@@ -5,7 +5,7 @@ RSpec.describe 'HasMany' do
5
5
  let(:builder) { ActiveRecord::Associations::Builder::HasMany }
6
6
 
7
7
  it 'adds the array option' do
8
- expect(builder.send(:valid_options, [])).to include(:array)
8
+ expect(builder.send(:valid_options, {})).to include(:array)
9
9
  end
10
10
  end
11
11
 
@@ -66,7 +66,7 @@ RSpec.describe 'TableInheritance' do
66
66
 
67
67
  it 'allows empty-body create table operation' do
68
68
  sql = connection.create_table(:activity_posts, inherits: :activities)
69
- result = 'CREATE TABLE "activity_posts" ()'
69
+ result = "CREATE TABLE \"activity_posts\" ()"
70
70
  result << ' INHERITS ( "activities" )'
71
71
  expect(sql).to eql(result)
72
72
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: torque-postgresql
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Silva
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-26 00:00:00.000000000 Z
11
+ date: 2020-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -333,8 +333,10 @@ test_files:
333
333
  - spec/factories/videos.rb
334
334
  - spec/tests/geometric_builder_spec.rb
335
335
  - spec/tests/range_spec.rb
336
+ - spec/tests/table_inheritance_spec.rb
336
337
  - spec/tests/enum_spec.rb
337
338
  - spec/tests/auxiliary_statement_spec.rb
339
+ - spec/tests/enum_set_spec.rb
338
340
  - spec/tests/coder_spec.rb
339
341
  - spec/tests/collector_spec.rb
340
342
  - spec/tests/distinct_on_spec.rb
@@ -342,12 +344,10 @@ test_files:
342
344
  - spec/tests/lazy_spec.rb
343
345
  - spec/tests/quoting_spec.rb
344
346
  - spec/tests/relation_spec.rb
345
- - spec/tests/arel_spec.rb
346
- - spec/tests/enum_set_spec.rb
347
347
  - spec/tests/belongs_to_many_spec.rb
348
- - spec/tests/has_many_spec.rb
349
348
  - spec/tests/period_spec.rb
350
- - spec/tests/table_inheritance_spec.rb
349
+ - spec/tests/arel_spec.rb
350
+ - spec/tests/has_many_spec.rb
351
351
  - spec/mocks/cache_query.rb
352
352
  - spec/mocks/create_table.rb
353
353
  - spec/en.yml