composite_primary_keys 12.0.8 → 13.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.rdoc +888 -874
- data/README.rdoc +181 -180
- data/lib/composite_primary_keys/active_model/attribute_assignment.rb +19 -19
- data/lib/composite_primary_keys/associations/association_scope.rb +66 -68
- data/lib/composite_primary_keys/associations/join_dependency.rb +137 -103
- data/lib/composite_primary_keys/attribute_methods/primary_key.rb +0 -2
- data/lib/composite_primary_keys/attribute_methods/read.rb +30 -30
- data/lib/composite_primary_keys/attribute_methods/write.rb +35 -35
- data/lib/composite_primary_keys/attribute_methods.rb +21 -9
- data/lib/composite_primary_keys/base.rb +141 -141
- data/lib/composite_primary_keys/composite_predicates.rb +2 -1
- data/lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb +37 -22
- data/lib/composite_primary_keys/connection_adapters/sqlserver/database_statements.rb +44 -44
- data/lib/composite_primary_keys/core.rb +48 -48
- data/lib/composite_primary_keys/nested_attributes.rb +1 -1
- data/lib/composite_primary_keys/persistence.rb +82 -81
- data/lib/composite_primary_keys/reflection.rb +91 -29
- data/lib/composite_primary_keys/relation/batches.rb +15 -7
- data/lib/composite_primary_keys/relation/calculations.rb +104 -81
- data/lib/composite_primary_keys/relation/finder_methods.rb +235 -235
- data/lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb +39 -20
- data/lib/composite_primary_keys/relation/query_methods.rb +42 -42
- data/lib/composite_primary_keys/relation/where_clause.rb +18 -23
- data/lib/composite_primary_keys/relation.rb +197 -193
- data/lib/composite_primary_keys/table_metadata.rb +11 -0
- data/lib/composite_primary_keys/version.rb +8 -8
- data/lib/composite_primary_keys.rb +119 -119
- data/test/abstract_unit.rb +114 -114
- data/test/connections/databases.ci.yml +22 -22
- data/test/fixtures/db_definitions/db2-create-tables.sql +112 -112
- data/test/fixtures/db_definitions/db2-drop-tables.sql +16 -16
- data/test/fixtures/db_definitions/mysql.sql +180 -180
- data/test/fixtures/db_definitions/oracle.drop.sql +41 -41
- data/test/fixtures/db_definitions/oracle.sql +199 -199
- data/test/fixtures/db_definitions/postgresql.sql +182 -182
- data/test/fixtures/db_definitions/sqlite.sql +169 -169
- data/test/fixtures/db_definitions/sqlserver.sql +176 -176
- data/test/fixtures/department.rb +16 -16
- data/test/fixtures/departments.yml +19 -15
- data/test/fixtures/employees.yml +33 -28
- data/test/fixtures/membership.rb +8 -6
- data/test/fixtures/restaurants_suburbs.yml +10 -10
- data/test/fixtures/streets.yml +16 -16
- data/test/fixtures/suburbs.yml +14 -14
- data/test/fixtures/user.rb +11 -11
- data/test/test_associations.rb +372 -358
- data/test/test_attributes.rb +75 -60
- data/test/test_calculations.rb +49 -42
- data/test/test_create.rb +218 -206
- data/test/test_delete.rb +188 -179
- data/test/test_exists.rb +39 -39
- data/test/test_find.rb +170 -164
- data/test/test_ids.rb +112 -112
- data/test/test_nested_attributes.rb +67 -67
- data/test/test_update.rb +102 -96
- metadata +6 -6
- data/lib/composite_primary_keys/connection_adapters/mysql/database_statements.rb +0 -24
@@ -1,67 +1,67 @@
|
|
1
|
-
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
-
|
3
|
-
# Testing the find action on composite ActiveRecords with two primary keys
|
4
|
-
class TestNestedAttributes < ActiveSupport::TestCase
|
5
|
-
fixtures :reference_types, :reference_codes
|
6
|
-
|
7
|
-
def test_nested_atttribute_create
|
8
|
-
code_id = 1001
|
9
|
-
|
10
|
-
reference_type = reference_types(:name_prefix)
|
11
|
-
reference_type.update :reference_codes_attributes => [{
|
12
|
-
:reference_code => code_id,
|
13
|
-
:code_label => 'XX',
|
14
|
-
:abbreviation => 'Xx'
|
15
|
-
}]
|
16
|
-
assert_not_nil ReferenceCode.find_by_reference_code(code_id)
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_nested_atttribute_update
|
20
|
-
code_id = 1002
|
21
|
-
|
22
|
-
reference_type = reference_types(:name_prefix)
|
23
|
-
reference_type.update :reference_codes_attributes => [{
|
24
|
-
:reference_code => code_id,
|
25
|
-
:code_label => 'XX',
|
26
|
-
:abbreviation => 'Xx'
|
27
|
-
}]
|
28
|
-
|
29
|
-
reference_code = ReferenceCode.find_by_reference_code(code_id)
|
30
|
-
cpk = CompositePrimaryKeys::CompositeKeys[reference_type.reference_type_id, code_id]
|
31
|
-
reference_type.update :reference_codes_attributes => [{
|
32
|
-
:id => cpk,
|
33
|
-
:code_label => 'AAA',
|
34
|
-
:abbreviation => 'Aaa'
|
35
|
-
}]
|
36
|
-
|
37
|
-
reference_code = ReferenceCode.find_by_reference_code(code_id)
|
38
|
-
assert_kind_of(ReferenceCode, reference_code)
|
39
|
-
assert_equal(reference_code.code_label, 'AAA')
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_nested_atttribute_update_2
|
43
|
-
reference_type = reference_types(:gender)
|
44
|
-
reference_code = reference_codes(:gender_male)
|
45
|
-
|
46
|
-
reference_type.update(:reference_codes_attributes => [{:id => reference_code.id,
|
47
|
-
:code_label => 'XX',
|
48
|
-
:abbreviation => 'Xx'}])
|
49
|
-
|
50
|
-
reference_code.reload
|
51
|
-
assert_equal(reference_code.code_label, 'XX')
|
52
|
-
assert_equal(reference_code.abbreviation, 'Xx')
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_nested_atttribute_update_3
|
56
|
-
reference_type = reference_types(:gender)
|
57
|
-
reference_code = reference_codes(:gender_male)
|
58
|
-
|
59
|
-
reference_type.update(:reference_codes_attributes => [{:id => reference_code.id.to_s,
|
60
|
-
:code_label => 'XX',
|
61
|
-
:abbreviation => 'Xx'}])
|
62
|
-
|
63
|
-
reference_code.reload
|
64
|
-
assert_equal(reference_code.code_label, 'XX')
|
65
|
-
assert_equal(reference_code.abbreviation, 'Xx')
|
66
|
-
end
|
67
|
-
end
|
1
|
+
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
+
|
3
|
+
# Testing the find action on composite ActiveRecords with two primary keys
|
4
|
+
class TestNestedAttributes < ActiveSupport::TestCase
|
5
|
+
fixtures :reference_types, :reference_codes
|
6
|
+
|
7
|
+
def test_nested_atttribute_create
|
8
|
+
code_id = 1001
|
9
|
+
|
10
|
+
reference_type = reference_types(:name_prefix)
|
11
|
+
reference_type.update :reference_codes_attributes => [{
|
12
|
+
:reference_code => code_id,
|
13
|
+
:code_label => 'XX',
|
14
|
+
:abbreviation => 'Xx'
|
15
|
+
}]
|
16
|
+
assert_not_nil ReferenceCode.find_by_reference_code(code_id)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_nested_atttribute_update
|
20
|
+
code_id = 1002
|
21
|
+
|
22
|
+
reference_type = reference_types(:name_prefix)
|
23
|
+
reference_type.update :reference_codes_attributes => [{
|
24
|
+
:reference_code => code_id,
|
25
|
+
:code_label => 'XX',
|
26
|
+
:abbreviation => 'Xx'
|
27
|
+
}]
|
28
|
+
|
29
|
+
reference_code = ReferenceCode.find_by_reference_code(code_id)
|
30
|
+
cpk = CompositePrimaryKeys::CompositeKeys[reference_type.reference_type_id, code_id]
|
31
|
+
reference_type.update :reference_codes_attributes => [{
|
32
|
+
:id => cpk,
|
33
|
+
:code_label => 'AAA',
|
34
|
+
:abbreviation => 'Aaa'
|
35
|
+
}]
|
36
|
+
|
37
|
+
reference_code = ReferenceCode.find_by_reference_code(code_id)
|
38
|
+
assert_kind_of(ReferenceCode, reference_code)
|
39
|
+
assert_equal(reference_code.code_label, 'AAA')
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_nested_atttribute_update_2
|
43
|
+
reference_type = reference_types(:gender)
|
44
|
+
reference_code = reference_codes(:gender_male)
|
45
|
+
|
46
|
+
reference_type.update(:reference_codes_attributes => [{:id => reference_code.id,
|
47
|
+
:code_label => 'XX',
|
48
|
+
:abbreviation => 'Xx'}])
|
49
|
+
|
50
|
+
reference_code.reload
|
51
|
+
assert_equal(reference_code.code_label, 'XX')
|
52
|
+
assert_equal(reference_code.abbreviation, 'Xx')
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_nested_atttribute_update_3
|
56
|
+
reference_type = reference_types(:gender)
|
57
|
+
reference_code = reference_codes(:gender_male)
|
58
|
+
|
59
|
+
reference_type.update(:reference_codes_attributes => [{:id => reference_code.id.to_s,
|
60
|
+
:code_label => 'XX',
|
61
|
+
:abbreviation => 'Xx'}])
|
62
|
+
|
63
|
+
reference_code.reload
|
64
|
+
assert_equal(reference_code.code_label, 'XX')
|
65
|
+
assert_equal(reference_code.abbreviation, 'Xx')
|
66
|
+
end
|
67
|
+
end
|
data/test/test_update.rb
CHANGED
@@ -1,97 +1,103 @@
|
|
1
|
-
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
-
|
3
|
-
class TestUpdate < ActiveSupport::TestCase
|
4
|
-
fixtures :departments, :reference_types, :reference_codes, :rooms, :room_assignments
|
5
|
-
|
6
|
-
CLASSES = {
|
7
|
-
:single => {
|
8
|
-
:class => ReferenceType,
|
9
|
-
:primary_keys => :reference_type_id,
|
10
|
-
:update => { :description => 'RT Desc' },
|
11
|
-
},
|
12
|
-
:dual => {
|
13
|
-
:class => ReferenceCode,
|
14
|
-
:primary_keys => [:reference_type_id, :reference_code],
|
15
|
-
:update => { :description => 'RT Desc' },
|
16
|
-
},
|
17
|
-
}
|
18
|
-
|
19
|
-
def setup
|
20
|
-
self.class.classes = CLASSES
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_setup
|
24
|
-
testing_with do
|
25
|
-
assert_not_nil @klass_info[:update]
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_update_attributes
|
30
|
-
testing_with do
|
31
|
-
assert(@first.update(@klass_info[:update]))
|
32
|
-
assert(@first.reload)
|
33
|
-
@klass_info[:update].each_pair do |attr_name, new_value|
|
34
|
-
assert_equal(new_value, @first[attr_name])
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_update_attributes_with_id_field
|
40
|
-
department = departments(:accounting)
|
41
|
-
department.update_attribute(:location_id, 3)
|
42
|
-
department.reload
|
43
|
-
assert_equal(3, department.location_id)
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_update_primary_key
|
47
|
-
obj = ReferenceCode.find([1,1])
|
48
|
-
obj.reference_type_id = 2
|
49
|
-
obj.reference_code = 3
|
50
|
-
assert_equal({"reference_type_id" => 2, "reference_code" => 3}, obj.ids_hash)
|
51
|
-
assert(obj.save)
|
52
|
-
assert(obj.reload)
|
53
|
-
assert_equal(2, obj.reference_type_id)
|
54
|
-
assert_equal(3, obj.reference_code)
|
55
|
-
assert_equal({"reference_type_id" => 2, "reference_code" => 3}, obj.ids_hash)
|
56
|
-
assert_equal([2, 3], obj.id)
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_update_attribute
|
60
|
-
obj = ReferenceType.find(1)
|
61
|
-
obj[:abbreviation] = 'a'
|
62
|
-
obj['abbreviation'] = 'b'
|
63
|
-
assert(obj.save)
|
64
|
-
assert(obj.reload)
|
65
|
-
assert_equal('b', obj.abbreviation)
|
66
|
-
end
|
67
|
-
|
68
|
-
def test_update_all
|
69
|
-
ReferenceCode.update_all(description: 'random value')
|
70
|
-
|
71
|
-
ReferenceCode.all.each do |reference_code|
|
72
|
-
assert_equal('random value', reference_code.description)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_update_all_join
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
assignment
|
95
|
-
|
96
|
-
|
1
|
+
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
+
|
3
|
+
class TestUpdate < ActiveSupport::TestCase
|
4
|
+
fixtures :departments, :reference_types, :reference_codes, :rooms, :room_assignments
|
5
|
+
|
6
|
+
CLASSES = {
|
7
|
+
:single => {
|
8
|
+
:class => ReferenceType,
|
9
|
+
:primary_keys => :reference_type_id,
|
10
|
+
:update => { :description => 'RT Desc' },
|
11
|
+
},
|
12
|
+
:dual => {
|
13
|
+
:class => ReferenceCode,
|
14
|
+
:primary_keys => [:reference_type_id, :reference_code],
|
15
|
+
:update => { :description => 'RT Desc' },
|
16
|
+
},
|
17
|
+
}
|
18
|
+
|
19
|
+
def setup
|
20
|
+
self.class.classes = CLASSES
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_setup
|
24
|
+
testing_with do
|
25
|
+
assert_not_nil @klass_info[:update]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_update_attributes
|
30
|
+
testing_with do
|
31
|
+
assert(@first.update(@klass_info[:update]))
|
32
|
+
assert(@first.reload)
|
33
|
+
@klass_info[:update].each_pair do |attr_name, new_value|
|
34
|
+
assert_equal(new_value, @first[attr_name])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_update_attributes_with_id_field
|
40
|
+
department = departments(:accounting)
|
41
|
+
department.update_attribute(:location_id, 3)
|
42
|
+
department.reload
|
43
|
+
assert_equal(3, department.location_id)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_update_primary_key
|
47
|
+
obj = ReferenceCode.find([1,1])
|
48
|
+
obj.reference_type_id = 2
|
49
|
+
obj.reference_code = 3
|
50
|
+
assert_equal({"reference_type_id" => 2, "reference_code" => 3}, obj.ids_hash)
|
51
|
+
assert(obj.save)
|
52
|
+
assert(obj.reload)
|
53
|
+
assert_equal(2, obj.reference_type_id)
|
54
|
+
assert_equal(3, obj.reference_code)
|
55
|
+
assert_equal({"reference_type_id" => 2, "reference_code" => 3}, obj.ids_hash)
|
56
|
+
assert_equal([2, 3], obj.id)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_update_attribute
|
60
|
+
obj = ReferenceType.find(1)
|
61
|
+
obj[:abbreviation] = 'a'
|
62
|
+
obj['abbreviation'] = 'b'
|
63
|
+
assert(obj.save)
|
64
|
+
assert(obj.reload)
|
65
|
+
assert_equal('b', obj.abbreviation)
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_update_all
|
69
|
+
ReferenceCode.update_all(description: 'random value')
|
70
|
+
|
71
|
+
ReferenceCode.all.each do |reference_code|
|
72
|
+
assert_equal('random value', reference_code.description)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_update_all_join
|
77
|
+
tested_update_all = false
|
78
|
+
Arel::Table.engine = nil # should not rely on the global Arel::Table.engine
|
79
|
+
ReferenceCode.joins(:reference_type).
|
80
|
+
where('reference_types.reference_type_id = ?', 2).
|
81
|
+
update_all(:description => 'random value')
|
82
|
+
|
83
|
+
query = ReferenceCode.where('reference_type_id = ?', 2).
|
84
|
+
where(:description => 'random value')
|
85
|
+
|
86
|
+
assert_equal(2, query.count)
|
87
|
+
tested_update_all = true
|
88
|
+
ensure
|
89
|
+
Arel::Table.engine = ActiveRecord::Base
|
90
|
+
assert tested_update_all
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_update_with_uniqueness
|
94
|
+
assignment = room_assignments(:jacksons_room)
|
95
|
+
room_1 = rooms(:branner_room_1)
|
96
|
+
room_2 = rooms(:branner_room_3)
|
97
|
+
|
98
|
+
assert_equal(room_1, assignment.room)
|
99
|
+
assignment.room = room_2
|
100
|
+
assignment.save!
|
101
|
+
assert_equal(room_2, assignment.room)
|
102
|
+
end
|
97
103
|
end
|
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:
|
4
|
+
version: 13.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Charlie Savage
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 6.
|
19
|
+
version: 6.1.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 6.
|
26
|
+
version: 6.1.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -71,7 +71,6 @@ files:
|
|
71
71
|
- lib/composite_primary_keys/composite_relation.rb
|
72
72
|
- lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb
|
73
73
|
- lib/composite_primary_keys/connection_adapters/abstract_adapter.rb
|
74
|
-
- lib/composite_primary_keys/connection_adapters/mysql/database_statements.rb
|
75
74
|
- lib/composite_primary_keys/connection_adapters/postgresql/database_statements.rb
|
76
75
|
- lib/composite_primary_keys/connection_adapters/sqlserver/database_statements.rb
|
77
76
|
- lib/composite_primary_keys/core.rb
|
@@ -88,6 +87,7 @@ files:
|
|
88
87
|
- lib/composite_primary_keys/relation/query_methods.rb
|
89
88
|
- lib/composite_primary_keys/relation/where_clause.rb
|
90
89
|
- lib/composite_primary_keys/sanitization.rb
|
90
|
+
- lib/composite_primary_keys/table_metadata.rb
|
91
91
|
- lib/composite_primary_keys/transactions.rb
|
92
92
|
- lib/composite_primary_keys/validations/uniqueness.rb
|
93
93
|
- lib/composite_primary_keys/version.rb
|
@@ -216,7 +216,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
216
216
|
- !ruby/object:Gem::Version
|
217
217
|
version: '0'
|
218
218
|
requirements: []
|
219
|
-
rubygems_version: 3.2.
|
219
|
+
rubygems_version: 3.2.31
|
220
220
|
signing_key:
|
221
221
|
specification_version: 4
|
222
222
|
summary: Composite key support for ActiveRecord
|
@@ -1,24 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ActiveRecord
|
4
|
-
module ConnectionAdapters
|
5
|
-
module MySQL
|
6
|
-
module DatabaseStatements
|
7
|
-
def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
|
8
|
-
sql, binds = to_sql_and_binds(arel, binds)
|
9
|
-
value = exec_insert(sql, name, binds, pk, sequence_name)
|
10
|
-
|
11
|
-
# CPK
|
12
|
-
if pk.is_a?(Array)
|
13
|
-
pk.map do |key|
|
14
|
-
column = column_for(arel.ast.relation.name, key)
|
15
|
-
column.auto_increment? ? last_inserted_id(value) : nil
|
16
|
-
end
|
17
|
-
else
|
18
|
-
id_value || last_inserted_id(value)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|