composite_primary_keys 14.0.9 → 14.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.rdoc +10 -0
- data/README.rdoc +182 -182
- data/Rakefile +37 -37
- data/lib/composite_primary_keys/associations/collection_association.rb +38 -38
- data/lib/composite_primary_keys/associations/preloader/association.rb +52 -52
- data/lib/composite_primary_keys/autosave_association.rb +60 -60
- data/lib/composite_primary_keys/composite_arrays.rb +88 -88
- data/lib/composite_primary_keys/composite_predicates.rb +121 -121
- data/lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb +36 -36
- data/lib/composite_primary_keys/core.rb +71 -48
- data/lib/composite_primary_keys/persistence.rb +96 -96
- data/lib/composite_primary_keys/reflection.rb +93 -91
- data/lib/composite_primary_keys/relation/calculations.rb +110 -110
- data/lib/composite_primary_keys/relation/query_methods.rb +40 -40
- data/lib/composite_primary_keys/relation.rb +199 -199
- data/lib/composite_primary_keys/validations/uniqueness.rb +40 -40
- data/lib/composite_primary_keys/version.rb +1 -1
- data/lib/composite_primary_keys.rb +117 -117
- data/scripts/console.rb +48 -48
- data/tasks/databases/trilogy.rake +23 -23
- data/test/abstract_unit.rb +124 -124
- data/test/connections/databases.ci.yml +32 -32
- data/test/fixtures/admin.rb +4 -4
- data/test/fixtures/db_definitions/db2-create-tables.sql +146 -146
- data/test/fixtures/db_definitions/db2-drop-tables.sql +23 -23
- data/test/fixtures/db_definitions/mysql.sql +203 -203
- data/test/fixtures/db_definitions/oracle.drop.sql +45 -45
- data/test/fixtures/db_definitions/oracle.sql +220 -220
- data/test/fixtures/db_definitions/postgresql.sql +205 -205
- data/test/fixtures/db_definitions/sqlite.sql +190 -190
- data/test/fixtures/db_definitions/sqlserver.sql +199 -199
- data/test/fixtures/department.rb +20 -20
- data/test/fixtures/moderator.rb +4 -4
- data/test/fixtures/room.rb +14 -14
- data/test/fixtures/room_assignment.rb +18 -18
- data/test/fixtures/staff_room.rb +6 -6
- data/test/fixtures/staff_room_key.rb +6 -6
- data/test/fixtures/user.rb +14 -14
- data/test/test_associations.rb +403 -403
- data/test/test_composite_arrays.rb +44 -44
- data/test/test_equal.rb +55 -26
- data/test/test_has_one_through.rb +30 -30
- data/test/test_hash.rb +73 -0
- data/test/test_nested_attributes.rb +90 -90
- metadata +7 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3146a5b7d713e297763cc29532c681192b42f3e4d79c140490ea1d48d5c82764
|
4
|
+
data.tar.gz: 276876230cdda1766bcdc830275919524826883a561870a24ab8785dc723912e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d99a8574f1438b22d1d10406eba3ca91854d932da8f6f5f3ff0057ad56929ecafc8398a9e7c53d05e8ab05090a2c3b69456270dd7dc1f5e1f7287b37ca30432
|
7
|
+
data.tar.gz: 44a624f1c0ff8de168b42adad92d082308fa9cd9c8bc536bdb126555e137bee2882c97d89e41a69fdff4a588a0747cd25eb8e11b88c452892bc0e740b4d311b1
|
data/History.rdoc
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
== 14.0.10 (2024-12-19)
|
2
|
+
* Fix `update_all` to update loaded associations (Piotr Kowalski)
|
3
|
+
* Fix object comparison for models with composite keys (Piotr Kowalski)
|
4
|
+
* Add changelog to gemfile (Berkan Unal)
|
5
|
+
* Fix fk scope removes join query (Henrique Tatagiba)
|
6
|
+
|
1
7
|
== 14.0.9 (2023-12-03)
|
2
8
|
* Fix Error for Array-Based Composite Keys in nested_attribute (T.S.)
|
3
9
|
|
@@ -44,6 +50,10 @@ more frequently. (Andrew Kiellor)
|
|
44
50
|
== 14.0.0 (2022-01-9)
|
45
51
|
* Update to ActiveRecord 7.0 (Sammy Larbi)
|
46
52
|
|
53
|
+
== 13.0.9 (2024-12-19)
|
54
|
+
* Fix `update_all` to update loaded associations (Piotr Kowalski)
|
55
|
+
* Fix object comparison for models with composite keys (Piotr Kowalski)
|
56
|
+
|
47
57
|
==13.0.8 (2023-11-24)
|
48
58
|
Fix ‘polymorphic_name` support (Vladimir Kochnev)
|
49
59
|
|
data/README.rdoc
CHANGED
@@ -1,182 +1,182 @@
|
|
1
|
-
= Composite Primary Keys for ActiveRecords
|
2
|
-
|
3
|
-
== Summary
|
4
|
-
|
5
|
-
ActiveRecord infamously doesn't support composite primary keys.
|
6
|
-
This gem, composite_primary_keys, or CPK for short, extends ActiveRecord
|
7
|
-
to support composite keys.
|
8
|
-
|
9
|
-
== Installation
|
10
|
-
|
11
|
-
gem install composite_primary_keys
|
12
|
-
|
13
|
-
If you are using Rails add the following to your Gemfile:
|
14
|
-
|
15
|
-
gem 'composite_primary_keys', '=x.x.x' (see next section about what version to use)
|
16
|
-
|
17
|
-
== Versions
|
18
|
-
|
19
|
-
Every major version of ActiveRecord has included numerous internal changes. As a result,
|
20
|
-
CPK has to be rewritten for each version of ActiveRecord. To help keep
|
21
|
-
things straight, here is the mapping:
|
22
|
-
|
23
|
-
Version 14.x is designed to work with ActiveRecord 7.0.x
|
24
|
-
Version 13.x is designed to work with ActiveRecord 6.1.x
|
25
|
-
Version 12.x is designed to work with ActiveRecord 6.0.x
|
26
|
-
Version 11.x is designed to work with ActiveRecord 5.2.x
|
27
|
-
Version 10.x is designed to work with ActiveRecord 5.1.x
|
28
|
-
Version 9.x is designed to work with ActiveRecord 5.0.x
|
29
|
-
Version 8.x is designed to work with ActiveRecord 4.2.x
|
30
|
-
Version 7.x is designed to work with ActiveRecord 4.1.x
|
31
|
-
Version 6.x is designed to work with ActiveRecord 4.0.x
|
32
|
-
Version 5.x is designed to work with ActiveRecord 3.2.x
|
33
|
-
Version 4.x is designed to work with ActiveRecord 3.1.x
|
34
|
-
|
35
|
-
Run the following command to list available versions:
|
36
|
-
|
37
|
-
gem list composite_primary_keys -ra
|
38
|
-
|
39
|
-
== The basics
|
40
|
-
|
41
|
-
A model with composite primary keys is defined like this:
|
42
|
-
|
43
|
-
class Membership < ActiveRecord::Base
|
44
|
-
self.primary_keys = :user_id, :group_id
|
45
|
-
belongs_to :user
|
46
|
-
belongs_to :group
|
47
|
-
has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
|
48
|
-
end
|
49
|
-
|
50
|
-
Note the addition of the line:
|
51
|
-
|
52
|
-
self.primary_keys = :user_id, :group_id
|
53
|
-
|
54
|
-
|
55
|
-
A model associated with a composite key model is defined like this:
|
56
|
-
|
57
|
-
class MembershipStatus < ActiveRecord::Base
|
58
|
-
belongs_to :membership, :foreign_key => [:user_id, :group_id]
|
59
|
-
end
|
60
|
-
|
61
|
-
That is, associations can include composite keys too. All Rails association types are supported. Nice.
|
62
|
-
|
63
|
-
== Usage
|
64
|
-
|
65
|
-
Once you’ve created your models to specify composite primary keys (such as the Membership class)
|
66
|
-
and associations (such as MembershipStatus#membership), you can use them like any normal model
|
67
|
-
with associations.
|
68
|
-
|
69
|
-
But first, lets check out our primary keys.
|
70
|
-
|
71
|
-
MembershipStatus.primary_key # => "id" # normal single key
|
72
|
-
Membership.primary_key # => [:user_id, :group_id] # composite keys
|
73
|
-
Membership.primary_key.to_s # => "user_id,group_id"
|
74
|
-
|
75
|
-
Now we want to be able to find instances using the same syntax we always use for ActiveRecords.
|
76
|
-
|
77
|
-
MembershipStatus.find(1) # single id returns single instance
|
78
|
-
=> <MembershipStatus:0x392a8c8 @attributes={"id"=>"1", "status"=>"Active"}>
|
79
|
-
|
80
|
-
Membership.find([1,1]) # composite ids returns single instance
|
81
|
-
=> <Membership:0x39218b0 @attributes={"user_id"=>"1", "group_id"=>"1"}>
|
82
|
-
|
83
|
-
Notice the use of an array to specify the composite key values.
|
84
|
-
|
85
|
-
NOTE - API CHANGE. CPK Version 6.x and earlier used to allow composite keys to be listed out
|
86
|
-
like this:
|
87
|
-
|
88
|
-
Membership.find(1,1)
|
89
|
-
|
90
|
-
This usage is no longer supported.
|
91
|
-
|
92
|
-
== Databases
|
93
|
-
|
94
|
-
CPK supports the following databases:
|
95
|
-
|
96
|
-
* PostgreSQL
|
97
|
-
* MySQL
|
98
|
-
* MariaDB
|
99
|
-
* Oracle
|
100
|
-
* DB2
|
101
|
-
* SQLite
|
102
|
-
* SQLServer
|
103
|
-
|
104
|
-
== Tests
|
105
|
-
|
106
|
-
To run tests you first need to install the appropriate gems for the database you want to test. Database gems are
|
107
|
-
divided into the following bundler groups:
|
108
|
-
|
109
|
-
* mysql
|
110
|
-
* oracle
|
111
|
-
* postgresql
|
112
|
-
* sqlite
|
113
|
-
* sqlserver
|
114
|
-
|
115
|
-
Since it is likely you do not have all the above databases installed on your computer, you want to install just the
|
116
|
-
gems for your database. For example, to test postgresql you would install the appropriate gems like this:
|
117
|
-
|
118
|
-
bundler config set --local without "mysql oracle sqlite sqlserver"
|
119
|
-
bundler install
|
120
|
-
|
121
|
-
Once you have installed the appropriate gems, the next step is to create the test database. There is a rake
|
122
|
-
command for each database. Using our example:
|
123
|
-
|
124
|
-
rake postgresql:build_database
|
125
|
-
|
126
|
-
You can also rebuild the database if it already exists using this command:
|
127
|
-
|
128
|
-
rake postgresql:rebuild_database
|
129
|
-
|
130
|
-
To get a list of commands for your database use:
|
131
|
-
|
132
|
-
Rake -T
|
133
|
-
|
134
|
-
Finally, to run tests:
|
135
|
-
|
136
|
-
rake postgresql:test
|
137
|
-
|
138
|
-
Travis build status: {<img src="https://travis-ci.com/composite-primary-keys/composite_primary_keys.svg" alt="Build Status" />}[https://travis-ci.com/composite-primary-keys/composite_primary_keys]
|
139
|
-
|
140
|
-
=== DB2
|
141
|
-
|
142
|
-
DB2 is no longer supported due to difficulties in getting the ibm_db2 gem to build. Thus tests
|
143
|
-
have not been run against db2.
|
144
|
-
|
145
|
-
=== MariaDb (mysql)
|
146
|
-
|
147
|
-
MariaDb is fully supported with all tests passing.
|
148
|
-
|
149
|
-
=== Oracle
|
150
|
-
|
151
|
-
Oracle is fully supported with all tests passing.
|
152
|
-
|
153
|
-
=== Postgresql
|
154
|
-
|
155
|
-
Postgresql is fully supported with all tests passing.
|
156
|
-
|
157
|
-
=== Sqlite 3
|
158
|
-
|
159
|
-
The sqlite database is created at the path composite_primary_keys/db. Note you must *first* create the database using the
|
160
|
-
built-in rake task before running tests:
|
161
|
-
|
162
|
-
rake sqlite:build_database
|
163
|
-
|
164
|
-
For sqlite3 to work correctly, you must manually require 'composite_primary_keys/connection_adapters/sqlite3_adapter' after
|
165
|
-
loading the CPK gem.
|
166
|
-
|
167
|
-
=== SqlServer
|
168
|
-
|
169
|
-
SqlServer is partially supported. There are a number of failing tests - patches welcomed.
|
170
|
-
|
171
|
-
== Questions, Discussion and Contributions
|
172
|
-
|
173
|
-
For help please visit https://github.com/composite-primary-keys/composite_primary_keys.
|
174
|
-
|
175
|
-
== Author
|
176
|
-
|
177
|
-
First version was written by Dr Nic Williams.
|
178
|
-
|
179
|
-
Maintained by Charlie Savage
|
180
|
-
|
181
|
-
Contributions by many!
|
182
|
-
|
1
|
+
= Composite Primary Keys for ActiveRecords
|
2
|
+
|
3
|
+
== Summary
|
4
|
+
|
5
|
+
ActiveRecord infamously doesn't support composite primary keys.
|
6
|
+
This gem, composite_primary_keys, or CPK for short, extends ActiveRecord
|
7
|
+
to support composite keys.
|
8
|
+
|
9
|
+
== Installation
|
10
|
+
|
11
|
+
gem install composite_primary_keys
|
12
|
+
|
13
|
+
If you are using Rails add the following to your Gemfile:
|
14
|
+
|
15
|
+
gem 'composite_primary_keys', '=x.x.x' (see next section about what version to use)
|
16
|
+
|
17
|
+
== Versions
|
18
|
+
|
19
|
+
Every major version of ActiveRecord has included numerous internal changes. As a result,
|
20
|
+
CPK has to be rewritten for each version of ActiveRecord. To help keep
|
21
|
+
things straight, here is the mapping:
|
22
|
+
|
23
|
+
Version 14.x is designed to work with ActiveRecord 7.0.x
|
24
|
+
Version 13.x is designed to work with ActiveRecord 6.1.x
|
25
|
+
Version 12.x is designed to work with ActiveRecord 6.0.x
|
26
|
+
Version 11.x is designed to work with ActiveRecord 5.2.x
|
27
|
+
Version 10.x is designed to work with ActiveRecord 5.1.x
|
28
|
+
Version 9.x is designed to work with ActiveRecord 5.0.x
|
29
|
+
Version 8.x is designed to work with ActiveRecord 4.2.x
|
30
|
+
Version 7.x is designed to work with ActiveRecord 4.1.x
|
31
|
+
Version 6.x is designed to work with ActiveRecord 4.0.x
|
32
|
+
Version 5.x is designed to work with ActiveRecord 3.2.x
|
33
|
+
Version 4.x is designed to work with ActiveRecord 3.1.x
|
34
|
+
|
35
|
+
Run the following command to list available versions:
|
36
|
+
|
37
|
+
gem list composite_primary_keys -ra
|
38
|
+
|
39
|
+
== The basics
|
40
|
+
|
41
|
+
A model with composite primary keys is defined like this:
|
42
|
+
|
43
|
+
class Membership < ActiveRecord::Base
|
44
|
+
self.primary_keys = :user_id, :group_id
|
45
|
+
belongs_to :user
|
46
|
+
belongs_to :group
|
47
|
+
has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
|
48
|
+
end
|
49
|
+
|
50
|
+
Note the addition of the line:
|
51
|
+
|
52
|
+
self.primary_keys = :user_id, :group_id
|
53
|
+
|
54
|
+
|
55
|
+
A model associated with a composite key model is defined like this:
|
56
|
+
|
57
|
+
class MembershipStatus < ActiveRecord::Base
|
58
|
+
belongs_to :membership, :foreign_key => [:user_id, :group_id]
|
59
|
+
end
|
60
|
+
|
61
|
+
That is, associations can include composite keys too. All Rails association types are supported. Nice.
|
62
|
+
|
63
|
+
== Usage
|
64
|
+
|
65
|
+
Once you’ve created your models to specify composite primary keys (such as the Membership class)
|
66
|
+
and associations (such as MembershipStatus#membership), you can use them like any normal model
|
67
|
+
with associations.
|
68
|
+
|
69
|
+
But first, lets check out our primary keys.
|
70
|
+
|
71
|
+
MembershipStatus.primary_key # => "id" # normal single key
|
72
|
+
Membership.primary_key # => [:user_id, :group_id] # composite keys
|
73
|
+
Membership.primary_key.to_s # => "user_id,group_id"
|
74
|
+
|
75
|
+
Now we want to be able to find instances using the same syntax we always use for ActiveRecords.
|
76
|
+
|
77
|
+
MembershipStatus.find(1) # single id returns single instance
|
78
|
+
=> <MembershipStatus:0x392a8c8 @attributes={"id"=>"1", "status"=>"Active"}>
|
79
|
+
|
80
|
+
Membership.find([1,1]) # composite ids returns single instance
|
81
|
+
=> <Membership:0x39218b0 @attributes={"user_id"=>"1", "group_id"=>"1"}>
|
82
|
+
|
83
|
+
Notice the use of an array to specify the composite key values.
|
84
|
+
|
85
|
+
NOTE - API CHANGE. CPK Version 6.x and earlier used to allow composite keys to be listed out
|
86
|
+
like this:
|
87
|
+
|
88
|
+
Membership.find(1,1)
|
89
|
+
|
90
|
+
This usage is no longer supported.
|
91
|
+
|
92
|
+
== Databases
|
93
|
+
|
94
|
+
CPK supports the following databases:
|
95
|
+
|
96
|
+
* PostgreSQL
|
97
|
+
* MySQL
|
98
|
+
* MariaDB
|
99
|
+
* Oracle
|
100
|
+
* DB2
|
101
|
+
* SQLite
|
102
|
+
* SQLServer
|
103
|
+
|
104
|
+
== Tests
|
105
|
+
|
106
|
+
To run tests you first need to install the appropriate gems for the database you want to test. Database gems are
|
107
|
+
divided into the following bundler groups:
|
108
|
+
|
109
|
+
* mysql
|
110
|
+
* oracle
|
111
|
+
* postgresql
|
112
|
+
* sqlite
|
113
|
+
* sqlserver
|
114
|
+
|
115
|
+
Since it is likely you do not have all the above databases installed on your computer, you want to install just the
|
116
|
+
gems for your database. For example, to test postgresql you would install the appropriate gems like this:
|
117
|
+
|
118
|
+
bundler config set --local without "mysql oracle sqlite sqlserver"
|
119
|
+
bundler install
|
120
|
+
|
121
|
+
Once you have installed the appropriate gems, the next step is to create the test database. There is a rake
|
122
|
+
command for each database. Using our example:
|
123
|
+
|
124
|
+
rake postgresql:build_database
|
125
|
+
|
126
|
+
You can also rebuild the database if it already exists using this command:
|
127
|
+
|
128
|
+
rake postgresql:rebuild_database
|
129
|
+
|
130
|
+
To get a list of commands for your database use:
|
131
|
+
|
132
|
+
Rake -T
|
133
|
+
|
134
|
+
Finally, to run tests:
|
135
|
+
|
136
|
+
rake postgresql:test
|
137
|
+
|
138
|
+
Travis build status: {<img src="https://travis-ci.com/composite-primary-keys/composite_primary_keys.svg" alt="Build Status" />}[https://travis-ci.com/composite-primary-keys/composite_primary_keys]
|
139
|
+
|
140
|
+
=== DB2
|
141
|
+
|
142
|
+
DB2 is no longer supported due to difficulties in getting the ibm_db2 gem to build. Thus tests
|
143
|
+
have not been run against db2.
|
144
|
+
|
145
|
+
=== MariaDb (mysql)
|
146
|
+
|
147
|
+
MariaDb is fully supported with all tests passing.
|
148
|
+
|
149
|
+
=== Oracle
|
150
|
+
|
151
|
+
Oracle is fully supported with all tests passing.
|
152
|
+
|
153
|
+
=== Postgresql
|
154
|
+
|
155
|
+
Postgresql is fully supported with all tests passing.
|
156
|
+
|
157
|
+
=== Sqlite 3
|
158
|
+
|
159
|
+
The sqlite database is created at the path composite_primary_keys/db. Note you must *first* create the database using the
|
160
|
+
built-in rake task before running tests:
|
161
|
+
|
162
|
+
rake sqlite:build_database
|
163
|
+
|
164
|
+
For sqlite3 to work correctly, you must manually require 'composite_primary_keys/connection_adapters/sqlite3_adapter' after
|
165
|
+
loading the CPK gem.
|
166
|
+
|
167
|
+
=== SqlServer
|
168
|
+
|
169
|
+
SqlServer is partially supported. There are a number of failing tests - patches welcomed.
|
170
|
+
|
171
|
+
== Questions, Discussion and Contributions
|
172
|
+
|
173
|
+
For help please visit https://github.com/composite-primary-keys/composite_primary_keys.
|
174
|
+
|
175
|
+
== Author
|
176
|
+
|
177
|
+
First version was written by Dr Nic Williams.
|
178
|
+
|
179
|
+
Maintained by Charlie Savage
|
180
|
+
|
181
|
+
Contributions by many!
|
182
|
+
|
data/Rakefile
CHANGED
@@ -1,37 +1,37 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'rake'
|
3
|
-
require 'rake/clean'
|
4
|
-
require 'rake/testtask'
|
5
|
-
require 'rubygems/package_task'
|
6
|
-
|
7
|
-
# Set global variable so other tasks can access them
|
8
|
-
::PROJECT_ROOT = File.expand_path(".")
|
9
|
-
::GEM_NAME = 'composite_primary_keys'
|
10
|
-
|
11
|
-
require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
|
12
|
-
require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
|
13
|
-
|
14
|
-
# Read the spec file
|
15
|
-
spec = Gem::Specification.load("#{GEM_NAME}.gemspec")
|
16
|
-
|
17
|
-
# Setup Rake tasks for managing the gem
|
18
|
-
Gem::PackageTask.new(spec).define
|
19
|
-
|
20
|
-
# Now load in other task files
|
21
|
-
Dir.glob('tasks/**/*.rake').each do |rake_file|
|
22
|
-
load File.join(File.dirname(__FILE__), rake_file)
|
23
|
-
end
|
24
|
-
|
25
|
-
# Set up test tasks for each supported connection adapter
|
26
|
-
%w(mysql sqlite oracle oracle_enhanced postgresql ibm_db sqlserver trilogy).each do |adapter|
|
27
|
-
namespace adapter do
|
28
|
-
desc "Run tests using the #{adapter} adapter"
|
29
|
-
task "test" do
|
30
|
-
ENV["ADAPTER"] = adapter
|
31
|
-
Rake::TestTask.new("subtest_#{adapter}") do |t|
|
32
|
-
t.libs << "test"
|
33
|
-
end
|
34
|
-
Rake::Task["subtest_#{adapter}"].invoke
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rubygems/package_task'
|
6
|
+
|
7
|
+
# Set global variable so other tasks can access them
|
8
|
+
::PROJECT_ROOT = File.expand_path(".")
|
9
|
+
::GEM_NAME = 'composite_primary_keys'
|
10
|
+
|
11
|
+
require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
|
12
|
+
require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
|
13
|
+
|
14
|
+
# Read the spec file
|
15
|
+
spec = Gem::Specification.load("#{GEM_NAME}.gemspec")
|
16
|
+
|
17
|
+
# Setup Rake tasks for managing the gem
|
18
|
+
Gem::PackageTask.new(spec).define
|
19
|
+
|
20
|
+
# Now load in other task files
|
21
|
+
Dir.glob('tasks/**/*.rake').each do |rake_file|
|
22
|
+
load File.join(File.dirname(__FILE__), rake_file)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Set up test tasks for each supported connection adapter
|
26
|
+
%w(mysql sqlite oracle oracle_enhanced postgresql ibm_db sqlserver trilogy).each do |adapter|
|
27
|
+
namespace adapter do
|
28
|
+
desc "Run tests using the #{adapter} adapter"
|
29
|
+
task "test" do
|
30
|
+
ENV["ADAPTER"] = adapter
|
31
|
+
Rake::TestTask.new("subtest_#{adapter}") do |t|
|
32
|
+
t.libs << "test"
|
33
|
+
end
|
34
|
+
Rake::Task["subtest_#{adapter}"].invoke
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -1,39 +1,39 @@
|
|
1
|
-
module CompositePrimaryKeys
|
2
|
-
module CollectionAssociation
|
3
|
-
def ids_writer(ids)
|
4
|
-
primary_key = reflection.association_primary_key
|
5
|
-
ids = Array(ids).reject(&:blank?)
|
6
|
-
|
7
|
-
# CPK-
|
8
|
-
if primary_key.is_a?(Array)
|
9
|
-
ids = ids.map { |id| CompositePrimaryKeys::CompositeKeys.parse(id) }
|
10
|
-
primary_key.each_with_index do |key, i|
|
11
|
-
pk_type = klass.type_for_attribute(key)
|
12
|
-
ids.each { |id| id[i] = pk_type.cast(id[i]) }
|
13
|
-
end
|
14
|
-
|
15
|
-
predicate = CompositePrimaryKeys::Predicates.cpk_in_predicate(klass.arel_table, reflection.association_primary_key, ids)
|
16
|
-
records = klass.where(predicate).index_by do |r|
|
17
|
-
reflection.association_primary_key.map{ |k| r.send(k) }
|
18
|
-
end.values_at(*ids).compact
|
19
|
-
else
|
20
|
-
pk_type = klass.type_for_attribute(primary_key)
|
21
|
-
ids.map! { |i| pk_type.cast(i) }
|
22
|
-
|
23
|
-
records = klass.where(primary_key => ids).index_by do |r|
|
24
|
-
r.public_send(primary_key)
|
25
|
-
end.values_at(*ids).compact
|
26
|
-
end
|
27
|
-
|
28
|
-
if records.size != ids.size
|
29
|
-
found_ids = records.map { |record| record.public_send(primary_key) }
|
30
|
-
not_found_ids = ids - found_ids
|
31
|
-
klass.all.raise_record_not_found_exception!(ids, records.size, ids.size, primary_key, not_found_ids)
|
32
|
-
else
|
33
|
-
replace(records)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
1
|
+
module CompositePrimaryKeys
|
2
|
+
module CollectionAssociation
|
3
|
+
def ids_writer(ids)
|
4
|
+
primary_key = reflection.association_primary_key
|
5
|
+
ids = Array(ids).reject(&:blank?)
|
6
|
+
|
7
|
+
# CPK-
|
8
|
+
if primary_key.is_a?(Array)
|
9
|
+
ids = ids.map { |id| CompositePrimaryKeys::CompositeKeys.parse(id) }
|
10
|
+
primary_key.each_with_index do |key, i|
|
11
|
+
pk_type = klass.type_for_attribute(key)
|
12
|
+
ids.each { |id| id[i] = pk_type.cast(id[i]) }
|
13
|
+
end
|
14
|
+
|
15
|
+
predicate = CompositePrimaryKeys::Predicates.cpk_in_predicate(klass.arel_table, reflection.association_primary_key, ids)
|
16
|
+
records = klass.where(predicate).index_by do |r|
|
17
|
+
reflection.association_primary_key.map{ |k| r.send(k) }
|
18
|
+
end.values_at(*ids).compact
|
19
|
+
else
|
20
|
+
pk_type = klass.type_for_attribute(primary_key)
|
21
|
+
ids.map! { |i| pk_type.cast(i) }
|
22
|
+
|
23
|
+
records = klass.where(primary_key => ids).index_by do |r|
|
24
|
+
r.public_send(primary_key)
|
25
|
+
end.values_at(*ids).compact
|
26
|
+
end
|
27
|
+
|
28
|
+
if records.size != ids.size
|
29
|
+
found_ids = records.map { |record| record.public_send(primary_key) }
|
30
|
+
not_found_ids = ids - found_ids
|
31
|
+
klass.all.raise_record_not_found_exception!(ids, records.size, ids.size, primary_key, not_found_ids)
|
32
|
+
else
|
33
|
+
replace(records)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
39
|
ActiveRecord::Associations::CollectionAssociation.prepend CompositePrimaryKeys::CollectionAssociation
|
@@ -1,52 +1,52 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module Associations
|
3
|
-
class Preloader
|
4
|
-
class Association
|
5
|
-
|
6
|
-
class LoaderQuery
|
7
|
-
def load_records_for_keys(keys, &block)
|
8
|
-
# CPK
|
9
|
-
if association_key_name.is_a?(Array)
|
10
|
-
predicate = cpk_in_predicate(scope.klass.arel_table, association_key_name, keys)
|
11
|
-
scope.where(predicate).load(&block)
|
12
|
-
else
|
13
|
-
scope.where(association_key_name => keys).load(&block)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
# TODO: is records_for needed anymore? Rails' implementation has changed significantly
|
19
|
-
def records_for(ids)
|
20
|
-
records = if association_key_name.is_a?(Array)
|
21
|
-
predicate = cpk_in_predicate(klass.arel_table, association_key_name, ids)
|
22
|
-
scope.where(predicate)
|
23
|
-
else
|
24
|
-
scope.where(association_key_name => ids)
|
25
|
-
end
|
26
|
-
records.load do |record|
|
27
|
-
# Processing only the first owner
|
28
|
-
# because the record is modified but not an owner
|
29
|
-
owner = owners_by_key[convert_key(record[association_key_name])].first
|
30
|
-
association = owner.association(reflection.name)
|
31
|
-
association.set_inverse_instance(record)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def owners_by_key
|
36
|
-
@owners_by_key ||= owners.each_with_object({}) do |owner, result|
|
37
|
-
# CPK
|
38
|
-
# key = convert_key(owner[owner_key_name])
|
39
|
-
key = if owner_key_name.is_a?(Array)
|
40
|
-
Array(owner_key_name).map do |key_name|
|
41
|
-
convert_key(owner[key_name])
|
42
|
-
end
|
43
|
-
else
|
44
|
-
convert_key(owner[owner_key_name])
|
45
|
-
end
|
46
|
-
(result[key] ||= []) << owner if key
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
1
|
+
module ActiveRecord
|
2
|
+
module Associations
|
3
|
+
class Preloader
|
4
|
+
class Association
|
5
|
+
|
6
|
+
class LoaderQuery
|
7
|
+
def load_records_for_keys(keys, &block)
|
8
|
+
# CPK
|
9
|
+
if association_key_name.is_a?(Array)
|
10
|
+
predicate = cpk_in_predicate(scope.klass.arel_table, association_key_name, keys)
|
11
|
+
scope.where(predicate).load(&block)
|
12
|
+
else
|
13
|
+
scope.where(association_key_name => keys).load(&block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# TODO: is records_for needed anymore? Rails' implementation has changed significantly
|
19
|
+
def records_for(ids)
|
20
|
+
records = if association_key_name.is_a?(Array)
|
21
|
+
predicate = cpk_in_predicate(klass.arel_table, association_key_name, ids)
|
22
|
+
scope.where(predicate)
|
23
|
+
else
|
24
|
+
scope.where(association_key_name => ids)
|
25
|
+
end
|
26
|
+
records.load do |record|
|
27
|
+
# Processing only the first owner
|
28
|
+
# because the record is modified but not an owner
|
29
|
+
owner = owners_by_key[convert_key(record[association_key_name])].first
|
30
|
+
association = owner.association(reflection.name)
|
31
|
+
association.set_inverse_instance(record)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def owners_by_key
|
36
|
+
@owners_by_key ||= owners.each_with_object({}) do |owner, result|
|
37
|
+
# CPK
|
38
|
+
# key = convert_key(owner[owner_key_name])
|
39
|
+
key = if owner_key_name.is_a?(Array)
|
40
|
+
Array(owner_key_name).map do |key_name|
|
41
|
+
convert_key(owner[key_name])
|
42
|
+
end
|
43
|
+
else
|
44
|
+
convert_key(owner[owner_key_name])
|
45
|
+
end
|
46
|
+
(result[key] ||= []) << owner if key
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|