composite_primary_keys 5.0.5 → 5.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,12 @@
1
+ == 5.0.6 2012-05-20
2
+ * Fixed bug where setting a belongs_to association to nil would raise an error (Tyler Rick)
3
+ * Remove special case code for 1 column selects and just choose to use Rails version of the
4
+ method unless we need ours to deal with an Array (Sammy Larbi)
5
+ * Change count tests to actually check for distinct, fix resulting error it
6
+ demonstrated in the code (Sammy Larbi)
7
+ * Specify mysql2 driver by default (Charlie Savage)
8
+ * Update sqlite tests to run on Windows (Charlie Savage)
9
+
1
10
  == 5.0.5 2012-05-05
2
11
  * Count without slower subquery in cases where that is possible (Sammy Larbi)
3
12
  * When you call association.build for a has_many association that uses a
@@ -105,6 +114,9 @@ by replacing quoted identifiers with all-caps equivalents on Oracle (Rhett Sutph
105
114
  * ActiveRecord 3.1 RC1 compatibility. This required a significant rewrite due to
106
115
  all the changes in AR 3.1 versus 3.0.
107
116
 
117
+ == 3.1.11 2012-05-20
118
+ Fix AssociationReflection#derive_primary_key for belongs_to relationships (Heinrich Lee Yu).
119
+
108
120
  == 3.1.10 2011-07-08
109
121
  * Bugfix for belongs_to with includes (John Ash)
110
122
  * Improved tests for calling clear on a habtm association, which involved (David Rueck)
@@ -4,6 +4,7 @@ module ActiveRecord
4
4
  def write_attribute(attr_name, value)
5
5
  # CPK
6
6
  if attr_name.kind_of?(Array)
7
+ value = [nil]*attr_name.length if value.nil?
7
8
  unless value.length == attr_name.length
8
9
  raise "Number of attr_names and values do not match"
9
10
  end
@@ -39,6 +39,7 @@ module CompositePrimaryKeys
39
39
  end
40
40
 
41
41
  def build_count_subquery(relation, column_name, distinct)
42
+ return super(relation, column_name, distinct) unless column_name.kind_of?(Array)
42
43
  # CPK
43
44
  # column_alias = Arel.sql('count_column')
44
45
  subquery_alias = Arel.sql('subquery_for_count')
@@ -46,24 +47,8 @@ module CompositePrimaryKeys
46
47
  # CPK
47
48
  # aliased_column = aggregate_column(column_name == :all ? 1 : column_name).as(column_alias)
48
49
  # relation.select_values = [aliased_column]
49
- relation.select_values = if column_name.kind_of?(Array)
50
- column_name.map do |column|
51
- Arel::Attribute.new(@klass.unscoped.table, column)
52
- end
53
- elsif @klass.column_names.include?(column_name.to_s)
54
- [Arel::Attribute.new(@klass.unscoped.table, column_name)]
55
- else
56
- [Arel.sql(column_name == :all ? "#{@klass.quoted_table_name}.*" : column_name.to_s)]
57
- end
58
-
59
- # do not use slower subquery if we're only counting on one column
60
- if relation.select_values.count == 1
61
- # exclude distinct table_name.* in case of joins where subbing * would change results
62
- if (!distinct && column_name == :all) || column_name != :all
63
- column = relation.select_values[0].gsub("#{@klass.quoted_table_name}.","")
64
- relation.select_values = [operation_over_aggregate_column(column, 'count', distinct)]
65
- return relation.arel
66
- end
50
+ relation.select_values = column_name.map do |column|
51
+ Arel::Attribute.new(@klass.unscoped.table, column)
67
52
  end
68
53
 
69
54
  relation.distinct(true)
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION
3
3
  MAJOR = 5
4
4
  MINOR = 0
5
- TINY = 5
5
+ TINY = 6
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -2,7 +2,7 @@ require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
2
2
  require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
3
3
 
4
4
  namespace :mysql do
5
- desc 'Create the MySQL test databases'
5
+ desc 'Create the MySQL test database'
6
6
  task :create_database do
7
7
  ActiveRecord::Base.clear_all_connections!
8
8
  spec = CompositePrimaryKeys::ConnectionSpec['mysql'].dup
@@ -12,8 +12,8 @@ namespace :mysql do
12
12
  ActiveRecord::Base.clear_all_connections!
13
13
  end
14
14
 
15
- desc 'Build the MySQL test databases'
16
- task :build_databases => [:create_database] do
15
+ desc 'Build the MySQL test database'
16
+ task :build_database => [:create_database] do
17
17
  path = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'mysql.sql')
18
18
  sql = File.open(path, 'rb') do |file|
19
19
  file.read
@@ -25,13 +25,13 @@ namespace :mysql do
25
25
  ActiveRecord::Base.connection.execute(sql)
26
26
  end
27
27
 
28
- desc 'Drop the MySQL test databases'
29
- task :drop_databases => :load_connection do
28
+ desc 'Drop the MySQL test database'
29
+ task :drop_database => :load_connection do
30
30
  ActiveRecord::Base.connection.drop_database(SPEC['database'])
31
31
  end
32
32
 
33
- desc 'Rebuild the MySQL test databases'
34
- task :rebuild_databases => [:drop_databases, :build_databases]
33
+ desc 'Rebuild the MySQL test database'
34
+ task :rebuild_database => [:drop_database, :build_database]
35
35
 
36
36
  task :load_connection do
37
37
  require File.join(PROJECT_ROOT, "test", "connections", "native_mysql", "connection")
@@ -2,22 +2,22 @@ require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
2
2
  require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
3
3
 
4
4
  namespace :oracle do
5
- desc 'Build the Oracle test databases'
6
- task :build_databases => :load_connection do
5
+ desc 'Build the Oracle test database'
6
+ task :build_database => :load_connection do
7
7
  options_str = connection_string
8
8
 
9
9
  schema = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'oracle.sql')
10
10
  sh %( sqlplus #{options_str} < #{schema} )
11
11
  end
12
12
 
13
- desc 'Drop the Oracle test databases'
14
- task :drop_databases => :load_connection do
13
+ desc 'Drop the Oracle test database'
14
+ task :drop_database => :load_connection do
15
15
  options_str = connection_string
16
16
  sh %( sqlplus #{options_str} < #{File.join(SCHEMA_PATH, 'oracle.drop.sql')} )
17
17
  end
18
18
 
19
- desc 'Rebuild the Oracle test databases'
20
- task :rebuild_databases => [:drop_databases, :build_databases]
19
+ desc 'Rebuild the Oracle test database'
20
+ task :rebuild_database => [:drop_database, :build_database]
21
21
 
22
22
  task :load_connection do
23
23
  require File.join(PROJECT_ROOT, "test", "connections", "native_oracle", "connection")
@@ -2,7 +2,7 @@ require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
2
2
  require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
3
3
 
4
4
  namespace :postgresql do
5
- desc 'Create the PostgreSQL test databases'
5
+ desc 'Create the PostgreSQL test database'
6
6
  task :create_database do
7
7
  ActiveRecord::Base.clear_all_connections!
8
8
  spec = CompositePrimaryKeys::ConnectionSpec['postgresql'].dup
@@ -13,8 +13,8 @@ namespace :postgresql do
13
13
  ActiveRecord::Base.clear_all_connections!
14
14
  end
15
15
 
16
- desc 'Build the PostgreSQL test databases'
17
- task :build_databases => [:create_database] do
16
+ desc 'Build the PostgreSQL test database'
17
+ task :build_database => [:create_database] do
18
18
  path = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'postgresql.sql')
19
19
  sql = File.open(path, 'rb') do |file|
20
20
  file.read
@@ -24,8 +24,8 @@ namespace :postgresql do
24
24
  ActiveRecord::Base.connection.execute(sql)
25
25
  end
26
26
 
27
- desc 'Drop the PostgreSQL test databases'
28
- task :drop_databases => :load_connection do
27
+ desc 'Drop the PostgreSQL test database'
28
+ task :drop_database => :load_connection do
29
29
  ActiveRecord::Base.clear_all_connections!
30
30
  spec = CompositePrimaryKeys::ConnectionSpec['postgresql'].dup
31
31
  database_name = spec.delete('database')
@@ -35,8 +35,8 @@ namespace :postgresql do
35
35
  ActiveRecord::Base.clear_all_connections!
36
36
  end
37
37
 
38
- desc 'Rebuild the PostgreSQL test databases'
39
- task :rebuild_databases => [:drop_databases, :build_databases]
38
+ desc 'Rebuild the PostgreSQL test database'
39
+ task :rebuild_database => [:drop_database, :build_database]
40
40
 
41
41
  task :load_connection do
42
42
  require File.join(PROJECT_ROOT, "test", "connections", "native_postgresql", "connection")
@@ -2,26 +2,24 @@ require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
2
2
  require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
3
3
 
4
4
  namespace :sqlite3 do
5
- desc 'Build the sqlite test databases'
6
- task :build_databases => :load_connection do
5
+ desc 'Build the sqlite test database'
6
+ task :build_database => :load_connection do
7
7
  schema = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'sqlite.sql')
8
8
  dbfile = File.join(PROJECT_ROOT, connection_string)
9
- cmd = "mkdir -p #{File.dirname(dbfile)}"
10
- puts cmd
11
- sh %{ #{cmd} }
9
+ FileUtils.mkdir_p(File.dirname(dbfile))
12
10
  cmd = "sqlite3 #{dbfile} < #{schema}"
13
11
  puts cmd
14
12
  sh %{ #{cmd} }
15
13
  end
16
14
 
17
- desc 'Drop the sqlite test databases'
18
- task :drop_databases => :load_connection do
15
+ desc 'Drop the sqlite test database'
16
+ task :drop_database => :load_connection do
19
17
  dbfile = connection_string
20
18
  sh %{ rm -f #{dbfile} }
21
19
  end
22
20
 
23
- desc 'Rebuild the sqlite test databases'
24
- task :rebuild_databases => [:drop_databases, :build_databases]
21
+ desc 'Rebuild the sqlite test database'
22
+ task :rebuild_database => [:drop_database, :build_database]
25
23
 
26
24
  task :load_connection do
27
25
  require File.join(PROJECT_ROOT, "test", "connections", "native_sqlite3", "connection")
@@ -3,13 +3,13 @@
3
3
  # 2. Update to match the databases you want to test against.
4
4
 
5
5
  mysql:
6
- adapter: mysql
6
+ adapter: mysql2
7
7
  username: root
8
8
  database: composite_primary_keys_unittest
9
9
 
10
10
  sqlite3:
11
11
  adapter: sqlite3
12
- database: db/composite_primary_keys_unittest.sqlite3
12
+ database: db/composite_primary_keys_unittest.sqlite
13
13
 
14
14
  postgresql:
15
15
  adapter: postgresql
@@ -5,12 +5,12 @@
5
5
  mysql:
6
6
  adapter: mysql2
7
7
  username: root
8
- password: mysql
8
+ password: root
9
9
  database: composite_primary_keys_unittest
10
10
 
11
11
  sqlite3:
12
12
  adapter: sqlite3
13
- database: db/composite_primary_keys_unittest.sqlite3
13
+ database: db/composite_primary_keys_unittest.sqlite
14
14
 
15
15
  postgresql:
16
16
  adapter: postgresql
@@ -1,7 +1,5 @@
1
- create sequence public.reference_types_seq start 1000;
2
-
3
1
  create table reference_types (
4
- reference_type_id int default nextval('public.reference_types_seq'),
2
+ reference_type_id serial not null,
5
3
  type_label varchar(50) default null,
6
4
  abbreviation varchar(50) default null,
7
5
  description varchar(50) default null,
@@ -17,10 +15,8 @@ create table reference_codes (
17
15
  primary key (reference_type_id, reference_code)
18
16
  );
19
17
 
20
- create sequence public.products_seq start 1000;
21
-
22
18
  create table products (
23
- id int not null default nextval('public.products_seq'),
19
+ id serial not null,
24
20
  name varchar(50) default null,
25
21
  primary key (id)
26
22
  );
@@ -46,46 +42,36 @@ create table suburbs (
46
42
  primary key (city_id, suburb_id)
47
43
  );
48
44
 
49
- create sequence public.streets_seq start 1000;
50
-
51
45
  create table streets (
52
- id int not null default nextval('public.streets_seq'),
46
+ id serial not null,
53
47
  city_id int not null,
54
48
  suburb_id int not null,
55
49
  name varchar(50) not null,
56
50
  primary key (id)
57
51
  );
58
52
 
59
- create sequence public.users_seq start 1000;
60
-
61
53
  create table users (
62
- id int not null default nextval('public.users_seq'),
54
+ id serial not null,
63
55
  name varchar(50) not null,
64
56
  primary key (id)
65
57
  );
66
58
 
67
- create sequence public.articles_seq start 1000;
68
-
69
59
  create table articles (
70
- id int not null default nextval('public.articles_seq'),
60
+ id serial not null,
71
61
  name varchar(50) not null,
72
62
  primary key (id)
73
63
  );
74
64
 
75
- create sequence public.readings_seq start 1000;
76
-
77
65
  create table readings (
78
- id int not null default nextval('public.readings_seq'),
66
+ id serial not null,
79
67
  user_id int not null,
80
68
  article_id int not null,
81
69
  rating int not null,
82
70
  primary key (id)
83
71
  );
84
72
 
85
- create sequence public.groups_seq start 1000;
86
-
87
73
  create table groups (
88
- id int not null default nextval('public.groups_seq'),
74
+ id serial not null,
89
75
  name varchar(50) not null,
90
76
  primary key (id)
91
77
  );
@@ -96,10 +82,8 @@ create table memberships (
96
82
  primary key (user_id, group_id)
97
83
  );
98
84
 
99
- create sequence public.membership_statuses_seq start 1000;
100
-
101
85
  create table membership_statuses (
102
- id int not null default nextval('public.membership_statuses_seq'),
86
+ id serial not null,
103
87
  user_id int not null,
104
88
  group_id int not null,
105
89
  status varchar(50) not null,
@@ -112,29 +96,23 @@ create table departments (
112
96
  primary key (department_id, location_id)
113
97
  );
114
98
 
115
- create sequence public.employees_seq start 1000;
116
-
117
99
  create table employees (
118
- id int not null default nextval('public.employees_seq'),
100
+ id serial not null,
119
101
  department_id int default null,
120
102
  location_id int default null,
121
103
  primary key (id)
122
104
  );
123
105
 
124
- create sequence public.comments_seq start 1000;
125
-
126
106
  create table comments (
127
- id int not null default nextval('public.comments_seq'),
107
+ id serial not null,
128
108
  person_id int default null,
129
109
  person_type varchar(100) default null,
130
110
  hack_id int default null,
131
111
  primary key (id)
132
112
  );
133
113
 
134
- create sequence public.hacks_seq start 1000;
135
-
136
114
  create table hacks (
137
- id int default nextval('public.hacks_seq'),
115
+ id serial not null,
138
116
  name varchar(50) not null,
139
117
  primary key (id)
140
118
  );
@@ -153,10 +131,8 @@ create table restaurants_suburbs (
153
131
  suburb_id int not null
154
132
  );
155
133
 
156
- create sequence public.dorms_seq start 1000;
157
-
158
134
  create table dorms (
159
- id int not null default nextval('public.dorms_seq'),
135
+ id serial not null,
160
136
  primary key (id)
161
137
  );
162
138
 
@@ -166,10 +142,8 @@ create table rooms (
166
142
  primary key (dorm_id, room_id)
167
143
  );
168
144
 
169
- create sequence public.room_attributes_seq start 1000;
170
-
171
145
  create table room_attributes (
172
- id int not null default nextval('public.room_attributes_seq'),
146
+ id serial not null,
173
147
  name varchar(50),
174
148
  primary key (id)
175
149
  );
@@ -180,10 +154,8 @@ create table room_attribute_assignments (
180
154
  room_attribute_id int not null
181
155
  );
182
156
 
183
- create sequence public.students_seq start 1000;
184
-
185
157
  create table students (
186
- id int not null default nextval('public.students_seq'),
158
+ id serial not null,
187
159
  primary key (id)
188
160
  );
189
161
 
@@ -210,5 +182,4 @@ create table products_restaurants (
210
182
  product_id int not null,
211
183
  franchise_id int not null,
212
184
  store_id int not null
213
- );
214
-
185
+ );
@@ -176,6 +176,13 @@ class TestAssociations < ActiveSupport::TestCase
176
176
  assert_equal({:department_id=>[1, 2]}, steve.changes)
177
177
  end
178
178
 
179
+ def test_composite_belongs_to__setting_to_nil
180
+ room_assignment = room_assignments(:jacksons_room)
181
+ # This was raising an error before:
182
+ # NoMethodError: undefined method `length' for nil:NilClass
183
+ assert_nothing_raised { room_assignment.room = nil }
184
+ end
185
+
179
186
  def test_has_one_with_composite
180
187
  # In this case a regular model has_one composite model
181
188
  department = departments(:engineering)
@@ -17,7 +17,12 @@ class TestCalculations < ActiveSupport::TestCase
17
17
 
18
18
  def test_count_distinct
19
19
  product = products(:first_product)
20
- assert_equal(2, product.product_tariffs.count(:distinct => true))
20
+ assert_equal(1, product.product_tariffs.select('tariff_start_date').count(:distinct => true))
21
+ end
22
+
23
+ def test_count_not_distinct
24
+ product = products(:first_product)
25
+ assert_equal(2, product.product_tariffs.select('tariff_start_date').count(:distinct => false))
21
26
  end
22
27
 
23
28
  def test_count_includes
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: 5.0.5
4
+ version: 5.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-05-05 00:00:00.000000000 Z
13
+ date: 2012-05-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -197,7 +197,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
197
  version: '0'
198
198
  requirements: []
199
199
  rubyforge_project: compositekeys
200
- rubygems_version: 1.8.21
200
+ rubygems_version: 1.8.24
201
201
  signing_key:
202
202
  specification_version: 3
203
203
  summary: Composite key support for ActiveRecord