composite_primary_keys 8.1.5 → 8.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/History.rdoc +9 -0
- data/README_DB2.rdoc +33 -33
- data/lib/composite_primary_keys/associations/association_scope.rb +27 -2
- data/lib/composite_primary_keys/autosave_association.rb +38 -2
- data/lib/composite_primary_keys/version.rb +1 -1
- data/test/fixtures/db_definitions/mysql.sql +10 -0
- data/test/fixtures/db_definitions/oracle.sql +2 -1
- data/test/fixtures/db_definitions/postgresql.sql +1 -0
- data/test/fixtures/db_definitions/sqlite.sql +9 -0
- data/test/fixtures/db_definitions/sqlserver.sql +2 -1
- data/test/fixtures/employee.rb +2 -1
- data/test/fixtures/employees.yml +23 -19
- data/test/test_associations.rb +7 -0
- data/test/test_create.rb +2 -3
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f837ed1d8f5f9a5af46c14b91dfed1101e55de3
|
4
|
+
data.tar.gz: e115fe2d97338b6d2aa19132be1f936f107dea74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f81ee887e2a3145def527fbdce440bd2cc986fa2a03d4fef914e269e642ba355d09c307cb8be25723d6d79fed632fe238cd28376fb6734a549de54d7b9f16a2a
|
7
|
+
data.tar.gz: 2e0166164b07de368e07758d72029c6461fab1596d311c11634b8c4e97dfd4159a3593d5acde2af2b60eec7701b7214f8487824b3b6185d687df064b5ebe91ef
|
data/History.rdoc
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
== 8.1.6 (2017-05-20)
|
2
|
+
|
3
|
+
Query cache with bind params (ttw)
|
4
|
+
Fix TestIds#test_set_ids_string. Sqlite's serial and integer are a bit different (Boris Peterbarg)
|
5
|
+
Associations with inverse fix (Boris Peterbarg)
|
6
|
+
Fix save_has_one_association and add a test for it (Boris Peterbarg)
|
7
|
+
Fix save_belongs_to_association override (Boris Peterbarg)
|
8
|
+
Extend approach from #344 to fix autosave for has_one associations as well (Cameron Finucane)
|
9
|
+
|
1
10
|
== 8.1.5 (2017-01-01)
|
2
11
|
|
3
12
|
* Don't nest PK twice when looking up id, fixes #319 (Kerey Roper)
|
data/README_DB2.rdoc
CHANGED
@@ -1,33 +1,33 @@
|
|
1
|
-
Composite Primary key support for db2
|
2
|
-
|
3
|
-
== Driver Support
|
4
|
-
|
5
|
-
DB2 support requires the IBM_DB driver provided by http://rubyforge.org/projects/rubyibm/
|
6
|
-
project. Install using gem install ibm_db. Tested against version 0.60 of the driver.
|
7
|
-
This rubyforge project appears to be permenant location for the IBM adapter.
|
8
|
-
Older versions of the driver available from IBM Alphaworks will not work.
|
9
|
-
|
10
|
-
== Driver Bug and workaround provided as part of this plugin
|
11
|
-
|
12
|
-
Unlike the basic quote routine available for Rails AR, the DB2 adapter's quote
|
13
|
-
method doesn't return " column_name = 1 " when string values (integers in string type variable)
|
14
|
-
are passed for quoting numeric column. Rather it returns "column_name = '1'.
|
15
|
-
DB2 doesn't accept single quoting numeric columns in SQL. Currently, as part of
|
16
|
-
this plugin a fix is provided for the DB2 adapter since this plugin does
|
17
|
-
pass string values like this. Perhaps a patch should be sent to the DB2 adapter
|
18
|
-
project for a permanant fix.
|
19
|
-
|
20
|
-
== Database Setup
|
21
|
-
|
22
|
-
Database must be manually created using a separate command. Read the rake task
|
23
|
-
for creating tables and change the db name, user and passwords accordingly.
|
24
|
-
|
25
|
-
== Tested Database Server version
|
26
|
-
|
27
|
-
This is tested against DB2 v9.1 in Ubuntu Feisty Fawn (7.04)
|
28
|
-
|
29
|
-
== Tested Database Client version
|
30
|
-
|
31
|
-
This is tested against DB2 v9.1 in Ubuntu Feisty Fawn (7.04)
|
32
|
-
|
33
|
-
|
1
|
+
Composite Primary key support for db2
|
2
|
+
|
3
|
+
== Driver Support
|
4
|
+
|
5
|
+
DB2 support requires the IBM_DB driver provided by http://rubyforge.org/projects/rubyibm/
|
6
|
+
project. Install using gem install ibm_db. Tested against version 0.60 of the driver.
|
7
|
+
This rubyforge project appears to be permenant location for the IBM adapter.
|
8
|
+
Older versions of the driver available from IBM Alphaworks will not work.
|
9
|
+
|
10
|
+
== Driver Bug and workaround provided as part of this plugin
|
11
|
+
|
12
|
+
Unlike the basic quote routine available for Rails AR, the DB2 adapter's quote
|
13
|
+
method doesn't return " column_name = 1 " when string values (integers in string type variable)
|
14
|
+
are passed for quoting numeric column. Rather it returns "column_name = '1'.
|
15
|
+
DB2 doesn't accept single quoting numeric columns in SQL. Currently, as part of
|
16
|
+
this plugin a fix is provided for the DB2 adapter since this plugin does
|
17
|
+
pass string values like this. Perhaps a patch should be sent to the DB2 adapter
|
18
|
+
project for a permanant fix.
|
19
|
+
|
20
|
+
== Database Setup
|
21
|
+
|
22
|
+
Database must be manually created using a separate command. Read the rake task
|
23
|
+
for creating tables and change the db name, user and passwords accordingly.
|
24
|
+
|
25
|
+
== Tested Database Server version
|
26
|
+
|
27
|
+
This is tested against DB2 v9.1 in Ubuntu Feisty Fawn (7.04)
|
28
|
+
|
29
|
+
== Tested Database Client version
|
30
|
+
|
31
|
+
This is tested against DB2 v9.1 in Ubuntu Feisty Fawn (7.04)
|
32
|
+
|
33
|
+
|
@@ -2,6 +2,27 @@ module ActiveRecord
|
|
2
2
|
module Associations
|
3
3
|
class AssociationScope
|
4
4
|
|
5
|
+
def self.get_bind_values(owner, chain)
|
6
|
+
binds = []
|
7
|
+
last_reflection = chain.last
|
8
|
+
|
9
|
+
# CPK
|
10
|
+
# binds << last_reflection.join_id_for(owner)
|
11
|
+
values = last_reflection.join_id_for(owner)
|
12
|
+
binds += Array(values)
|
13
|
+
|
14
|
+
if last_reflection.type
|
15
|
+
binds << owner.class.base_class.name
|
16
|
+
end
|
17
|
+
|
18
|
+
chain.each_cons(2).each do |reflection, next_reflection|
|
19
|
+
if reflection.type
|
20
|
+
binds << next_reflection.klass.base_class.name
|
21
|
+
end
|
22
|
+
end
|
23
|
+
binds
|
24
|
+
end
|
25
|
+
|
5
26
|
def next_chain_scope(scope, table, reflection, tracker, assoc_klass, foreign_table, next_reflection)
|
6
27
|
join_keys = reflection.join_keys(assoc_klass)
|
7
28
|
key = join_keys.key
|
@@ -26,8 +47,12 @@ module ActiveRecord
|
|
26
47
|
foreign_key = join_keys.foreign_key
|
27
48
|
|
28
49
|
if key.kind_of?(Array) || foreign_key.kind_of?(Array)
|
29
|
-
|
30
|
-
|
50
|
+
key = Array(key) unless key.kind_of?(Array)
|
51
|
+
foreign_key = Array(foreign_key) unless foreign_key.kind_of?(Array)
|
52
|
+
key.zip(foreign_key).map do |k, fk|
|
53
|
+
bind_val = bind scope, table.table_name, k.to_s, owner[fk], tracker
|
54
|
+
scope = scope.where(table[k].eq(bind_val))
|
55
|
+
end
|
31
56
|
else
|
32
57
|
bind_val = bind scope, table.table_name, key.to_s, owner[foreign_key], tracker
|
33
58
|
scope = scope.where(table[key].eq(bind_val))
|
@@ -17,8 +17,9 @@ module ActiveRecord
|
|
17
17
|
saved = record.save(:validate => !autosave) if record.new_record? || (autosave && record.changed_for_autosave?)
|
18
18
|
|
19
19
|
if association.updated?
|
20
|
-
#
|
21
|
-
association_id = record.
|
20
|
+
# CPK
|
21
|
+
# association_id = record.send(reflection.options[:primary_key] || :id)
|
22
|
+
association_id = reflection.options[:primary_key] ? record.read_attribute(reflection.options[:primary_key]) : record.id
|
22
23
|
self[reflection.foreign_key] = association_id
|
23
24
|
association.loaded!
|
24
25
|
end
|
@@ -27,5 +28,40 @@ module ActiveRecord
|
|
27
28
|
end
|
28
29
|
end
|
29
30
|
end
|
31
|
+
|
32
|
+
# Saves the associated record if it's new or <tt>:autosave</tt> is enabled
|
33
|
+
# on the association.
|
34
|
+
#
|
35
|
+
# In addition, it will destroy the association if it was marked for
|
36
|
+
# destruction with mark_for_destruction.
|
37
|
+
#
|
38
|
+
# This all happens inside a transaction, _if_ the Transactions module is included into
|
39
|
+
# ActiveRecord::Base after the AutosaveAssociation module, which it does by default.
|
40
|
+
def save_has_one_association(reflection)
|
41
|
+
association = association_instance_get(reflection.name)
|
42
|
+
record = association && association.load_target
|
43
|
+
|
44
|
+
if record && !record.destroyed?
|
45
|
+
autosave = reflection.options[:autosave]
|
46
|
+
|
47
|
+
if autosave && record.marked_for_destruction?
|
48
|
+
record.destroy
|
49
|
+
elsif autosave != false
|
50
|
+
# CPK
|
51
|
+
#key = reflection.options[:primary_key] ? send(reflection.options[:primary_key]) : id
|
52
|
+
key = reflection.options[:primary_key] ? read_attribute(reflection.options[:primary_key]) : id
|
53
|
+
|
54
|
+
if (autosave && record.changed_for_autosave?) || new_record? || record_changed?(reflection, record, key)
|
55
|
+
unless reflection.through_reflection
|
56
|
+
record[reflection.foreign_key] = key
|
57
|
+
end
|
58
|
+
|
59
|
+
saved = record.save(:validate => !autosave)
|
60
|
+
raise ActiveRecord::Rollback if !saved && autosave
|
61
|
+
saved
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
30
66
|
end
|
31
67
|
end
|
@@ -116,6 +116,7 @@ create table employees (
|
|
116
116
|
id int not null auto_increment,
|
117
117
|
department_id int default null,
|
118
118
|
location_id int default null,
|
119
|
+
name varchar(100) not null default 'anonymous',
|
119
120
|
primary key (id)
|
120
121
|
);
|
121
122
|
|
@@ -216,3 +217,12 @@ create table employees_groups (
|
|
216
217
|
employee_id int not null,
|
217
218
|
group_id int not null
|
218
219
|
);
|
220
|
+
|
221
|
+
create table pk_called_ids (
|
222
|
+
id serial not null,
|
223
|
+
reference_code int not null,
|
224
|
+
code_label varchar(50) default null,
|
225
|
+
abbreviation varchar(50) default null,
|
226
|
+
description varchar(50) default null,
|
227
|
+
primary key (id, reference_code)
|
228
|
+
);
|
@@ -124,7 +124,8 @@ create sequence employees_seq start with 1000;
|
|
124
124
|
create table employees (
|
125
125
|
id number(11) not null primary key,
|
126
126
|
department_id number(11) default null,
|
127
|
-
location_id number(11) default null
|
127
|
+
location_id number(11) default null,
|
128
|
+
name varchar2(100) default 'anonymous'
|
128
129
|
);
|
129
130
|
|
130
131
|
create sequence salaries_seq start with 1000;
|
@@ -109,6 +109,7 @@ create table departments (
|
|
109
109
|
create table employees (
|
110
110
|
id integer not null primary key autoincrement,
|
111
111
|
department_id integer null,
|
112
|
+
name text not null default 'anonymous',
|
112
113
|
location_id integer null
|
113
114
|
);
|
114
115
|
|
@@ -204,3 +205,11 @@ create table employees_groups (
|
|
204
205
|
group_id integer not null
|
205
206
|
);
|
206
207
|
|
208
|
+
create table pk_called_ids (
|
209
|
+
id integer not null,
|
210
|
+
reference_code int not null,
|
211
|
+
code_label varchar(50) default null,
|
212
|
+
abbreviation varchar(50) default null,
|
213
|
+
description varchar(50) default null,
|
214
|
+
primary key (id, reference_code)
|
215
|
+
);
|
data/test/fixtures/employee.rb
CHANGED
@@ -7,5 +7,6 @@ class Employee < ActiveRecord::Base
|
|
7
7
|
:foreign_key => [:employee_id, :location_id]
|
8
8
|
has_one :one_salary, :class_name => "Salary",
|
9
9
|
:primary_key => [:id, :location_id],
|
10
|
-
:foreign_key => [:employee_id, :location_id]
|
10
|
+
:foreign_key => [:employee_id, :location_id],
|
11
|
+
:inverse_of => :employee
|
11
12
|
end
|
data/test/fixtures/employees.yml
CHANGED
@@ -1,19 +1,23 @@
|
|
1
|
-
steve:
|
2
|
-
id: 1
|
3
|
-
department_id: 1
|
4
|
-
location_id: 1
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
1
|
+
steve:
|
2
|
+
id: 1
|
3
|
+
department_id: 1
|
4
|
+
location_id: 1
|
5
|
+
name: steve
|
6
|
+
|
7
|
+
jill:
|
8
|
+
id: 2
|
9
|
+
department_id: 1
|
10
|
+
location_id: 1
|
11
|
+
name: jill
|
12
|
+
|
13
|
+
sarah:
|
14
|
+
id: 3
|
15
|
+
department_id: 2
|
16
|
+
location_id: 1
|
17
|
+
name: sarah
|
18
|
+
|
19
|
+
robert:
|
20
|
+
id: 4
|
21
|
+
department_id: 2
|
22
|
+
location_id: 1
|
23
|
+
name: robert
|
data/test/test_associations.rb
CHANGED
@@ -91,6 +91,13 @@ class TestAssociations < ActiveSupport::TestCase
|
|
91
91
|
assert_equal(room_assignment.room_id, 1001)
|
92
92
|
end
|
93
93
|
|
94
|
+
def test_association_with_composite_primary_key_can_be_autosaved_on_update
|
95
|
+
robert = employees(:robert)
|
96
|
+
robert_salary = robert.create_one_salary(year: "2015", month: "1")
|
97
|
+
robert.update(name: robert.name.reverse)
|
98
|
+
assert_equal(robert.id, robert_salary.employee_id)
|
99
|
+
end
|
100
|
+
|
94
101
|
def test_has_one_association_primary_key_and_foreign_key_are_present
|
95
102
|
steve = employees(:steve)
|
96
103
|
steve_salary = steve.create_one_salary(year: "2015", month: "1")
|
data/test/test_create.rb
CHANGED
@@ -123,7 +123,7 @@ class TestCreate < ActiveSupport::TestCase
|
|
123
123
|
assignment1 = RoomAssignment.new(:student_id => student1.id, :dorm_id => room.dorm_id, :room_id => room.room_id)
|
124
124
|
assignment1.save!
|
125
125
|
|
126
|
-
room.
|
126
|
+
room.room_assignments = [assignment1]
|
127
127
|
room.save!
|
128
128
|
|
129
129
|
assert_equal(1, room.room_assignments.length)
|
@@ -146,8 +146,7 @@ class TestCreate < ActiveSupport::TestCase
|
|
146
146
|
assignment2 = RoomAssignment.new(:student_id => student2.id, :dorm_id => room.dorm_id, :room_id => room.room_id)
|
147
147
|
assignment2.save!
|
148
148
|
|
149
|
-
room.
|
150
|
-
[assignment2.student_id, assignment2.dorm_id, assignment2.room_id]]
|
149
|
+
room.room_assignments = [assignment1,assignment2]
|
151
150
|
room.save!
|
152
151
|
|
153
152
|
assert_equal(2, room.room_assignments.length)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: composite_primary_keys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 8.1.
|
4
|
+
version: 8.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Charlie Savage
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-05-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - '='
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 0.4.4
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.9.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.9.0
|
69
83
|
description: Composite key support for ActiveRecord
|
70
84
|
email:
|
71
85
|
executables: []
|
@@ -271,7 +285,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
271
285
|
version: '0'
|
272
286
|
requirements: []
|
273
287
|
rubyforge_project:
|
274
|
-
rubygems_version: 2.6.
|
288
|
+
rubygems_version: 2.6.11
|
275
289
|
signing_key:
|
276
290
|
specification_version: 4
|
277
291
|
summary: Composite key support for ActiveRecord
|