composite_primary_keys 6.0.5 → 6.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/History.rdoc +12 -0
- data/README.rdoc +72 -26
- data/lib/composite_primary_keys/version.rb +1 -1
- data/scripts/console.rb +0 -0
- data/test/abstract_unit.rb +3 -5
- data/test/connections/databases.yml +2 -11
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
YzNmMGQzNmIxODQ1NTk1ODk1ZDJjMTk4OTg4NDIxZDZlM2I5NzI4Nw==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cf7821e1dce524f20a81e982faa1042c1bb5cf0b
|
4
|
+
data.tar.gz: 5112714e5ae73ba0335ac34aca8a7f5f699ba6a3
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
MjhmNzk3NzI4NjFmOTc3OWVhMDY4ZThhYjI2NDhiMDQ3YzM0MTA1ZWQzOGM3
|
11
|
-
ZWZjOWE2NDhlNGJkZGU2MGI1Mjk1ZDJmODU2MDIzOTQ0NjExZDU=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
NTZmZTQ0ZTNlYjhiZjBmYmE5YWEwZDNkMjViMTM0YjIyYTc1NDRhOTQ3YWU1
|
14
|
-
YzlmMTdkZDc3MmIxNDMwYTUwYWIyOWMwM2IzYzM2YTI3MzVlNmI3YzUzZTVk
|
15
|
-
YmIxZWJjNzc4YWU0YWE3YmRhZjVlODJjMGQwYzZmNWIwMGZmYWM=
|
6
|
+
metadata.gz: 9944a233a344edfdf63b0bd1dfadf70074f241d4b1cce209aa1280307693e124d09b1bd54907ab94f7a23731c8e8b6ab693dee2246eb65475385a478728c5efd
|
7
|
+
data.tar.gz: 7ec24c0880bbab735c1491bd3e6891692974190150deebfe57a7347502718d751f42f7a44ef28fd9161a3a47bb16721244bf7b829d3a0c6b29d7c7e60c8787a4
|
data/History.rdoc
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
== 6.0.6 (2014-05-26)
|
2
|
+
|
3
|
+
* Don't refer to deprecated set_primary_keys in README (Henare Degan)
|
4
|
+
* Fix gemspec dependency (Charlie Savage)
|
5
|
+
|
6
|
+
== 6.0.5 (2014-05-20)
|
7
|
+
|
8
|
+
* Removed debugging output (Dan Draper)
|
9
|
+
* Fixes to ensure that has_one associations work correctly (Dan Draper)
|
10
|
+
* Fix postgres db migration failure since no result is returned in some cases (seashell-qd)
|
11
|
+
* Fix exception raised in method "id=" (seashell.qd@hotmail.com)
|
12
|
+
|
1
13
|
== 6.0.3 (2014-04-28)
|
2
14
|
|
3
15
|
* Fixes setting of primary key when CPK is not used for a given model (see #191)
|
data/README.rdoc
CHANGED
@@ -2,54 +2,100 @@
|
|
2
2
|
|
3
3
|
== Summary
|
4
4
|
|
5
|
-
ActiveRecords
|
6
|
-
This
|
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.
|
7
8
|
|
8
9
|
== Installation
|
9
10
|
|
10
11
|
gem install composite_primary_keys
|
11
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 verison 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 7.x is designed to work with ActiveRecord 4.1.x
|
24
|
+
Version 6.x is designed to work with ActiveRecord 4.0.x
|
25
|
+
Version 5.x is designed to work with ActiveRecord 3.2.x
|
26
|
+
Version 4.x is designed to work with ActiveRecord 3.1.x
|
27
|
+
|
28
|
+
== The basics
|
29
|
+
|
30
|
+
A model with composite primary keys is defined like this:
|
31
|
+
|
32
|
+
class Membership < ActiveRecord::Base
|
33
|
+
self.primary_keys = :user_id, :group_id
|
34
|
+
belongs_to :user
|
35
|
+
belongs_to :group
|
36
|
+
has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
|
37
|
+
end
|
38
|
+
|
39
|
+
Note the addition of the line:
|
40
|
+
|
41
|
+
self.primary_keys = :user_id, :group_id
|
42
|
+
|
43
|
+
|
44
|
+
A model associated with a composite key model is defined like this:
|
45
|
+
|
46
|
+
class MembershipStatus < ActiveRecord::Base
|
47
|
+
belongs_to :membership, :foreign_key => [:user_id, :group_id]
|
48
|
+
end
|
49
|
+
|
50
|
+
That is, associations can include composite keys too. All Rails association types are supported. Nice.
|
51
|
+
|
12
52
|
== Usage
|
13
53
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
54
|
+
Once you’ve created your models to specify composite primary keys (such as the Membership class)
|
55
|
+
and associations (such as MembershipStatus#membership), you can uses them like any normal model
|
56
|
+
with associations.
|
18
57
|
|
19
|
-
|
58
|
+
But first, lets check out our primary keys.
|
20
59
|
|
21
|
-
|
60
|
+
MembershipStatus.primary_key # => "id" # normal single key
|
61
|
+
Membership.primary_key # => [:user_id, :group_id] # composite keys
|
62
|
+
Membership.primary_key.to_s # => "user_id,group_id"
|
22
63
|
|
23
|
-
|
24
|
-
set_primary_keys :id, :updated_at
|
25
|
-
end
|
64
|
+
Now we want to be able to find instances using the same syntax we always use for ActiveRecords…
|
26
65
|
|
27
|
-
|
28
|
-
|
29
|
-
sequence( :id ) { |n| [n,Time.now] }
|
30
|
-
name "Brett"
|
31
|
-
end
|
32
|
-
end
|
66
|
+
MembershipStatus.find(1) # single id returns single instance
|
67
|
+
=> <MembershipStatus:0x392a8c8 @attributes={"id"=>"1", "status"=>"Active"}>
|
33
68
|
|
69
|
+
Membership.find([1,1]) # composite ids returns single instance
|
70
|
+
=> <Membership:0x39218b0 @attributes={"user_id"=>"1", "group_id"=>"1"}>
|
34
71
|
|
35
|
-
|
72
|
+
Notice the use of an array to specify the composite key values.
|
36
73
|
|
37
|
-
|
74
|
+
== Databases
|
38
75
|
|
39
|
-
|
76
|
+
CPK supports the following databases:
|
40
77
|
|
41
|
-
|
78
|
+
* PostgreSQL
|
79
|
+
* MySQL
|
80
|
+
* Oracle
|
81
|
+
* DB2
|
82
|
+
* SQLite
|
83
|
+
* SQLServer
|
42
84
|
|
43
|
-
==
|
85
|
+
== Running Tests
|
44
86
|
|
45
|
-
|
87
|
+
See test/README_tests.rdoc
|
46
88
|
|
47
89
|
== Questions, Discussion and Contributions
|
48
90
|
|
49
|
-
|
91
|
+
The CPK home page is hosted at https://github.com/composite-primary-keys/composite_primary_keys. There
|
92
|
+
is also a Google group at https://groups.google.com/forum/#!forum/compositekeys,
|
93
|
+
but you'll have better luck getting support using GitHub.
|
50
94
|
|
51
95
|
== Author
|
52
96
|
|
53
|
-
|
54
|
-
Contributions by many!
|
97
|
+
First version was written by Dr Nic Williams.
|
55
98
|
|
99
|
+
Maintained by Charlie Savage
|
100
|
+
|
101
|
+
Contributions by many!
|
data/scripts/console.rb
CHANGED
File without changes
|
data/test/abstract_unit.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
2
2
|
|
3
|
-
require 'active_support/test_case'
|
4
|
-
require 'minitest/autorun'
|
5
|
-
require 'minitest/reporters'
|
6
|
-
MiniTest::Reporters.use!
|
7
|
-
|
8
3
|
# To make debugging easier, test within this source tree versus an installed gem
|
9
4
|
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
10
5
|
require 'composite_primary_keys'
|
11
6
|
|
7
|
+
require 'active_support/test_case'
|
8
|
+
require 'minitest/autorun'
|
9
|
+
|
12
10
|
# Now load the connection spec
|
13
11
|
require File.join(PROJECT_ROOT, "test", "connections", "connection_spec")
|
14
12
|
|
@@ -1,18 +1,9 @@
|
|
1
1
|
# To run tests:
|
2
2
|
# 1. Copy this file to test/connections/databases.yml.
|
3
3
|
# 2. Update to match the databases you want to test against.
|
4
|
-
|
5
|
-
mysql:
|
6
|
-
adapter: mysql2
|
7
|
-
username: root
|
8
|
-
database: composite_primary_keys_unittest
|
9
|
-
|
10
|
-
sqlite3:
|
11
|
-
adapter: sqlite3
|
12
|
-
database: db/composite_primary_keys_unittest.sqlite
|
13
|
-
|
4
|
+
# 3. Build the database using rake <databasename>:build_database
|
14
5
|
postgresql:
|
15
6
|
adapter: postgresql
|
16
7
|
database: composite_primary_keys_unittest
|
17
|
-
username:
|
8
|
+
username: postgres
|
18
9
|
host: localhost
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: composite_primary_keys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 6.0.
|
4
|
+
version: 6.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dr Nic Williams
|
@@ -9,20 +9,20 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-05-
|
12
|
+
date: 2014-05-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- -
|
18
|
+
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
20
|
version: 4.0.0
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- -
|
25
|
+
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: 4.0.0
|
28
28
|
description: Composite key support for ActiveRecord
|
@@ -197,12 +197,12 @@ require_paths:
|
|
197
197
|
- lib
|
198
198
|
required_ruby_version: !ruby/object:Gem::Requirement
|
199
199
|
requirements:
|
200
|
-
- -
|
200
|
+
- - ">="
|
201
201
|
- !ruby/object:Gem::Version
|
202
202
|
version: 1.9.3
|
203
203
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
204
204
|
requirements:
|
205
|
-
- -
|
205
|
+
- - ">="
|
206
206
|
- !ruby/object:Gem::Version
|
207
207
|
version: '0'
|
208
208
|
requirements: []
|
@@ -218,8 +218,8 @@ test_files:
|
|
218
218
|
- test/setup.rb
|
219
219
|
- test/test_aliases.rb
|
220
220
|
- test/test_associations.rb
|
221
|
-
- test/test_attribute_methods.rb
|
222
221
|
- test/test_attributes.rb
|
222
|
+
- test/test_attribute_methods.rb
|
223
223
|
- test/test_calculations.rb
|
224
224
|
- test/test_composite_arrays.rb
|
225
225
|
- test/test_counter_cache.rb
|