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.
- checksums.yaml +4 -4
- data/History.rdoc +49 -0
- data/README.rdoc +182 -182
- data/Rakefile +1 -1
- data/lib/composite_primary_keys/associations/association.rb +2 -2
- data/lib/composite_primary_keys/associations/association_scope.rb +1 -1
- data/lib/composite_primary_keys/associations/collection_association.rb +10 -3
- data/lib/composite_primary_keys/associations/has_many_through_association.rb +19 -0
- data/lib/composite_primary_keys/associations/preloader/association.rb +52 -68
- data/lib/composite_primary_keys/autosave_association.rb +1 -1
- data/lib/composite_primary_keys/composite_arrays.rb +2 -0
- data/lib/composite_primary_keys/composite_predicates.rb +121 -71
- data/lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb +1 -2
- data/lib/composite_primary_keys/nested_attributes.rb +2 -2
- data/lib/composite_primary_keys/persistence.rb +96 -96
- data/lib/composite_primary_keys/relation/calculations.rb +110 -110
- data/lib/composite_primary_keys/relation/query_methods.rb +14 -16
- data/lib/composite_primary_keys/relation.rb +4 -2
- data/lib/composite_primary_keys/validations/uniqueness.rb +10 -2
- data/lib/composite_primary_keys/version.rb +1 -1
- data/lib/composite_primary_keys.rb +117 -119
- data/scripts/console.rb +2 -2
- data/tasks/databases/trilogy.rake +23 -0
- data/test/abstract_unit.rb +124 -118
- data/test/connections/databases.ci.yml +10 -0
- data/test/fixtures/admin.rb +4 -0
- data/test/fixtures/comments.yml +6 -0
- data/test/fixtures/db_definitions/db2-create-tables.sql +34 -0
- data/test/fixtures/db_definitions/db2-drop-tables.sql +7 -1
- data/test/fixtures/db_definitions/mysql.sql +23 -0
- data/test/fixtures/db_definitions/oracle.drop.sql +4 -0
- data/test/fixtures/db_definitions/oracle.sql +21 -0
- data/test/fixtures/db_definitions/postgresql.sql +23 -0
- data/test/fixtures/db_definitions/sqlite.sql +21 -0
- data/test/fixtures/db_definitions/sqlserver.sql +23 -0
- data/test/fixtures/department.rb +20 -16
- data/test/fixtures/moderator.rb +4 -0
- data/test/fixtures/room.rb +4 -1
- data/test/fixtures/room_assignment.rb +6 -2
- data/test/fixtures/staff_room.rb +6 -0
- data/test/fixtures/staff_room_key.rb +6 -0
- data/test/fixtures/user.rb +3 -0
- data/test/fixtures/user_with_polymorphic_name.rb +9 -0
- data/test/test_associations.rb +403 -372
- data/test/test_composite_arrays.rb +6 -0
- data/test/test_create.rb +219 -218
- data/test/test_has_one_through.rb +30 -0
- data/test/test_nested_attributes.rb +23 -0
- data/test/test_polymorphic.rb +6 -0
- data/test/test_predicates.rb +130 -60
- metadata +13 -6
- 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
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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?
|
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?
|
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
|
-
|
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
|
@@ -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
|
-
|
56
|
-
|
57
|
-
require 'active_record/attribute_methods/
|
58
|
-
require 'active_record/attribute_methods/
|
59
|
-
require 'active_record/
|
60
|
-
|
61
|
-
|
62
|
-
require 'active_record/connection_adapters/
|
63
|
-
require 'active_record/connection_adapters/
|
64
|
-
|
65
|
-
|
66
|
-
require 'active_record/
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
require_relative 'composite_primary_keys/
|
71
|
-
require_relative 'composite_primary_keys/
|
72
|
-
require_relative 'composite_primary_keys/
|
73
|
-
require_relative 'composite_primary_keys/
|
74
|
-
require_relative 'composite_primary_keys/
|
75
|
-
require_relative 'composite_primary_keys/
|
76
|
-
require_relative 'composite_primary_keys/
|
77
|
-
require_relative 'composite_primary_keys/
|
78
|
-
require_relative 'composite_primary_keys/
|
79
|
-
require_relative 'composite_primary_keys/
|
80
|
-
require_relative 'composite_primary_keys/
|
81
|
-
require_relative 'composite_primary_keys/
|
82
|
-
require_relative 'composite_primary_keys/
|
83
|
-
require_relative 'composite_primary_keys/
|
84
|
-
|
85
|
-
|
86
|
-
require_relative 'composite_primary_keys/associations/
|
87
|
-
require_relative 'composite_primary_keys/associations/
|
88
|
-
require_relative 'composite_primary_keys/associations/
|
89
|
-
require_relative 'composite_primary_keys/associations/
|
90
|
-
require_relative 'composite_primary_keys/associations/
|
91
|
-
require_relative 'composite_primary_keys/associations/
|
92
|
-
require_relative 'composite_primary_keys/associations/
|
93
|
-
|
94
|
-
require_relative 'composite_primary_keys/
|
95
|
-
|
96
|
-
require_relative 'composite_primary_keys/attribute_methods/
|
97
|
-
require_relative 'composite_primary_keys/
|
98
|
-
|
99
|
-
require_relative 'composite_primary_keys/
|
100
|
-
|
101
|
-
require_relative 'composite_primary_keys/connection_adapters/
|
102
|
-
require_relative 'composite_primary_keys/connection_adapters/
|
103
|
-
|
104
|
-
require_relative 'composite_primary_keys/
|
105
|
-
|
106
|
-
require_relative 'composite_primary_keys/relation/
|
107
|
-
require_relative 'composite_primary_keys/relation/
|
108
|
-
require_relative 'composite_primary_keys/relation/
|
109
|
-
require_relative 'composite_primary_keys/relation/
|
110
|
-
|
111
|
-
require_relative 'composite_primary_keys/
|
112
|
-
|
113
|
-
require_relative 'composite_primary_keys/
|
114
|
-
|
115
|
-
require_relative 'composite_primary_keys/
|
116
|
-
|
117
|
-
require_relative 'composite_primary_keys/
|
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
|
data/test/abstract_unit.rb
CHANGED
@@ -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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
ActiveRecord::Base.
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
ActiveRecord::Base.connection.class.class_eval do
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
end
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
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
|