composite_primary_keys 1.0.8 → 1.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +10 -0
- data/lib/composite_primary_keys/association_preload.rb +67 -61
- data/lib/composite_primary_keys/associations.rb +428 -409
- data/lib/composite_primary_keys/attribute_methods.rb +3 -3
- data/lib/composite_primary_keys/base.rb +128 -110
- data/lib/composite_primary_keys/calculations.rb +8 -8
- data/lib/composite_primary_keys/composite_arrays.rb +6 -5
- data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +44 -2
- data/lib/composite_primary_keys/fixtures.rb +2 -3
- data/lib/composite_primary_keys/version.rb +1 -1
- data/scripts/console.rb +34 -11
- data/test/fixtures/db_definitions/db2-create-tables.sql +2 -2
- data/test/fixtures/db_definitions/mysql.sql +156 -155
- data/test/fixtures/db_definitions/oracle.drop.sql +4 -1
- data/test/fixtures/db_definitions/oracle.sql +117 -119
- data/test/fixtures/db_definitions/postgresql.sql +164 -98
- data/test/fixtures/db_definitions/sqlite.sql +74 -73
- data/test/fixtures/street.rb +2 -2
- data/test/fixtures/streets.yml +14 -14
- data/test/test_associations.rb +0 -5
- data/test/test_attribute_methods.rb +2 -2
- data/test/test_create.rb +68 -68
- data/test/test_delete.rb +19 -10
- data/test/test_ids.rb +7 -0
- data/tmp/test.db +0 -0
- data/website/template.js +3 -3
- metadata +2 -2
@@ -1,108 +1,108 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
create table reference_types (
|
2
|
+
reference_type_id integer primary key,
|
3
|
+
type_label varchar(50) default null,
|
4
|
+
abbreviation varchar(50) default null,
|
5
|
+
description varchar(50) default null
|
6
6
|
);
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
8
|
+
create table reference_codes (
|
9
|
+
reference_type_id int(11),
|
10
|
+
reference_code int(11) not null,
|
11
|
+
code_label varchar(50) default null,
|
12
|
+
abbreviation varchar(50) default null,
|
13
|
+
description varchar(50) default null,
|
14
|
+
primary key (reference_type_id, reference_code)
|
15
15
|
);
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
create table products (
|
18
|
+
id int(11) not null primary key,
|
19
|
+
name varchar(50) default null
|
20
20
|
);
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
22
|
+
create table tariffs (
|
23
|
+
tariff_id int(11) not null,
|
24
|
+
start_date date not null,
|
25
|
+
amount integer(11) default null,
|
26
|
+
primary key (tariff_id, start_date)
|
27
27
|
);
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
29
|
+
create table product_tariffs (
|
30
|
+
product_id int(11) not null,
|
31
|
+
tariff_id int(11) not null,
|
32
|
+
tariff_start_date date not null,
|
33
|
+
primary key (product_id, tariff_id, tariff_start_date)
|
34
34
|
);
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
36
|
+
create table suburbs (
|
37
|
+
city_id int(11) not null,
|
38
|
+
suburb_id int(11) not null,
|
39
|
+
name varchar(50) not null,
|
40
|
+
primary key (city_id, suburb_id)
|
41
41
|
);
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
43
|
+
create table streets (
|
44
|
+
id integer not null primary key autoincrement,
|
45
|
+
city_id int(11) not null,
|
46
|
+
suburb_id int(11) not null,
|
47
|
+
name varchar(50) not null
|
48
48
|
);
|
49
49
|
|
50
|
-
|
51
|
-
|
52
|
-
|
50
|
+
create table users (
|
51
|
+
id integer not null primary key autoincrement,
|
52
|
+
name varchar(50) not null
|
53
53
|
);
|
54
54
|
|
55
|
-
|
56
|
-
|
57
|
-
|
55
|
+
create table articles (
|
56
|
+
id integer not null primary key autoincrement,
|
57
|
+
name varchar(50) not null
|
58
58
|
);
|
59
59
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
60
|
+
create table readings (
|
61
|
+
id integer not null primary key autoincrement,
|
62
|
+
user_id int(11) not null,
|
63
|
+
article_id int(11) not null,
|
64
|
+
rating int(11) not null
|
65
65
|
);
|
66
66
|
|
67
|
-
|
68
|
-
|
69
|
-
|
67
|
+
create table groups (
|
68
|
+
id integer not null primary key autoincrement,
|
69
|
+
name varchar(50) not null
|
70
70
|
);
|
71
71
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
72
|
+
create table memberships (
|
73
|
+
user_id int not null,
|
74
|
+
group_id int not null,
|
75
|
+
primary key (user_id, group_id)
|
76
76
|
);
|
77
77
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
status varchar(50)
|
78
|
+
create table membership_statuses (
|
79
|
+
id integer not null primary key autoincrement,
|
80
|
+
user_id int not null,
|
81
|
+
group_id int not null,
|
82
|
+
status varchar(50) not null
|
83
83
|
);
|
84
84
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
85
|
+
create table departments (
|
86
|
+
department_id integer not null,
|
87
|
+
location_id integer not null,
|
88
|
+
primary key (department_id, location_id)
|
89
89
|
);
|
90
90
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
91
|
+
create table employees (
|
92
|
+
id integer not null primary key autoincrement,
|
93
|
+
department_id integer null,
|
94
|
+
location_id integer null
|
95
95
|
);
|
96
96
|
|
97
|
-
|
98
|
-
id
|
99
|
-
person_id varchar(100)
|
100
|
-
person_type varchar(100)
|
101
|
-
hack_id varchar(100)
|
97
|
+
create table comments (
|
98
|
+
id integer not null primary key autoincrement,
|
99
|
+
person_id varchar(100) null,
|
100
|
+
person_type varchar(100) null,
|
101
|
+
hack_id varchar(100) null
|
102
102
|
);
|
103
103
|
|
104
|
-
|
105
|
-
|
104
|
+
create table hacks (
|
105
|
+
name varchar(50) not null primary key
|
106
106
|
);
|
107
107
|
|
108
108
|
create table kitchen_sinks (
|
@@ -157,3 +157,4 @@ create table room_assignments (
|
|
157
157
|
dorm_id integer not null,
|
158
158
|
room_id integer not null
|
159
159
|
);
|
160
|
+
|
data/test/fixtures/street.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
class Street < ActiveRecord::Base
|
2
|
-
belongs_to :suburb, :foreign_key => [:city_id, :suburb_id]
|
1
|
+
class Street < ActiveRecord::Base
|
2
|
+
belongs_to :suburb, :foreign_key => [:city_id, :suburb_id]
|
3
3
|
end
|
data/test/fixtures/streets.yml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
first:
|
2
|
-
id: 1
|
3
|
-
city_id: 1
|
4
|
-
suburb_id: 1
|
5
|
-
name: First Street
|
6
|
-
second1:
|
7
|
-
id: 2
|
8
|
-
city_id: 2
|
9
|
-
suburb_id: 1
|
10
|
-
name: First Street
|
11
|
-
second2:
|
12
|
-
id: 3
|
13
|
-
city_id: 2
|
14
|
-
suburb_id: 1
|
1
|
+
first:
|
2
|
+
id: 1
|
3
|
+
city_id: 1
|
4
|
+
suburb_id: 1
|
5
|
+
name: First Street
|
6
|
+
second1:
|
7
|
+
id: 2
|
8
|
+
city_id: 2
|
9
|
+
suburb_id: 1
|
10
|
+
name: First Street
|
11
|
+
second2:
|
12
|
+
id: 3
|
13
|
+
city_id: 2
|
14
|
+
suburb_id: 1
|
15
15
|
name: Second Street
|
data/test/test_associations.rb
CHANGED
@@ -19,11 +19,6 @@ class TestAssociations < Test::Unit::TestCase
|
|
19
19
|
fixtures :articles, :products, :tariffs, :product_tariffs, :suburbs, :streets, :restaurants, :restaurants_suburbs,
|
20
20
|
:dorms, :rooms, :room_attributes, :room_attribute_assignments, :students, :room_assignments, :users, :readings
|
21
21
|
|
22
|
-
def test_quoted_table_columns
|
23
|
-
assert_equal "product_tariffs.product_id,product_tariffs.tariff_id,product_tariffs.tariff_start_date",
|
24
|
-
ProductTariff.send(:quoted_table_columns, ProductTariff.primary_key)
|
25
|
-
end
|
26
|
-
|
27
22
|
def test_has_many_through_with_conditions_when_through_association_is_not_composite
|
28
23
|
user = User.find(:first)
|
29
24
|
assert_equal 1, user.articles.find(:all, :conditions => ["articles.name = ?", "Article One"]).size
|
@@ -16,7 +16,7 @@ class TestAttributeMethods < Test::Unit::TestCase
|
|
16
16
|
sink = KitchenSink.find(1,2)
|
17
17
|
assert_equal(1, sink.id_1)
|
18
18
|
assert_equal(2, sink.id_2)
|
19
|
-
assert_equal(Date.today, sink.a_date)
|
19
|
+
assert_equal(Date.today, sink.a_date.to_date)
|
20
20
|
assert_equal('string', sink.a_string)
|
21
21
|
end
|
22
|
-
end
|
22
|
+
end
|
data/test/test_create.rb
CHANGED
@@ -1,68 +1,68 @@
|
|
1
|
-
require 'abstract_unit'
|
2
|
-
require 'fixtures/reference_type'
|
3
|
-
require 'fixtures/reference_code'
|
4
|
-
require 'fixtures/street'
|
5
|
-
require 'fixtures/suburb'
|
6
|
-
|
7
|
-
class TestCreate < Test::Unit::TestCase
|
8
|
-
fixtures :reference_types, :reference_codes, :streets, :suburbs
|
9
|
-
|
10
|
-
CLASSES = {
|
11
|
-
:single => {
|
12
|
-
:class => ReferenceType,
|
13
|
-
:primary_keys => :reference_type_id,
|
14
|
-
:create => {:reference_type_id => 10, :type_label => 'NEW_TYPE', :abbreviation => 'New Type'}
|
15
|
-
},
|
16
|
-
:dual => {
|
17
|
-
:class => ReferenceCode,
|
18
|
-
:primary_keys => [:reference_type_id, :reference_code],
|
19
|
-
:create => {:reference_type_id => 1, :reference_code => 20, :code_label => 'NEW_CODE', :abbreviation => 'New Code'}
|
20
|
-
},
|
21
|
-
}
|
22
|
-
|
23
|
-
def setup
|
24
|
-
self.class.classes = CLASSES
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_setup
|
28
|
-
testing_with do
|
29
|
-
assert_not_nil @klass_info[:create]
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_create
|
34
|
-
testing_with do
|
35
|
-
assert new_obj = @klass.create(@klass_info[:create])
|
36
|
-
assert !new_obj.new_record?
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_create_no_id
|
41
|
-
testing_with do
|
42
|
-
begin
|
43
|
-
@obj = @klass.create(@klass_info[:create].block(@klass.primary_key))
|
44
|
-
@successful = !composite?
|
45
|
-
rescue CompositePrimaryKeys::ActiveRecord::CompositeKeyError
|
46
|
-
@successful = false
|
47
|
-
rescue
|
48
|
-
flunk "Incorrect exception raised: #{$!}, #{$!.class}"
|
49
|
-
end
|
50
|
-
assert_equal composite?, !@successful, "Create should have failed for composites; #{@obj.inspect}"
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_create_on_association
|
55
|
-
suburb = Suburb.find(:first)
|
56
|
-
suburb.streets.create(:
|
57
|
-
street = Street.find_by_name('my street')
|
58
|
-
assert_equal(suburb.city_id, street.city_id)
|
59
|
-
assert_equal(suburb.suburb_id, street.suburb_id)
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_create_on_association_when_belongs_to_is_single_key
|
63
|
-
rt = ReferenceType.find(:first)
|
64
|
-
rt.reference_codes.create(:
|
65
|
-
rc = ReferenceCode.find_by_reference_code(4321)
|
66
|
-
assert_equal(rc.reference_type_id, rt.reference_type_id)
|
67
|
-
end
|
68
|
-
end
|
1
|
+
require 'abstract_unit'
|
2
|
+
require 'fixtures/reference_type'
|
3
|
+
require 'fixtures/reference_code'
|
4
|
+
require 'fixtures/street'
|
5
|
+
require 'fixtures/suburb'
|
6
|
+
|
7
|
+
class TestCreate < Test::Unit::TestCase
|
8
|
+
fixtures :reference_types, :reference_codes, :streets, :suburbs
|
9
|
+
|
10
|
+
CLASSES = {
|
11
|
+
:single => {
|
12
|
+
:class => ReferenceType,
|
13
|
+
:primary_keys => :reference_type_id,
|
14
|
+
:create => {:reference_type_id => 10, :type_label => 'NEW_TYPE', :abbreviation => 'New Type'}
|
15
|
+
},
|
16
|
+
:dual => {
|
17
|
+
:class => ReferenceCode,
|
18
|
+
:primary_keys => [:reference_type_id, :reference_code],
|
19
|
+
:create => {:reference_type_id => 1, :reference_code => 20, :code_label => 'NEW_CODE', :abbreviation => 'New Code'}
|
20
|
+
},
|
21
|
+
}
|
22
|
+
|
23
|
+
def setup
|
24
|
+
self.class.classes = CLASSES
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_setup
|
28
|
+
testing_with do
|
29
|
+
assert_not_nil @klass_info[:create]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_create
|
34
|
+
testing_with do
|
35
|
+
assert new_obj = @klass.create(@klass_info[:create])
|
36
|
+
assert !new_obj.new_record?
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_create_no_id
|
41
|
+
testing_with do
|
42
|
+
begin
|
43
|
+
@obj = @klass.create(@klass_info[:create].block(@klass.primary_key))
|
44
|
+
@successful = !composite?
|
45
|
+
rescue CompositePrimaryKeys::ActiveRecord::CompositeKeyError
|
46
|
+
@successful = false
|
47
|
+
rescue
|
48
|
+
flunk "Incorrect exception raised: #{$!}, #{$!.class}"
|
49
|
+
end
|
50
|
+
assert_equal composite?, !@successful, "Create should have failed for composites; #{@obj.inspect}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_create_on_association
|
55
|
+
suburb = Suburb.find(:first)
|
56
|
+
suburb.streets.create(:name => "my street")
|
57
|
+
street = Street.find_by_name('my street')
|
58
|
+
assert_equal(suburb.city_id, street.city_id)
|
59
|
+
assert_equal(suburb.suburb_id, street.suburb_id)
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_create_on_association_when_belongs_to_is_single_key
|
63
|
+
rt = ReferenceType.find(:first)
|
64
|
+
rt.reference_codes.create(:reference_code => 4321, :code_label => 'foo', :abbreviation => 'bar')
|
65
|
+
rc = ReferenceCode.find_by_reference_code(4321)
|
66
|
+
assert_equal(rc.reference_type_id, rt.reference_type_id)
|
67
|
+
end
|
68
|
+
end
|
data/test/test_delete.rb
CHANGED
@@ -65,23 +65,32 @@ class TestDelete < Test::Unit::TestCase
|
|
65
65
|
@klass.delete_all
|
66
66
|
end
|
67
67
|
end
|
68
|
-
|
68
|
+
|
69
69
|
def test_clear_association
|
70
70
|
department = Department.find(1,1)
|
71
|
-
assert_equal 2, department.employees.size, "
|
71
|
+
assert_equal 2, department.employees.size, "Before clear employee count should be 2."
|
72
72
|
department.employees.clear
|
73
|
-
assert_equal 0, department.employees.size, "After clear
|
73
|
+
assert_equal 0, department.employees.size, "After clear employee count should be 0."
|
74
74
|
department.reload
|
75
|
-
assert_equal 0, department.employees.size, "After clear
|
75
|
+
assert_equal 0, department.employees.size, "After clear and a reload from DB employee count should be 0."
|
76
76
|
end
|
77
|
-
|
77
|
+
|
78
78
|
def test_delete_association
|
79
79
|
department = Department.find(1,1)
|
80
|
-
assert_equal 2, department.employees.size , "
|
81
|
-
|
82
|
-
department.employees.delete(first_employee)
|
83
|
-
assert_equal 1, department.employees.size, "After delete
|
80
|
+
assert_equal 2, department.employees.size , "Before delete employee count should be 2."
|
81
|
+
first_employee = department.employees[0]
|
82
|
+
department.employees.delete(first_employee)
|
83
|
+
assert_equal 1, department.employees.size, "After delete employee count should be 1."
|
84
84
|
department.reload
|
85
|
-
assert_equal 1, department.employees.size, "After delete
|
85
|
+
assert_equal 1, department.employees.size, "After delete and a reload from DB employee count should be 1."
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_delete_records_for_has_many_association_with_composite_primary_key
|
89
|
+
reference_type = ReferenceType.find(1)
|
90
|
+
codes_to_delete = reference_type.reference_codes[0..1]
|
91
|
+
assert_equal 3, reference_type.reference_codes.size, "Before deleting records reference_code count should be 3."
|
92
|
+
reference_type.reference_codes.delete_records(codes_to_delete)
|
93
|
+
reference_type.reload
|
94
|
+
assert_equal 1, reference_type.reference_codes.size, "After deleting 2 records and a reload from DB reference_code count should be 1."
|
86
95
|
end
|
87
96
|
end
|
data/test/test_ids.rb
CHANGED
@@ -46,6 +46,13 @@ class TestIds < Test::Unit::TestCase
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
+
def test_composite_where_clause
|
50
|
+
testing_with do
|
51
|
+
where = 'reference_codes.reference_type_id=1 AND reference_codes.reference_code=2) OR (reference_codes.reference_type_id=2 AND reference_codes.reference_code=2'
|
52
|
+
assert_equal(where, @klass.composite_where_clause([[1, 2], [2, 2]])) if @key_test == :dual
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
49
56
|
def test_set_ids_string
|
50
57
|
testing_with do
|
51
58
|
array = @primary_keys.collect {|key| 5}
|
data/tmp/test.db
CHANGED
Binary file
|
data/website/template.js
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
// <%= title %>
|
2
|
-
var version = <%= version.to_json %>;
|
3
|
-
<%= body %>
|
1
|
+
// <%= title %>
|
2
|
+
var version = <%= version.to_json %>;
|
3
|
+
<%= body %>
|