composite_primary_keys 12.0.5 → 13.0.0

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 (56) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +883 -862
  3. data/README.rdoc +181 -180
  4. data/lib/composite_primary_keys.rb +119 -118
  5. data/lib/composite_primary_keys/active_model/attribute_assignment.rb +19 -19
  6. data/lib/composite_primary_keys/associations/association_scope.rb +66 -68
  7. data/lib/composite_primary_keys/associations/join_dependency.rb +118 -103
  8. data/lib/composite_primary_keys/attribute_methods.rb +21 -9
  9. data/lib/composite_primary_keys/attribute_methods/primary_key.rb +0 -2
  10. data/lib/composite_primary_keys/attribute_methods/read.rb +30 -30
  11. data/lib/composite_primary_keys/attribute_methods/write.rb +35 -35
  12. data/lib/composite_primary_keys/base.rb +141 -141
  13. data/lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb +37 -22
  14. data/lib/composite_primary_keys/connection_adapters/sqlserver/database_statements.rb +44 -44
  15. data/lib/composite_primary_keys/core.rb +48 -48
  16. data/lib/composite_primary_keys/nested_attributes.rb +1 -1
  17. data/lib/composite_primary_keys/persistence.rb +82 -81
  18. data/lib/composite_primary_keys/reflection.rb +91 -29
  19. data/lib/composite_primary_keys/relation.rb +197 -193
  20. data/lib/composite_primary_keys/relation/batches.rb +16 -8
  21. data/lib/composite_primary_keys/relation/calculations.rb +104 -81
  22. data/lib/composite_primary_keys/relation/finder_methods.rb +235 -235
  23. data/lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb +39 -20
  24. data/lib/composite_primary_keys/relation/query_methods.rb +42 -42
  25. data/lib/composite_primary_keys/relation/where_clause.rb +18 -23
  26. data/lib/composite_primary_keys/table_metadata.rb +11 -0
  27. data/lib/composite_primary_keys/version.rb +8 -8
  28. data/test/abstract_unit.rb +114 -114
  29. data/test/connections/databases.ci.yml +22 -19
  30. data/test/fixtures/db_definitions/db2-create-tables.sql +112 -112
  31. data/test/fixtures/db_definitions/db2-drop-tables.sql +16 -16
  32. data/test/fixtures/db_definitions/mysql.sql +180 -180
  33. data/test/fixtures/db_definitions/oracle.drop.sql +41 -41
  34. data/test/fixtures/db_definitions/oracle.sql +199 -199
  35. data/test/fixtures/db_definitions/postgresql.sql +182 -182
  36. data/test/fixtures/db_definitions/sqlite.sql +169 -169
  37. data/test/fixtures/db_definitions/sqlserver.sql +176 -176
  38. data/test/fixtures/department.rb +16 -16
  39. data/test/fixtures/departments.yml +19 -15
  40. data/test/fixtures/employees.yml +33 -28
  41. data/test/fixtures/restaurants_suburbs.yml +10 -10
  42. data/test/fixtures/streets.yml +16 -16
  43. data/test/fixtures/suburbs.yml +14 -14
  44. data/test/fixtures/user.rb +11 -11
  45. data/test/test_associations.rb +364 -358
  46. data/test/test_attributes.rb +75 -60
  47. data/test/test_calculations.rb +49 -42
  48. data/test/test_create.rb +218 -180
  49. data/test/test_delete.rb +182 -179
  50. data/test/test_exists.rb +39 -39
  51. data/test/test_find.rb +170 -157
  52. data/test/test_ids.rb +112 -112
  53. data/test/test_nested_attributes.rb +67 -67
  54. data/test/test_update.rb +96 -96
  55. metadata +5 -5
  56. data/lib/composite_primary_keys/connection_adapters/mysql/database_statements.rb +0 -24
@@ -1,20 +1,39 @@
1
- module ActiveRecord
2
- class PredicateBuilder
3
- class AssociationQueryValue
4
- def queries
5
- # CPK
6
- if associated_table.association_join_foreign_key.is_a?(Array)
7
- if ids.is_a?(ActiveRecord::Relation)
8
- ids.map do |id|
9
- associated_table.association_join_foreign_key.zip(id.id).to_h
10
- end
11
- else
12
- [associated_table.association_join_foreign_key.zip(ids).to_h]
13
- end
14
- else
15
- [associated_table.association_join_foreign_key.to_s => ids]
16
- end
17
- end
18
- end
19
- end
20
- end
1
+ module ActiveRecord
2
+ class PredicateBuilder
3
+ class AssociationQueryValue
4
+ def queries
5
+ # CPK
6
+ if associated_table.join_foreign_key.is_a?(Array)
7
+ if ids.is_a?(ActiveRecord::Relation)
8
+ ids.map do |id|
9
+ associated_table.join_foreign_key.zip(id.id).to_h
10
+ end
11
+ else
12
+ [associated_table.join_foreign_key.zip(ids).to_h]
13
+ end
14
+ else
15
+ [associated_table.join_foreign_key => ids]
16
+ end
17
+ end
18
+
19
+ def ids
20
+ case value
21
+ when Relation
22
+ value.select_values.empty? ? value.select(primary_key) : value
23
+ when Array
24
+ value.map { |v| convert_to_id(v) }
25
+ else
26
+ # CPK
27
+ # convert_to_id(value)
28
+ if value.nil?
29
+ nil
30
+ elsif value.respond_to?(:composite?) && value.composite?
31
+ value.class.primary_keys.zip(value.id)
32
+ else
33
+ convert_to_id(value)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,43 +1,43 @@
1
- module CompositePrimaryKeys
2
- module ActiveRecord
3
- module QueryMethods
4
- def reverse_sql_order(order_query)
5
- if order_query.empty?
6
- # CPK
7
- # return [arel_attribute(primary_key).desc] if primary_key
8
-
9
- if primary_key
10
- # break apart CPKs
11
- return primary_key.map do |key|
12
- arel_attribute(key).desc
13
- end
14
- else
15
- raise IrreversibleOrderError,
16
- "Relation has no current order and table has no primary key to be used as default order"
17
- end
18
- end
19
-
20
- order_query.flat_map do |o|
21
- order_query.flat_map do |o|
22
- case o
23
- when Arel::Attribute
24
- o.desc
25
- when Arel::Nodes::Ordering
26
- o.reverse
27
- when String
28
- if does_not_support_reverse?(o)
29
- raise IrreversibleOrderError, "Order #{o.inspect} can not be reversed automatically"
30
- end
31
- o.split(",").map! do |s|
32
- s.strip!
33
- s.gsub!(/\sasc\Z/i, " DESC") || s.gsub!(/\sdesc\Z/i, " ASC") || (s << " DESC")
34
- end
35
- else
36
- o
37
- end
38
- end
39
- end
40
- end
41
- end
42
- end
1
+ module CompositePrimaryKeys
2
+ module ActiveRecord
3
+ module QueryMethods
4
+ def reverse_sql_order(order_query)
5
+ if order_query.empty?
6
+ # CPK
7
+ # return [table[primary_key].desc] if primary_key
8
+
9
+ if primary_key
10
+ # break apart CPKs
11
+ return primary_key.map do |key|
12
+ table[key].desc
13
+ end
14
+ else
15
+ raise IrreversibleOrderError,
16
+ "Relation has no current order and table has no primary key to be used as default order"
17
+ end
18
+ end
19
+
20
+ order_query.flat_map do |o|
21
+ order_query.flat_map do |o|
22
+ case o
23
+ when Arel::Attribute
24
+ o.desc
25
+ when Arel::Nodes::Ordering
26
+ o.reverse
27
+ when String
28
+ if does_not_support_reverse?(o)
29
+ raise IrreversibleOrderError, "Order #{o.inspect} can not be reversed automatically"
30
+ end
31
+ o.split(",").map! do |s|
32
+ s.strip!
33
+ s.gsub!(/\sasc\Z/i, " DESC") || s.gsub!(/\sdesc\Z/i, " ASC") || (s << " DESC")
34
+ end
35
+ else
36
+ o
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
43
  end
@@ -1,24 +1,19 @@
1
- module ActiveRecord
2
- class Relation
3
- class WhereClause
4
- def to_h(table_name = nil)
5
- equalities = equalities(predicates)
6
-
7
- # CPK Adds this line, because ours are coming in with AND->{EQUALITY, EQUALITY}
8
- equalities = predicates.grep(Arel::Nodes::And).map(&:children).flatten.grep(Arel::Nodes::Equality) if equalities.empty?
9
-
10
- if table_name
11
- equalities = equalities.select do |node|
12
- node.left.relation.name == table_name
13
- end
14
- end
15
-
16
- equalities.map { |node|
17
- name = node.left.name.to_s
18
- value = extract_node_value(node.right)
19
- [name, value]
20
- }.to_h
21
- end
22
- end
23
- end
1
+ module ActiveRecord
2
+ class Relation
3
+ class WhereClause
4
+ def to_h(table_name = nil, equality_only: false)
5
+ equalities = equalities(predicates, equality_only)
6
+
7
+ # CPK Adds this line, because ours are coming in with AND->{EQUALITY, EQUALITY}
8
+ equalities = predicates.grep(Arel::Nodes::And).map(&:children).flatten.grep(Arel::Nodes::Equality) if equalities.empty?
9
+
10
+ equalities.each_with_object({}) do |node, hash|
11
+ next if table_name&.!= node.left.relation.name
12
+ name = node.left.name.to_s
13
+ value = extract_node_value(node.right)
14
+ hash[name] = value
15
+ end
16
+ end
17
+ end
18
+ end
24
19
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ class TableMetadata # :nodoc:
5
+ def associated_with?(table_name)
6
+ # CPK
7
+ # klass&._reflect_on_association(table_name) || klass&._reflect_on_association(table_name.singularize)
8
+ klass&._reflect_on_association(table_name) || klass&._reflect_on_association(table_name.to_s.singularize)
9
+ end
10
+ end
11
+ end
@@ -1,8 +1,8 @@
1
- module CompositePrimaryKeys
2
- module VERSION
3
- MAJOR = 12
4
- MINOR = 0
5
- TINY = 5
6
- STRING = [MAJOR, MINOR, TINY].join('.')
7
- end
8
- end
1
+ module CompositePrimaryKeys
2
+ module VERSION
3
+ MAJOR = 13
4
+ MINOR = 0
5
+ TINY = 0
6
+ STRING = [MAJOR, MINOR, TINY].join('.')
7
+ end
8
+ end
@@ -1,114 +1,114 @@
1
- spec_name = ENV['ADAPTER'] || 'sqlite'
2
- require 'bundler'
3
- require 'minitest/autorun'
4
-
5
- Bundler.setup(:default, spec_name.to_sym)
6
- Bundler.require(:default, spec_name.to_sym)
7
- require 'composite_primary_keys'
8
-
9
- # Require the connection spec
10
- PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
11
- require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
12
-
13
- spec = CompositePrimaryKeys::ConnectionSpec[spec_name]
14
- puts "Loaded #{spec_name}"
15
-
16
- # And now connect to the database
17
- ActiveRecord::Base.establish_connection(spec)
18
-
19
- # Tell active record about the configuration
20
- ActiveRecord::Base.configurations = {test: spec}
21
-
22
- # Tell ActiveRecord where to find models
23
- ActiveSupport::Dependencies.autoload_paths << File.join(PROJECT_ROOT, 'test', 'fixtures')
24
-
25
- I18n.config.enforce_available_locales = true
26
-
27
- class ActiveSupport::TestCase
28
- include ActiveRecord::TestFixtures
29
-
30
- self.fixture_path = File.dirname(__FILE__) + '/fixtures/'
31
- self.use_instantiated_fixtures = false
32
- self.use_transactional_tests = true
33
- self.test_order = :random
34
-
35
- def assert_date_from_db(expected, actual, message = nil)
36
- # SQL Server doesn't have a separate column type just for dates,
37
- # so the time is in the string and incorrectly formatted
38
- if current_adapter?(:SQLServerAdapter)
39
- assert_equal expected.strftime('%Y/%m/%d 00:00:00'), actual.strftime('%Y/%m/%d 00:00:00')
40
- elsif current_adapter?(:SybaseAdapter)
41
- assert_equal expected.to_s, actual.to_date.to_s, message
42
- else
43
- assert_equal expected.to_s, actual.to_s, message
44
- end
45
- end
46
-
47
- def assert_queries(num = 1)
48
- ActiveRecord::Base.connection.class.class_eval do
49
- self.query_count = 0
50
- alias_method :execute, :execute_with_query_counting
51
- end
52
- yield
53
- ensure
54
- ActiveRecord::Base.connection.class.class_eval do
55
- alias_method :execute, :execute_without_query_counting
56
- end
57
- assert_equal num, ActiveRecord::Base.connection.query_count, '#{ActiveRecord::Base.connection.query_count} instead of #{num} queries were executed.'
58
- end
59
-
60
- def assert_no_queries(&block)
61
- assert_queries(0, &block)
62
- end
63
-
64
- cattr_accessor :classes
65
-
66
- protected
67
-
68
- def testing_with(&block)
69
- classes.keys.each do |key_test|
70
- @key_test = key_test
71
- @klass_info = classes[@key_test]
72
- @klass, @primary_keys = @klass_info[:class], @klass_info[:primary_keys]
73
- order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
74
- @first = @klass.order(order).first
75
- yield
76
- end
77
- end
78
-
79
- def first_id
80
- ids = (1..@primary_keys.length).map {|num| 1}
81
- composite? ? ids.to_composite_ids : ids.first
82
- end
83
-
84
- def composite?
85
- @key_test != :single
86
- end
87
-
88
- # Oracle metadata is in all caps.
89
- def with_quoted_identifiers(s)
90
- s.gsub(/'(\w+)'/) { |m|
91
- if ActiveRecord::Base.configurations[:test]['adapter'] =~ /oracle/i
92
- m.upcase
93
- else
94
- m
95
- end
96
- }
97
- end
98
- end
99
-
100
- def current_adapter?(type)
101
- ActiveRecord::ConnectionAdapters.const_defined?(type) &&
102
- ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type))
103
- end
104
-
105
- ActiveRecord::Base.connection.class.class_eval do
106
- cattr_accessor :query_count
107
- alias_method :execute_without_query_counting, :execute
108
- def execute_with_query_counting(sql, name = nil)
109
- self.query_count += 1
110
- execute_without_query_counting(sql, name)
111
- end
112
- end
113
-
114
- ActiveRecord::Base.logger = Logger.new(ENV['CPK_LOGFILE'] || STDOUT)
1
+ spec_name = ENV['ADAPTER'] || 'sqlite'
2
+ require 'bundler'
3
+ require 'minitest/autorun'
4
+
5
+ Bundler.setup(:default, spec_name.to_sym)
6
+ Bundler.require(:default, spec_name.to_sym)
7
+ require 'composite_primary_keys'
8
+
9
+ # Require the connection spec
10
+ PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
11
+ require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
12
+
13
+ spec = CompositePrimaryKeys::ConnectionSpec[spec_name]
14
+ puts "Loaded #{spec_name}"
15
+
16
+ # And now connect to the database
17
+ ActiveRecord::Base.establish_connection(spec)
18
+
19
+ # Tell active record about the configuration
20
+ ActiveRecord::Base.configurations = {test: spec}
21
+
22
+ # Tell ActiveRecord where to find models
23
+ ActiveSupport::Dependencies.autoload_paths << File.join(PROJECT_ROOT, 'test', 'fixtures')
24
+
25
+ I18n.config.enforce_available_locales = true
26
+
27
+ class ActiveSupport::TestCase
28
+ include ActiveRecord::TestFixtures
29
+
30
+ self.fixture_path = File.dirname(__FILE__) + '/fixtures/'
31
+ self.use_instantiated_fixtures = false
32
+ self.use_transactional_tests = true
33
+ self.test_order = :random
34
+
35
+ def assert_date_from_db(expected, actual, message = nil)
36
+ # SQL Server doesn't have a separate column type just for dates,
37
+ # so the time is in the string and incorrectly formatted
38
+ if current_adapter?(:SQLServerAdapter)
39
+ assert_equal expected.strftime('%Y/%m/%d 00:00:00'), actual.strftime('%Y/%m/%d 00:00:00')
40
+ elsif current_adapter?(:SybaseAdapter)
41
+ assert_equal expected.to_s, actual.to_date.to_s, message
42
+ else
43
+ assert_equal expected.to_s, actual.to_s, message
44
+ end
45
+ end
46
+
47
+ def assert_queries(num = 1)
48
+ ActiveRecord::Base.connection.class.class_eval do
49
+ self.query_count = 0
50
+ alias_method :execute, :execute_with_query_counting
51
+ end
52
+ yield
53
+ ensure
54
+ ActiveRecord::Base.connection.class.class_eval do
55
+ alias_method :execute, :execute_without_query_counting
56
+ end
57
+ assert_equal num, ActiveRecord::Base.connection.query_count, '#{ActiveRecord::Base.connection.query_count} instead of #{num} queries were executed.'
58
+ end
59
+
60
+ def assert_no_queries(&block)
61
+ assert_queries(0, &block)
62
+ end
63
+
64
+ cattr_accessor :classes
65
+
66
+ protected
67
+
68
+ def testing_with(&block)
69
+ classes.keys.each do |key_test|
70
+ @key_test = key_test
71
+ @klass_info = classes[@key_test]
72
+ @klass, @primary_keys = @klass_info[:class], @klass_info[:primary_keys]
73
+ order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
74
+ @first = @klass.order(order).first
75
+ yield
76
+ end
77
+ end
78
+
79
+ def first_id
80
+ ids = (1..@primary_keys.length).map {|num| 1}
81
+ composite? ? ids.to_composite_ids : ids.first
82
+ end
83
+
84
+ def composite?
85
+ @key_test != :single
86
+ end
87
+
88
+ # Oracle metadata is in all caps.
89
+ def with_quoted_identifiers(s)
90
+ s.gsub(/'(\w+)'/) { |m|
91
+ if ActiveRecord::Base.configurations[:test]['adapter'] =~ /oracle/i
92
+ m.upcase
93
+ else
94
+ m
95
+ end
96
+ }
97
+ end
98
+ end
99
+
100
+ def current_adapter?(type)
101
+ ActiveRecord::ConnectionAdapters.const_defined?(type) &&
102
+ ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type))
103
+ end
104
+
105
+ ActiveRecord::Base.connection.class.class_eval do
106
+ cattr_accessor :query_count
107
+ alias_method :execute_without_query_counting, :execute
108
+ def execute_with_query_counting(sql, name = nil)
109
+ self.query_count += 1
110
+ execute_without_query_counting(sql, name)
111
+ end
112
+ end
113
+
114
+ ActiveRecord::Base.logger = Logger.new(ENV['CPK_LOGFILE'] || STDOUT)