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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5076bbdf3cee649de6955a308c0a51425755f5c87a2e83ee4b028a5009f6960f
4
- data.tar.gz: ba7308a8927428a6496ef04b9edbd09270c303d43d0d10a9528033734a86fe2f
3
+ metadata.gz: fe8259f8bb0f4c8c67c32919ec2bc2ca448c5372857670ed5fbd4d932e3160d6
4
+ data.tar.gz: 3786a17977e0d4ffd81c1521c9e469fd411b6d15b413866941adc911beb8c2dd
5
5
  SHA512:
6
- metadata.gz: 4898d7f63ec4a7df180d3c4dcb80aab3d7269bcede0eab54a03f0c3591d314ddc025e45f755a9ae60a914d6cc4ecc8ca42011c44afbb62ec3432693a1abf8be0
7
- data.tar.gz: a4621ea2a37aa566fcb33db4cdcaaf83b4539d96ed30628f81fda79f0b02c2c2ab1b197ef6e0448cbcdc43b584e53293ca110f3d299a0c6b13b89ff4f34aefcb
6
+ metadata.gz: f35b0eb94b260cc5c5c9d08a47dcba841d9044c9acc52d30b0ad13b1e0a6ecbe2ef10ca940828870fbf5f04b387827b9c56db43523e0585f77f950a72e368b51
7
+ data.tar.gz: a3b899a4c7a26f9ff9a6a9af7f0d41a6ed193e96a76d96ae17a2423eb0b4bc6ea59138684cd324a28ccfad7374528c1cb72d4ae89a0ad710ce66655d3e355e50
@@ -18,7 +18,6 @@ require 'torque/postgresql/arel'
18
18
  require 'torque/postgresql/adapter'
19
19
  require 'torque/postgresql/associations'
20
20
  require 'torque/postgresql/attributes'
21
- require 'torque/postgresql/autosave_association'
22
21
  require 'torque/postgresql/auxiliary_statement'
23
22
  require 'torque/postgresql/base'
24
23
  require 'torque/postgresql/inheritance'
@@ -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|
@@ -49,30 +51,33 @@ module Torque
49
51
  if inherited_tables.present?
50
52
  stream.puts " # These are tables that has inheritance"
51
53
  inherited_tables.each do |table_name, inherits|
52
- unless ignored?(table_name)
53
- sub_stream = StringIO.new
54
- table(table_name, sub_stream)
55
-
56
- # Add the inherits setting
57
- sub_stream.rewind
58
- inherits.map!(&:to_sym)
59
- inherits = inherits.first if inherits.size === 1
60
- inherits = ", inherits: #{inherits.inspect} do |t|"
61
- table_dump = sub_stream.read.gsub(/ do \|t\|$/, inherits)
62
-
63
- # Ensure bodyless definitions
64
- table_dump.gsub!(/do \|t\|\n end/, '')
65
- stream.print table_dump
66
- end
54
+ next if ignored?(table_name)
55
+
56
+ sub_stream = StringIO.new
57
+ table(table_name, sub_stream)
58
+
59
+ # Add the inherits setting
60
+ sub_stream.rewind
61
+ inherits.map!(&:to_sym)
62
+ inherits = inherits.first if inherits.size === 1
63
+ inherits = ", inherits: #{inherits.inspect} do |t|"
64
+ table_dump = sub_stream.read.gsub(/ do \|t\|$/, inherits)
65
+
66
+ # Ensure bodyless definitions
67
+ table_dump.gsub!(/do \|t\|\n end/, '')
68
+ stream.print table_dump
67
69
  end
68
70
  end
69
71
 
70
- # dump foreign keys at the end to make sure all dependent tables exist.
72
+ # Dump foreign keys at the end to make sure all dependent tables exist.
71
73
  if @connection.supports_foreign_keys?
72
74
  sorted_tables.each do |tbl|
73
75
  foreign_keys(tbl, stream) unless ignored?(tbl)
74
76
  end
75
77
  end
78
+
79
+ # Scenic integration
80
+ views(stream) if defined?(::Scenic)
76
81
  end
77
82
 
78
83
  # Dump user defined types like enum
@@ -81,7 +86,7 @@ module Torque
81
86
  return unless types.any?
82
87
 
83
88
  stream.puts " # These are user-defined types used on this database"
84
- types.each { |name, type| send(type.to_sym, name, stream) }
89
+ types.sort_by(&:first).each { |(name, type)| send(type.to_sym, name, stream) }
85
90
  stream.puts
86
91
  rescue => e
87
92
  stream.puts "# Could not dump user-defined types because of following #{e.class}"
@@ -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
@@ -11,6 +13,10 @@ module Torque
11
13
  @inversed = self.target.present?
12
14
  end
13
15
 
16
+ def skip_statement_cache?(*)
17
+ super || reflection.connected_through_array?
18
+ end
19
+
14
20
  private
15
21
 
16
22
  def set_owner_attributes(record)
@@ -19,12 +25,14 @@ module Torque
19
25
  add_id = owner[reflection.active_record_primary_key]
20
26
  record_fk = reflection.foreign_key
21
27
 
22
- record[record_fk].push(add_id) unless (record[record_fk] ||= []).include?(add_id)
28
+ list = record[record_fk] ||= []
29
+ list.push(add_id) unless list.include?(add_id)
23
30
  end
24
31
 
25
32
  end
26
33
 
27
34
  ::ActiveRecord::Associations::Association.prepend(Association)
35
+ ::ActiveRecord::Associations::HasManyAssociation.prepend(Association)
28
36
  end
29
37
  end
30
38
  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
@@ -28,11 +31,11 @@ module Torque
28
31
  end
29
32
 
30
33
  def ids_reader
31
- owner[reflection.active_record_primary_key]
34
+ owner[source_attr]
32
35
  end
33
36
 
34
37
  def ids_writer(new_ids)
35
- column = reflection.active_record_primary_key
38
+ column = source_attr
36
39
  command = owner.persisted? ? :update_column : :write_attribute
37
40
  owner.public_send(command, column, new_ids.presence)
38
41
  @association_scope = nil
@@ -41,8 +44,8 @@ module Torque
41
44
  def insert_record(record, *)
42
45
  super
43
46
 
44
- attribute = (ids_reader || owner[reflection.active_record_primary_key] = [])
45
- attribute.push(record[klass_fk])
47
+ attribute = (ids_reader || owner[source_attr] = [])
48
+ attribute.push(record[klass_attr])
46
49
  record
47
50
  end
48
51
 
@@ -51,8 +54,8 @@ module Torque
51
54
  end
52
55
 
53
56
  def include?(record)
54
- list = owner[reflection.active_record_primary_key]
55
- ids_reader && ids_reader.include?(record[klass_fk])
57
+ list = owner[source_attr]
58
+ ids_reader && ids_reader.include?(record[klass_attr])
56
59
  end
57
60
 
58
61
  private
@@ -66,7 +69,7 @@ module Torque
66
69
  # When the idea is to nulligy the association, then just set the owner
67
70
  # +primary_key+ as empty
68
71
  def delete_count(method, scope, ids = nil)
69
- ids ||= scope.pluck(klass_fk)
72
+ ids ||= scope.pluck(klass_attr)
70
73
  scope.delete_all if method == :delete_all
71
74
  remove_stash_records(ids)
72
75
  end
@@ -77,13 +80,13 @@ module Torque
77
80
 
78
81
  # Deletes the records according to the <tt>:dependent</tt> option.
79
82
  def delete_records(records, method)
80
- ids = Array.wrap(records).each_with_object(klass_fk).map(&:[])
83
+ ids = Array.wrap(records).each_with_object(klass_attr).map(&:[])
81
84
 
82
85
  if method == :destroy
83
86
  records.each(&:destroy!)
84
87
  remove_stash_records(ids)
85
88
  else
86
- scope = self.scope.where(klass_fk => records)
89
+ scope = self.scope.where(klass_attr => records)
87
90
  delete_count(method, scope, ids)
88
91
  end
89
92
  end
@@ -99,10 +102,14 @@ module Torque
99
102
  ids_writer(ids_reader - Array.wrap(ids))
100
103
  end
101
104
 
102
- def klass_fk
105
+ def source_attr
103
106
  reflection.foreign_key
104
107
  end
105
108
 
109
+ def klass_attr
110
+ reflection.active_record_primary_key
111
+ end
112
+
106
113
  def difference(a, b)
107
114
  a - b
108
115
  end
@@ -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
@@ -33,16 +35,43 @@ module Torque
33
35
  ids.each { |id| records[id].concat(Array.wrap(record)) }
34
36
  end
35
37
 
38
+ records.default_proc = nil
36
39
  owners.each do |owner|
37
40
  associate_records_to_owner(owner, records[owner[owner_key_name]] || [])
38
41
  end
39
42
  end
40
43
 
44
+ if PostgreSQL::AR610
45
+ # This is how Rails 6.1 now load the records
46
+ def load_records
47
+ return super unless connected_through_array?
48
+
49
+ @records_by_owner = {}.compare_by_identity
50
+ raw_records = owner_keys.empty? ? [] : records_for(owner_keys)
51
+
52
+ @preloaded_records = raw_records.select do |record|
53
+ assignments = false
54
+
55
+ ids = convert_key(record[association_key_name])
56
+ owners_by_key.values_at(*ids).flat_map do |owner|
57
+ entries = (@records_by_owner[owner] ||= [])
58
+
59
+ if reflection.collection? || entries.empty?
60
+ entries << record
61
+ assignments = true
62
+ end
63
+ end
64
+
65
+ assignments
66
+ end
67
+ end
68
+ end
69
+
41
70
  # Build correctly the constraint condition in order to get the
42
71
  # associated ids
43
72
  def records_for(ids, &block)
44
73
  return super unless connected_through_array?
45
- condition = scope.arel_attribute(association_key_name)
74
+ condition = scope.arel_table[association_key_name]
46
75
  condition = reflection.build_id_constraint(condition, ids.flatten.uniq)
47
76
  scope.where(condition).load(&block)
48
77
  end