composite_primary_keys 13.0.7 → 14.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,181 +1,182 @@
1
- = Composite Primary Keys for ActiveRecords
2
-
3
- == Summary
4
-
5
- ActiveRecords 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 13.x is designed to work with ActiveRecord 6.1.x
24
- Version 12.x is designed to work with ActiveRecord 6.0.x
25
- Version 11.x is designed to work with ActiveRecord 5.2.x
26
- Version 10.x is designed to work with ActiveRecord 5.1.x
27
- Version 9.x is designed to work with ActiveRecord 5.0.x
28
- Version 8.x is designed to work with ActiveRecord 4.2.x
29
- Version 7.x is designed to work with ActiveRecord 4.1.x
30
- Version 6.x is designed to work with ActiveRecord 4.0.x
31
- Version 5.x is designed to work with ActiveRecord 3.2.x
32
- Version 4.x is designed to work with ActiveRecord 3.1.x
33
-
34
- Run the following command to list available versions:
35
-
36
- gem list composite_primary_keys -ra
37
-
38
- == The basics
39
-
40
- A model with composite primary keys is defined like this:
41
-
42
- class Membership < ActiveRecord::Base
43
- self.primary_keys = :user_id, :group_id
44
- belongs_to :user
45
- belongs_to :group
46
- has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
47
- end
48
-
49
- Note the addition of the line:
50
-
51
- self.primary_keys = :user_id, :group_id
52
-
53
-
54
- A model associated with a composite key model is defined like this:
55
-
56
- class MembershipStatus < ActiveRecord::Base
57
- belongs_to :membership, :foreign_key => [:user_id, :group_id]
58
- end
59
-
60
- That is, associations can include composite keys too. All Rails association types are supported. Nice.
61
-
62
- == Usage
63
-
64
- Once you’ve created your models to specify composite primary keys (such as the Membership class)
65
- and associations (such as MembershipStatus#membership), you can use them like any normal model
66
- with associations.
67
-
68
- But first, lets check out our primary keys.
69
-
70
- MembershipStatus.primary_key # => "id" # normal single key
71
- Membership.primary_key # => [:user_id, :group_id] # composite keys
72
- Membership.primary_key.to_s # => "user_id,group_id"
73
-
74
- Now we want to be able to find instances using the same syntax we always use for ActiveRecords…
75
-
76
- MembershipStatus.find(1) # single id returns single instance
77
- => <MembershipStatus:0x392a8c8 @attributes={"id"=>"1", "status"=>"Active"}>
78
-
79
- Membership.find([1,1]) # composite ids returns single instance
80
- => <Membership:0x39218b0 @attributes={"user_id"=>"1", "group_id"=>"1"}>
81
-
82
- Notice the use of an array to specify the composite key values.
83
-
84
- NOTE - API CHANGE. CPK Version 6.x and earlier used to allow composite keys to be listed out
85
- like this:
86
-
87
- Membership.find(1,1)
88
-
89
- This usage is no longer supported.
90
-
91
- == Databases
92
-
93
- CPK supports the following databases:
94
-
95
- * PostgreSQL
96
- * MySQL
97
- * MariaDB
98
- * Oracle
99
- * DB2
100
- * SQLite
101
- * SQLServer
102
-
103
- == Tests
104
-
105
- To run tests you first need to install the appropriate gems for the database you want to test. Database gems are
106
- divided into the following bundler groups:
107
-
108
- * mysql
109
- * oracle
110
- * postgresql
111
- * sqlite
112
- * sqlserver
113
-
114
- Since it is likely you do not have all the above databases installed on your computer, you want to install just the
115
- gems for your database. For example, to test postgresql you would install the appropriate gems like this:
116
-
117
- bundler config set --local without "mysql oracle sqlite sqlserver"
118
- bundler install
119
-
120
- Once you have installed the appropriate gems, the next step is to create the test database. There is a rake
121
- command for each database. Using our example:
122
-
123
- rake postgresql:build_database
124
-
125
- You can also rebuild the database if it already exists using this command:
126
-
127
- rake postgresql:rebuild_database
128
-
129
- To get a list of commands for your database use:
130
-
131
- Rake -T
132
-
133
- Finally, to run tests:
134
-
135
- rake postgresql:test
136
-
137
- 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]
138
-
139
- === DB2
140
-
141
- DB2 is no longer supported due to difficulties in getting the ibm_db2 gem to build. Thus tests
142
- have not been run against db2.
143
-
144
- === MariaDb (mysql)
145
-
146
- MariaDb is fully supported with all tests passing.
147
-
148
- === Oracle
149
-
150
- Oracle is fully supported with all tests passing.
151
-
152
- === Postgresql
153
-
154
- Postgresql is fully supported with all tests passing.
155
-
156
- === Sqlite 3
157
-
158
- The sqlite database is created at the path composite_primary_keys/db. Note you must *first* create the database using the
159
- built-in rake task before running tests:
160
-
161
- rake sqlite:build_database
162
-
163
- For sqlite3 to work correctly, you must manually require 'composite_primary_keys/connection_adapters/sqlite3_adapter' after
164
- loading the CPK gem.
165
-
166
- === SqlServer
167
-
168
- SqlServer is partially supported. There are a number of failing tests - patches welcomed.
169
-
170
- == Questions, Discussion and Contributions
171
-
172
- For help please visit https://github.com/composite-primary-keys/composite_primary_keys.
173
-
174
- == Author
175
-
176
- First version was written by Dr Nic Williams.
177
-
178
- Maintained by Charlie Savage
179
-
180
- Contributions by many!
181
-
1
+ = Composite Primary Keys for ActiveRecords
2
+
3
+ == Summary
4
+
5
+ ActiveRecords 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,32 +1,32 @@
1
- module CompositePrimaryKeys
2
- module CollectionAssociation
3
- def ids_writer(ids)
4
- primary_key = reflection.association_primary_key
5
- pk_type = klass.type_for_attribute(primary_key)
6
- ids = Array(ids).reject(&:blank?)
7
- ids.map! { |i| pk_type.cast(i) }
8
-
9
- # CPK-
10
- if primary_key.is_a?(Array)
11
- predicate = CompositePrimaryKeys::Predicates.cpk_in_predicate(klass.arel_table, reflection.association_primary_key, ids)
12
- records = klass.where(predicate).index_by do |r|
13
- reflection.association_primary_key.map{ |k| r.send(k) }
14
- end.values_at(*ids)
15
- else
16
- records = klass.where(primary_key => ids).index_by do |r|
17
- r.public_send(primary_key)
18
- end.values_at(*ids).compact
19
- end
20
-
21
- if records.size != ids.size
22
- found_ids = records.map { |record| record.public_send(primary_key) }
23
- not_found_ids = ids - found_ids
24
- klass.all.raise_record_not_found_exception!(ids, records.size, ids.size, primary_key, not_found_ids)
25
- else
26
- replace(records)
27
- end
28
- end
29
- end
30
- end
31
-
1
+ module CompositePrimaryKeys
2
+ module CollectionAssociation
3
+ def ids_writer(ids)
4
+ primary_key = reflection.association_primary_key
5
+ pk_type = klass.type_for_attribute(primary_key)
6
+ ids = Array(ids).reject(&:blank?)
7
+ ids.map! { |i| pk_type.cast(i) }
8
+
9
+ # CPK-
10
+ if primary_key.is_a?(Array)
11
+ predicate = CompositePrimaryKeys::Predicates.cpk_in_predicate(klass.arel_table, reflection.association_primary_key, ids)
12
+ records = klass.where(predicate).index_by do |r|
13
+ reflection.association_primary_key.map{ |k| r.send(k) }
14
+ end.values_at(*ids)
15
+ else
16
+ records = klass.where(primary_key => ids).index_by do |r|
17
+ r.public_send(primary_key)
18
+ end.values_at(*ids).compact
19
+ end
20
+
21
+ if records.size != ids.size
22
+ found_ids = records.map { |record| record.public_send(primary_key) }
23
+ not_found_ids = ids - found_ids
24
+ klass.all.raise_record_not_found_exception!(ids, records.size, ids.size, primary_key, not_found_ids)
25
+ else
26
+ replace(records)
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
32
  ActiveRecord::Associations::CollectionAssociation.prepend CompositePrimaryKeys::CollectionAssociation