composite_primary_keys 14.0.3 → 14.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +49 -0
  3. data/README.rdoc +182 -182
  4. data/Rakefile +1 -1
  5. data/lib/composite_primary_keys/associations/association.rb +2 -2
  6. data/lib/composite_primary_keys/associations/association_scope.rb +1 -1
  7. data/lib/composite_primary_keys/associations/collection_association.rb +10 -3
  8. data/lib/composite_primary_keys/associations/has_many_through_association.rb +19 -0
  9. data/lib/composite_primary_keys/associations/preloader/association.rb +52 -68
  10. data/lib/composite_primary_keys/autosave_association.rb +1 -1
  11. data/lib/composite_primary_keys/composite_arrays.rb +2 -0
  12. data/lib/composite_primary_keys/composite_predicates.rb +121 -71
  13. data/lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb +1 -2
  14. data/lib/composite_primary_keys/nested_attributes.rb +2 -2
  15. data/lib/composite_primary_keys/persistence.rb +96 -96
  16. data/lib/composite_primary_keys/relation/calculations.rb +110 -110
  17. data/lib/composite_primary_keys/relation/query_methods.rb +14 -16
  18. data/lib/composite_primary_keys/relation.rb +4 -2
  19. data/lib/composite_primary_keys/validations/uniqueness.rb +10 -2
  20. data/lib/composite_primary_keys/version.rb +1 -1
  21. data/lib/composite_primary_keys.rb +117 -119
  22. data/scripts/console.rb +2 -2
  23. data/tasks/databases/trilogy.rake +23 -0
  24. data/test/abstract_unit.rb +124 -118
  25. data/test/connections/databases.ci.yml +10 -0
  26. data/test/fixtures/admin.rb +4 -0
  27. data/test/fixtures/comments.yml +6 -0
  28. data/test/fixtures/db_definitions/db2-create-tables.sql +34 -0
  29. data/test/fixtures/db_definitions/db2-drop-tables.sql +7 -1
  30. data/test/fixtures/db_definitions/mysql.sql +23 -0
  31. data/test/fixtures/db_definitions/oracle.drop.sql +4 -0
  32. data/test/fixtures/db_definitions/oracle.sql +21 -0
  33. data/test/fixtures/db_definitions/postgresql.sql +23 -0
  34. data/test/fixtures/db_definitions/sqlite.sql +21 -0
  35. data/test/fixtures/db_definitions/sqlserver.sql +23 -0
  36. data/test/fixtures/department.rb +20 -16
  37. data/test/fixtures/moderator.rb +4 -0
  38. data/test/fixtures/room.rb +4 -1
  39. data/test/fixtures/room_assignment.rb +6 -2
  40. data/test/fixtures/staff_room.rb +6 -0
  41. data/test/fixtures/staff_room_key.rb +6 -0
  42. data/test/fixtures/user.rb +3 -0
  43. data/test/fixtures/user_with_polymorphic_name.rb +9 -0
  44. data/test/test_associations.rb +403 -372
  45. data/test/test_composite_arrays.rb +6 -0
  46. data/test/test_create.rb +219 -218
  47. data/test/test_has_one_through.rb +30 -0
  48. data/test/test_nested_attributes.rb +23 -0
  49. data/test/test_polymorphic.rb +6 -0
  50. data/test/test_predicates.rb +130 -60
  51. metadata +13 -6
  52. data/lib/composite_primary_keys/associations/through_association.rb +0 -24
@@ -18,23 +18,21 @@ module CompositePrimaryKeys
18
18
  end
19
19
 
20
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
21
+ case o
22
+ when Arel::Attribute
23
+ o.desc
24
+ when Arel::Nodes::Ordering
25
+ o.reverse
26
+ when String
27
+ if does_not_support_reverse?(o)
28
+ raise IrreversibleOrderError, "Order #{o.inspect} can not be reversed automatically"
37
29
  end
30
+ o.split(",").map! do |s|
31
+ s.strip!
32
+ s.gsub!(/\sasc\Z/i, " DESC") || s.gsub!(/\sdesc\Z/i, " ASC") || (s << " DESC")
33
+ end
34
+ else
35
+ o
38
36
  end
39
37
  end
40
38
  end
@@ -29,7 +29,7 @@ module ActiveRecord
29
29
  stmt.key = table[primary_key]
30
30
 
31
31
  # CPK
32
- if @klass.composite? && @klass.connection.visitor.compile(stmt.ast) =~ /['"]#{primary_key.to_s}['"]/
32
+ if @klass.composite?
33
33
  stmt = Arel::UpdateManager.new
34
34
  stmt.table(arel_table)
35
35
  cpk_subquery(stmt)
@@ -74,7 +74,7 @@ module ActiveRecord
74
74
  stmt.key = table[primary_key]
75
75
 
76
76
  # CPK
77
- if @klass.composite? && @klass.connection.visitor.compile(stmt.ast) =~ /['"]#{primary_key.to_s}['"]/
77
+ if @klass.composite?
78
78
  stmt = Arel::DeleteManager.new
79
79
  stmt.from(arel_table)
80
80
  cpk_subquery(stmt)
@@ -102,6 +102,8 @@ module ActiveRecord
102
102
  # database adapter to decide how to proceed.
103
103
  if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) && connection.is_a?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
104
104
  cpk_mysql_subquery(stmt)
105
+ elsif defined?(ActiveRecord::ConnectionAdapters::TrilogyAdapter) && connection.is_a?(ActiveRecord::ConnectionAdapters::TrilogyAdapter)
106
+ cpk_mysql_subquery(stmt)
105
107
  elsif defined?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) && connection.is_a?(ActiveRecord::ConnectionAdapters::SQLServerAdapter)
106
108
  cpk_exists_subquery(stmt)
107
109
  else
@@ -18,7 +18,15 @@ module ActiveRecord
18
18
  end
19
19
  end
20
20
  relation = scope_relation(record, relation)
21
- relation = relation.merge(options[:conditions]) if options[:conditions]
21
+ if options[:conditions]
22
+ conditions = options[:conditions]
23
+
24
+ relation = if conditions.arity.zero?
25
+ relation.instance_exec(&conditions)
26
+ else
27
+ relation.instance_exec(record, &conditions)
28
+ end
29
+ end
22
30
 
23
31
  if relation.exists?
24
32
  error_options = options.except(:case_sensitive, :scope, :conditions)
@@ -29,4 +37,4 @@ module ActiveRecord
29
37
  end
30
38
  end
31
39
  end
32
- end
40
+ end
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION
3
3
  MAJOR = 14
4
4
  MINOR = 0
5
- TINY = 3
5
+ TINY = 9
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -1,119 +1,117 @@
1
- #--
2
- # Copyright (c) 2006-2016 Nic Williams and Charlie Savage
3
- #
4
- # Permission is hereby granted, free of charge, to any person obtaining
5
- # a copy of this software and associated documentation files (the
6
- # "Software"), to deal in the Software without restriction, including
7
- # without limitation the rights to use, copy, modify, merge, publish,
8
- # distribute, sublicense, and/or sell copies of the Software, and to
9
- # permit persons to whom the Software is furnished to do so, subject to
10
- # the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be
13
- # included in all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
- #++
23
-
24
- unless defined?(ActiveRecord)
25
- require 'rubygems'
26
- gem 'activerecord', '~>7.0.0', '>= 7.0.1'
27
- require 'active_record'
28
- end
29
-
30
- # ActiveModel files we override
31
- # _write_attribute
32
- require 'active_model/attribute_assignment'
33
-
34
- # ActiveRecord files we override
35
- require 'active_record/attribute_methods'
36
- require 'active_record/autosave_association'
37
- require 'active_record/counter_cache'
38
- require 'active_record/fixtures'
39
- require 'active_record/model_schema'
40
- require 'active_record/persistence'
41
- require 'active_record/reflection'
42
- require 'active_record/relation'
43
- require 'active_record/sanitization'
44
- require 'active_record/transactions'
45
-
46
- require 'active_record/associations/association'
47
- require 'active_record/associations/association_scope'
48
- require 'active_record/associations/foreign_association'
49
- require 'active_record/associations/has_many_association'
50
- require 'active_record/associations/has_many_through_association'
51
- require 'active_record/associations/join_dependency'
52
- require 'active_record/associations/preloader/association'
53
- require 'active_record/associations/singular_association'
54
- require 'active_record/associations/collection_association'
55
- require 'active_record/associations/through_association'
56
-
57
- require 'active_record/attribute_methods/primary_key'
58
- require 'active_record/attribute_methods/read'
59
- require 'active_record/attribute_methods/write'
60
- require 'active_record/nested_attributes'
61
-
62
- require 'active_record/connection_adapters/abstract/database_statements'
63
- require 'active_record/connection_adapters/abstract_adapter'
64
- require 'active_record/connection_adapters/postgresql/database_statements'
65
-
66
- require 'active_record/relation/where_clause'
67
- require 'active_record/table_metadata'
68
-
69
- # CPK overrides
70
- require_relative 'composite_primary_keys/active_model/attribute_assignment'
71
- require_relative 'composite_primary_keys/attribute_methods'
72
- require_relative 'composite_primary_keys/autosave_association'
73
- require_relative 'composite_primary_keys/persistence'
74
- require_relative 'composite_primary_keys/base'
75
- require_relative 'composite_primary_keys/core'
76
- require_relative 'composite_primary_keys/composite_arrays'
77
- require_relative 'composite_primary_keys/composite_predicates'
78
- require_relative 'composite_primary_keys/counter_cache'
79
- require_relative 'composite_primary_keys/fixtures'
80
- require_relative 'composite_primary_keys/reflection'
81
- require_relative 'composite_primary_keys/relation'
82
- require_relative 'composite_primary_keys/sanitization'
83
- require_relative 'composite_primary_keys/transactions'
84
- require_relative 'composite_primary_keys/version'
85
-
86
- require_relative 'composite_primary_keys/associations/association'
87
- require_relative 'composite_primary_keys/associations/association_scope'
88
- require_relative 'composite_primary_keys/associations/foreign_association'
89
- require_relative 'composite_primary_keys/associations/has_many_association'
90
- require_relative 'composite_primary_keys/associations/has_many_through_association'
91
- require_relative 'composite_primary_keys/associations/join_association'
92
- require_relative 'composite_primary_keys/associations/preloader/association'
93
- require_relative 'composite_primary_keys/associations/collection_association'
94
- require_relative 'composite_primary_keys/associations/through_association'
95
-
96
- require_relative 'composite_primary_keys/attribute_methods/primary_key'
97
- require_relative 'composite_primary_keys/attribute_methods/read'
98
- require_relative 'composite_primary_keys/attribute_methods/write'
99
- require_relative 'composite_primary_keys/nested_attributes'
100
-
101
- require_relative 'composite_primary_keys/connection_adapters/abstract/database_statements'
102
- require_relative 'composite_primary_keys/connection_adapters/abstract_adapter'
103
- require_relative 'composite_primary_keys/connection_adapters/postgresql/database_statements'
104
- require_relative 'composite_primary_keys/connection_adapters/sqlserver/database_statements'
105
-
106
- require_relative 'composite_primary_keys/relation/batches'
107
- require_relative 'composite_primary_keys/relation/where_clause'
108
- require_relative 'composite_primary_keys/relation/calculations'
109
- require_relative 'composite_primary_keys/relation/finder_methods'
110
- require_relative 'composite_primary_keys/relation/predicate_builder/association_query_value'
111
- require_relative 'composite_primary_keys/relation/query_methods'
112
-
113
- require_relative 'composite_primary_keys/validations/uniqueness'
114
-
115
- require_relative 'composite_primary_keys/composite_relation'
116
-
117
- require_relative 'composite_primary_keys/arel/to_sql'
118
- require_relative 'composite_primary_keys/arel/sqlserver'
119
- require_relative 'composite_primary_keys/table_metadata'
1
+ #--
2
+ # Copyright (c) 2006-2016 Nic Williams and Charlie Savage
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ unless defined?(ActiveRecord)
25
+ require 'rubygems'
26
+ gem 'activerecord', '~>7.0.0', '>= 7.0.1'
27
+ require 'active_record'
28
+ end
29
+
30
+ # ActiveModel files we override
31
+ # _write_attribute
32
+ require 'active_model/attribute_assignment'
33
+
34
+ # ActiveRecord files we override
35
+ require 'active_record/attribute_methods'
36
+ require 'active_record/autosave_association'
37
+ require 'active_record/counter_cache'
38
+ require 'active_record/fixtures'
39
+ require 'active_record/model_schema'
40
+ require 'active_record/persistence'
41
+ require 'active_record/reflection'
42
+ require 'active_record/relation'
43
+ require 'active_record/sanitization'
44
+ require 'active_record/transactions'
45
+
46
+ require 'active_record/associations/association'
47
+ require 'active_record/associations/association_scope'
48
+ require 'active_record/associations/foreign_association'
49
+ require 'active_record/associations/has_many_association'
50
+ require 'active_record/associations/has_many_through_association'
51
+ require 'active_record/associations/join_dependency'
52
+ require 'active_record/associations/preloader/association'
53
+ require 'active_record/associations/singular_association'
54
+ require 'active_record/associations/collection_association'
55
+
56
+ require 'active_record/attribute_methods/primary_key'
57
+ require 'active_record/attribute_methods/read'
58
+ require 'active_record/attribute_methods/write'
59
+ require 'active_record/nested_attributes'
60
+
61
+ require 'active_record/connection_adapters/abstract/database_statements'
62
+ require 'active_record/connection_adapters/abstract_adapter'
63
+ require 'active_record/connection_adapters/postgresql/database_statements'
64
+
65
+ require 'active_record/relation/where_clause'
66
+ require 'active_record/table_metadata'
67
+
68
+ # CPK overrides
69
+ require_relative 'composite_primary_keys/active_model/attribute_assignment'
70
+ require_relative 'composite_primary_keys/attribute_methods'
71
+ require_relative 'composite_primary_keys/autosave_association'
72
+ require_relative 'composite_primary_keys/persistence'
73
+ require_relative 'composite_primary_keys/base'
74
+ require_relative 'composite_primary_keys/core'
75
+ require_relative 'composite_primary_keys/composite_arrays'
76
+ require_relative 'composite_primary_keys/composite_predicates'
77
+ require_relative 'composite_primary_keys/counter_cache'
78
+ require_relative 'composite_primary_keys/fixtures'
79
+ require_relative 'composite_primary_keys/reflection'
80
+ require_relative 'composite_primary_keys/relation'
81
+ require_relative 'composite_primary_keys/sanitization'
82
+ require_relative 'composite_primary_keys/transactions'
83
+ require_relative 'composite_primary_keys/version'
84
+
85
+ require_relative 'composite_primary_keys/associations/association'
86
+ require_relative 'composite_primary_keys/associations/association_scope'
87
+ require_relative 'composite_primary_keys/associations/foreign_association'
88
+ require_relative 'composite_primary_keys/associations/has_many_association'
89
+ require_relative 'composite_primary_keys/associations/has_many_through_association'
90
+ require_relative 'composite_primary_keys/associations/join_association'
91
+ require_relative 'composite_primary_keys/associations/preloader/association'
92
+ require_relative 'composite_primary_keys/associations/collection_association'
93
+
94
+ require_relative 'composite_primary_keys/attribute_methods/primary_key'
95
+ require_relative 'composite_primary_keys/attribute_methods/read'
96
+ require_relative 'composite_primary_keys/attribute_methods/write'
97
+ require_relative 'composite_primary_keys/nested_attributes'
98
+
99
+ require_relative 'composite_primary_keys/connection_adapters/abstract/database_statements'
100
+ require_relative 'composite_primary_keys/connection_adapters/abstract_adapter'
101
+ require_relative 'composite_primary_keys/connection_adapters/postgresql/database_statements'
102
+ require_relative 'composite_primary_keys/connection_adapters/sqlserver/database_statements'
103
+
104
+ require_relative 'composite_primary_keys/relation/batches'
105
+ require_relative 'composite_primary_keys/relation/where_clause'
106
+ require_relative 'composite_primary_keys/relation/calculations'
107
+ require_relative 'composite_primary_keys/relation/finder_methods'
108
+ require_relative 'composite_primary_keys/relation/predicate_builder/association_query_value'
109
+ require_relative 'composite_primary_keys/relation/query_methods'
110
+
111
+ require_relative 'composite_primary_keys/validations/uniqueness'
112
+
113
+ require_relative 'composite_primary_keys/composite_relation'
114
+
115
+ require_relative 'composite_primary_keys/arel/to_sql'
116
+ require_relative 'composite_primary_keys/arel/sqlserver'
117
+ require_relative 'composite_primary_keys/table_metadata'
data/scripts/console.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #
4
- # if run as script, load the file as library while starting irb
4
+ # if run as script, load the file as library while starting irb
5
5
  #
6
6
  if __FILE__ == $0
7
7
  irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
@@ -12,7 +12,7 @@ end
12
12
  #
13
13
  # check if the given adapter is supported (default: mysql)
14
14
  #
15
- adapters = %w[mysql sqlite oracle oracle_enhanced postgresql ibm_db]
15
+ adapters = %w[mysql sqlite oracle oracle_enhanced postgresql ibm_db trilogy]
16
16
  adapter = ENV['ADAPTER'] || 'mysql'
17
17
  unless adapters.include? adapter
18
18
  puts "Usage: #{__FILE__} <adapter>"
@@ -0,0 +1,23 @@
1
+ namespace :trilogy do
2
+ task :setup do
3
+ require 'bundler'
4
+ Bundler.require(:default, :trilogy)
5
+ end
6
+
7
+ task :create_database => :setup do
8
+ Rake::Task["mysql:create_database"].invoke
9
+ end
10
+
11
+ desc 'Build the MySQL test database'
12
+ task :build_database => [:create_database] do
13
+ Rake::Task["mysql:build_database"].invoke
14
+ end
15
+
16
+ desc 'Drop the MySQL test database'
17
+ task :drop_database => :setup do
18
+ Rake::Task["mysql:drop_database"].invoke
19
+ end
20
+
21
+ desc 'Rebuild the MySQL test database'
22
+ task :rebuild_database => [:drop_database, :build_database]
23
+ end
@@ -1,118 +1,124 @@
1
- spec_name = ENV['ADAPTER'] || 'postgresql'
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
- Dir[File.join(PROJECT_ROOT, 'test', 'fixtures', '*.rb')].each do |file_path|
25
- require_file = file_path.sub(PROJECT_ROOT, '..').sub('.rb', '')
26
- require_relative require_file
27
- end
28
-
29
- I18n.config.enforce_available_locales = true
30
-
31
- class ActiveSupport::TestCase
32
- include ActiveRecord::TestFixtures
33
-
34
- self.fixture_path = File.dirname(__FILE__) + '/fixtures/'
35
- self.use_instantiated_fixtures = false
36
- self.use_transactional_tests = true
37
- self.test_order = :random
38
-
39
- def assert_date_from_db(expected, actual, message = nil)
40
- # SQL Server doesn't have a separate column type just for dates,
41
- # so the time is in the string and incorrectly formatted
42
- if current_adapter?(:SQLServerAdapter)
43
- assert_equal expected.strftime('%Y/%m/%d 00:00:00'), actual.strftime('%Y/%m/%d 00:00:00')
44
- elsif current_adapter?(:SybaseAdapter)
45
- assert_equal expected.to_s, actual.to_date.to_s, message
46
- else
47
- assert_equal expected.to_s, actual.to_s, message
48
- end
49
- end
50
-
51
- def assert_queries(num = 1)
52
- ActiveRecord::Base.connection.class.class_eval do
53
- self.query_count = 0
54
- alias_method :execute, :execute_with_query_counting
55
- end
56
- yield
57
- ensure
58
- ActiveRecord::Base.connection.class.class_eval do
59
- alias_method :execute, :execute_without_query_counting
60
- end
61
- assert_equal num, ActiveRecord::Base.connection.query_count, '#{ActiveRecord::Base.connection.query_count} instead of #{num} queries were executed.'
62
- end
63
-
64
- def assert_no_queries(&block)
65
- assert_queries(0, &block)
66
- end
67
-
68
- cattr_accessor :classes
69
-
70
- protected
71
-
72
- def testing_with(&block)
73
- classes.keys.each do |key_test|
74
- @key_test = key_test
75
- @klass_info = classes[@key_test]
76
- @klass, @primary_keys = @klass_info[:class], @klass_info[:primary_keys]
77
- order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
78
- @first = @klass.order(order).first
79
- yield
80
- end
81
- end
82
-
83
- def first_id
84
- ids = (1..@primary_keys.length).map {|num| 1}
85
- composite? ? ids.to_composite_ids : ids.first
86
- end
87
-
88
- def composite?
89
- @key_test != :single
90
- end
91
-
92
- # Oracle metadata is in all caps.
93
- def with_quoted_identifiers(s)
94
- s.gsub(/'(\w+)'/) { |m|
95
- if ActiveRecord::Base.configurations[:test]['adapter'] =~ /oracle/i
96
- m.upcase
97
- else
98
- m
99
- end
100
- }
101
- end
102
- end
103
-
104
- def current_adapter?(type)
105
- ActiveRecord::ConnectionAdapters.const_defined?(type) &&
106
- ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type))
107
- end
108
-
109
- ActiveRecord::Base.connection.class.class_eval do
110
- cattr_accessor :query_count
111
- alias_method :execute_without_query_counting, :execute
112
- def execute_with_query_counting(sql, name = nil)
113
- self.query_count += 1
114
- execute_without_query_counting(sql, name)
115
- end
116
- end
117
-
118
- ActiveRecord::Base.logger = Logger.new(ENV['CPK_LOGFILE'] || STDOUT)
1
+ spec_name = ENV['ADAPTER'] || 'postgresql'
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
+ if spec_name == "trilogy"
18
+ require "activerecord-trilogy-adapter"
19
+ require "trilogy_adapter/connection"
20
+ ActiveRecord::Base.extend TrilogyAdapter::Connection
21
+ end
22
+
23
+ ActiveRecord::Base.establish_connection(spec)
24
+
25
+ # Tell active record about the configuration
26
+ ActiveRecord::Base.configurations = {test: spec}
27
+
28
+ # Tell ActiveRecord where to find models
29
+ ActiveSupport::Dependencies.autoload_paths << File.join(PROJECT_ROOT, 'test', 'fixtures')
30
+ Dir[File.join(PROJECT_ROOT, 'test', 'fixtures', '*.rb')].each do |file_path|
31
+ require_file = file_path.sub(PROJECT_ROOT, '..').sub('.rb', '')
32
+ require_relative require_file
33
+ end
34
+
35
+ I18n.config.enforce_available_locales = true
36
+
37
+ class ActiveSupport::TestCase
38
+ include ActiveRecord::TestFixtures
39
+
40
+ self.fixture_path = File.dirname(__FILE__) + '/fixtures/'
41
+ self.use_instantiated_fixtures = false
42
+ self.use_transactional_tests = true
43
+ self.test_order = :random
44
+
45
+ def assert_date_from_db(expected, actual, message = nil)
46
+ # SQL Server doesn't have a separate column type just for dates,
47
+ # so the time is in the string and incorrectly formatted
48
+ if current_adapter?(:SQLServerAdapter)
49
+ assert_equal expected.strftime('%Y/%m/%d 00:00:00'), actual.strftime('%Y/%m/%d 00:00:00')
50
+ elsif current_adapter?(:SybaseAdapter)
51
+ assert_equal expected.to_s, actual.to_date.to_s, message
52
+ else
53
+ assert_equal expected.to_s, actual.to_s, message
54
+ end
55
+ end
56
+
57
+ def assert_queries(num = 1)
58
+ ActiveRecord::Base.connection.class.class_eval do
59
+ self.query_count = 0
60
+ alias_method :execute, :execute_with_query_counting
61
+ end
62
+ yield
63
+ ensure
64
+ ActiveRecord::Base.connection.class.class_eval do
65
+ alias_method :execute, :execute_without_query_counting
66
+ end
67
+ assert_equal num, ActiveRecord::Base.connection.query_count, '#{ActiveRecord::Base.connection.query_count} instead of #{num} queries were executed.'
68
+ end
69
+
70
+ def assert_no_queries(&block)
71
+ assert_queries(0, &block)
72
+ end
73
+
74
+ cattr_accessor :classes
75
+
76
+ protected
77
+
78
+ def testing_with(&block)
79
+ classes.keys.each do |key_test|
80
+ @key_test = key_test
81
+ @klass_info = classes[@key_test]
82
+ @klass, @primary_keys = @klass_info[:class], @klass_info[:primary_keys]
83
+ order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
84
+ @first = @klass.order(order).first
85
+ yield
86
+ end
87
+ end
88
+
89
+ def first_id
90
+ ids = (1..@primary_keys.length).map {|num| 1}
91
+ composite? ? ids.to_composite_ids : ids.first
92
+ end
93
+
94
+ def composite?
95
+ @key_test != :single
96
+ end
97
+
98
+ # Oracle metadata is in all caps.
99
+ def with_quoted_identifiers(s)
100
+ s.gsub(/'(\w+)'/) { |m|
101
+ if ActiveRecord::Base.configurations[:test]['adapter'] =~ /oracle/i
102
+ m.upcase
103
+ else
104
+ m
105
+ end
106
+ }
107
+ end
108
+ end
109
+
110
+ def current_adapter?(type)
111
+ ActiveRecord::ConnectionAdapters.const_defined?(type) &&
112
+ ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type))
113
+ end
114
+
115
+ ActiveRecord::Base.connection.class.class_eval do
116
+ cattr_accessor :query_count
117
+ alias_method :execute_without_query_counting, :execute
118
+ def execute_with_query_counting(sql, name = nil)
119
+ self.query_count += 1
120
+ execute_without_query_counting(sql, name)
121
+ end
122
+ end
123
+
124
+ ActiveRecord::Base.logger = Logger.new(ENV['CPK_LOGFILE'] || STDOUT)
@@ -20,3 +20,13 @@ sqlite:
20
20
  adapter: sqlite3
21
21
  database: <%= File.join(project_root, 'db', 'composite_primary_keys_unittest.sqlite') %>
22
22
 
23
+ trilogy:
24
+ adapter: trilogy
25
+ username: github
26
+ password: github
27
+ host: 127.0.0.1
28
+ port: 3306
29
+ encoding: utf8mb4
30
+ charset: utf8mb4
31
+ collation: utf8mb4_bin
32
+ database: composite_primary_keys_unittest
@@ -0,0 +1,4 @@
1
+ class Admin < ActiveRecord::Base
2
+ belongs_to :moderator, :foreign_key => :id, :inverse_of => :admin
3
+ has_one :user, :through => :moderator
4
+ end