activerecord 3.2.8 → 3.2.9.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (28) hide show
  1. data/CHANGELOG.md +154 -6558
  2. data/examples/performance.rb +32 -37
  3. data/lib/active_record/associations.rb +8 -5
  4. data/lib/active_record/associations/collection_association.rb +5 -3
  5. data/lib/active_record/associations/has_many_through_association.rb +14 -0
  6. data/lib/active_record/associations/has_one_association.rb +16 -14
  7. data/lib/active_record/associations/preloader.rb +14 -10
  8. data/lib/active_record/associations/preloader/association.rb +1 -3
  9. data/lib/active_record/attribute_methods/dirty.rb +3 -3
  10. data/lib/active_record/attribute_methods/time_zone_conversion.rb +14 -5
  11. data/lib/active_record/base.rb +4 -5
  12. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +10 -4
  13. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +1 -0
  14. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +1 -1
  15. data/lib/active_record/connection_adapters/column.rb +24 -9
  16. data/lib/active_record/connection_adapters/postgresql_adapter.rb +29 -33
  17. data/lib/active_record/connection_adapters/sqlite_adapter.rb +3 -3
  18. data/lib/active_record/counter_cache.rb +5 -1
  19. data/lib/active_record/explain_subscriber.rb +2 -1
  20. data/lib/active_record/migration.rb +2 -2
  21. data/lib/active_record/persistence.rb +9 -8
  22. data/lib/active_record/railties/databases.rake +6 -6
  23. data/lib/active_record/scoping/named.rb +2 -2
  24. data/lib/active_record/store.rb +2 -0
  25. data/lib/active_record/timestamp.rb +1 -0
  26. data/lib/active_record/transactions.rb +22 -3
  27. data/lib/active_record/version.rb +2 -2
  28. metadata +9 -12
@@ -75,7 +75,7 @@ module ActiveRecord
75
75
 
76
76
  case type
77
77
  when :string, :text then value
78
- when :integer then value.to_i rescue value ? 1 : 0
78
+ when :integer then value.to_i
79
79
  when :float then value.to_f
80
80
  when :decimal then klass.value_to_decimal(value)
81
81
  when :datetime, :timestamp then klass.string_to_time(value)
@@ -92,7 +92,7 @@ module ActiveRecord
92
92
 
93
93
  case type
94
94
  when :string, :text then var_name
95
- when :integer then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)"
95
+ when :integer then "(#{var_name}.to_i)"
96
96
  when :float then "#{var_name}.to_f"
97
97
  when :decimal then "#{klass}.value_to_decimal(#{var_name})"
98
98
  when :datetime, :timestamp then "#{klass}.string_to_time(#{var_name})"
@@ -150,7 +150,13 @@ module ActiveRecord
150
150
  return string unless string.is_a?(String)
151
151
  return nil if string.empty?
152
152
 
153
- string_to_time "2000-01-01 #{string}"
153
+ dummy_time_string = "2000-01-01 #{string}"
154
+
155
+ fast_string_to_time(dummy_time_string) || begin
156
+ time_hash = Date._parse(dummy_time_string)
157
+ return nil if time_hash[:hour].nil?
158
+ new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
159
+ end
154
160
  end
155
161
 
156
162
  # convert something to a boolean
@@ -180,7 +186,7 @@ module ActiveRecord
180
186
  # '0.123456' -> 123456
181
187
  # '1.123456' -> 123456
182
188
  def microseconds(time)
183
- ((time[:sec_fraction].to_f % 1) * 1_000_000).to_i
189
+ time[:sec_fraction] ? (time[:sec_fraction] * 1_000_000).to_i : 0
184
190
  end
185
191
 
186
192
  def new_date(year, mon, mday)
@@ -202,11 +208,20 @@ module ActiveRecord
202
208
  end
203
209
  end
204
210
 
205
- # Doesn't handle time zones.
206
- def fast_string_to_time(string)
207
- if string =~ Format::ISO_DATETIME
208
- microsec = ($7.to_f * 1_000_000).to_i
209
- new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
211
+ if RUBY_VERSION >= '1.9'
212
+ # Doesn't handle time zones.
213
+ def fast_string_to_time(string)
214
+ if string =~ Format::ISO_DATETIME
215
+ microsec = ($7.to_r * 1_000_000).to_i
216
+ new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
217
+ end
218
+ end
219
+ else
220
+ def fast_string_to_time(string)
221
+ if string =~ Format::ISO_DATETIME
222
+ microsec = ($7.to_f * 1_000_000).round.to_i
223
+ new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
224
+ end
210
225
  end
211
226
  end
212
227
 
@@ -145,11 +145,8 @@ module ActiveRecord
145
145
  when /\A\(?(-?\d+(\.\d*)?\)?)\z/
146
146
  $1
147
147
  # Character types
148
- when /\A'(.*)'::(?:character varying|bpchar|text)\z/m
148
+ when /\A\(?'(.*)'::.*\b(?:character varying|bpchar|text)\z/m
149
149
  $1
150
- # Character types (8.1 formatting)
151
- when /\AE'(.*)'::(?:character varying|bpchar|text)\z/m
152
- $1.gsub(/\\(\d\d\d)/) { $1.oct.chr }
153
150
  # Binary data types
154
151
  when /\A'(.*)'::bytea\z/m
155
152
  $1
@@ -354,6 +351,7 @@ module ActiveRecord
354
351
  def reconnect!
355
352
  clear_cache!
356
353
  @connection.reset
354
+ @open_transactions = 0
357
355
  configure_connection
358
356
  end
359
357
 
@@ -795,28 +793,28 @@ module ActiveRecord
795
793
  binds = [[nil, table]]
796
794
  binds << [nil, schema] if schema
797
795
 
798
- exec_query(<<-SQL, 'SCHEMA', binds).rows.first[0].to_i > 0
796
+ exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
799
797
  SELECT COUNT(*)
800
798
  FROM pg_class c
801
799
  LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
802
800
  WHERE c.relkind in ('v','r')
803
- AND c.relname = $1
804
- AND n.nspname = #{schema ? '$2' : 'ANY (current_schemas(false))'}
801
+ AND c.relname = '#{table.gsub(/(^"|"$)/,'')}'
802
+ AND n.nspname = #{schema ? "'#{schema}'" : 'ANY (current_schemas(false))'}
805
803
  SQL
806
804
  end
807
805
 
808
806
  # Returns true if schema exists.
809
807
  def schema_exists?(name)
810
- exec_query(<<-SQL, 'SCHEMA', [[nil, name]]).rows.first[0].to_i > 0
808
+ exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
811
809
  SELECT COUNT(*)
812
810
  FROM pg_namespace
813
- WHERE nspname = $1
811
+ WHERE nspname = '#{name}'
814
812
  SQL
815
813
  end
816
814
 
817
815
  # Returns an array of indexes for the given table.
818
816
  def indexes(table_name, name = nil)
819
- result = query(<<-SQL, name)
817
+ result = query(<<-SQL, 'SCHEMA')
820
818
  SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid
821
819
  FROM pg_class t
822
820
  INNER JOIN pg_index d ON t.oid = d.indrelid
@@ -836,7 +834,7 @@ module ActiveRecord
836
834
  inddef = row[3]
837
835
  oid = row[4]
838
836
 
839
- columns = Hash[query(<<-SQL, "Columns for index #{row[0]} on #{table_name}")]
837
+ columns = Hash[query(<<-SQL, "SCHEMA")]
840
838
  SELECT a.attnum, a.attname
841
839
  FROM pg_attribute a
842
840
  WHERE a.attrelid = #{oid}
@@ -848,7 +846,7 @@ module ActiveRecord
848
846
  # add info on sort order for columns (only desc order is explicitly specified, asc is the default)
849
847
  desc_order_columns = inddef.scan(/(\w+) DESC/).flatten
850
848
  orders = desc_order_columns.any? ? Hash[desc_order_columns.map {|order_column| [order_column, :desc]}] : {}
851
-
849
+
852
850
  column_names.empty? ? nil : IndexDefinition.new(table_name, index_name, unique, column_names, [], orders)
853
851
  end.compact
854
852
  end
@@ -863,7 +861,7 @@ module ActiveRecord
863
861
 
864
862
  # Returns the current database name.
865
863
  def current_database
866
- query('select current_database()')[0][0]
864
+ query('select current_database()', 'SCHEMA')[0][0]
867
865
  end
868
866
 
869
867
  # Returns the current schema name.
@@ -873,7 +871,7 @@ module ActiveRecord
873
871
 
874
872
  # Returns the current database encoding format.
875
873
  def encoding
876
- query(<<-end_sql)[0][0]
874
+ query(<<-end_sql, 'SCHEMA')[0][0]
877
875
  SELECT pg_encoding_to_char(pg_database.encoding) FROM pg_database
878
876
  WHERE pg_database.datname LIKE '#{current_database}'
879
877
  end_sql
@@ -914,8 +912,8 @@ module ActiveRecord
914
912
  end
915
913
 
916
914
  def serial_sequence(table, column)
917
- result = exec_query(<<-eosql, 'SCHEMA', [[nil, table], [nil, column]])
918
- SELECT pg_get_serial_sequence($1, $2)
915
+ result = exec_query(<<-eosql, 'SCHEMA')
916
+ SELECT pg_get_serial_sequence('#{table}', '#{column}')
919
917
  eosql
920
918
  result.rows.first.first
921
919
  end
@@ -936,7 +934,7 @@ module ActiveRecord
936
934
  if pk && sequence
937
935
  quoted_sequence = quote_table_name(sequence)
938
936
 
939
- select_value <<-end_sql, 'Reset sequence'
937
+ select_value <<-end_sql, 'SCHEMA'
940
938
  SELECT setval('#{quoted_sequence}', (SELECT COALESCE(MAX(#{quote_column_name pk})+(SELECT increment_by FROM #{quoted_sequence}), (SELECT min_value FROM #{quoted_sequence})) FROM #{quote_table_name(table)}), false)
941
939
  end_sql
942
940
  end
@@ -946,7 +944,7 @@ module ActiveRecord
946
944
  def pk_and_sequence_for(table) #:nodoc:
947
945
  # First try looking for a sequence with a dependency on the
948
946
  # given table's primary key.
949
- result = query(<<-end_sql, 'PK and serial sequence')[0]
947
+ result = query(<<-end_sql, 'SCHEMA')[0]
950
948
  SELECT attr.attname, seq.relname
951
949
  FROM pg_class seq,
952
950
  pg_attribute attr,
@@ -964,16 +962,13 @@ module ActiveRecord
964
962
  end_sql
965
963
 
966
964
  if result.nil? or result.empty?
967
- # If that fails, try parsing the primary key's default value.
968
- # Support the 7.x and 8.0 nextval('foo'::text) as well as
969
- # the 8.1+ nextval('foo'::regclass).
970
- result = query(<<-end_sql, 'PK and custom sequence')[0]
965
+ result = query(<<-end_sql, 'SCHEMA')[0]
971
966
  SELECT attr.attname,
972
967
  CASE
973
- WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN
974
- substr(split_part(def.adsrc, '''', 2),
975
- strpos(split_part(def.adsrc, '''', 2), '.')+1)
976
- ELSE split_part(def.adsrc, '''', 2)
968
+ WHEN split_part(pg_get_expr(def.adbin, def.adrelid), '''', 2) ~ '.' THEN
969
+ substr(split_part(pg_get_expr(def.adbin, def.adrelid), '''', 2),
970
+ strpos(split_part(pg_get_expr(def.adbin, def.adrelid), '''', 2), '.')+1)
971
+ ELSE split_part(pg_get_expr(def.adbin, def.adrelid), '''', 2)
977
972
  END
978
973
  FROM pg_class t
979
974
  JOIN pg_attribute attr ON (t.oid = attrelid)
@@ -981,7 +976,7 @@ module ActiveRecord
981
976
  JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])
982
977
  WHERE t.oid = '#{quote_table_name(table)}'::regclass
983
978
  AND cons.contype = 'p'
984
- AND def.adsrc ~* 'nextval'
979
+ AND pg_get_expr(def.adbin, def.adrelid) ~* 'nextval'
985
980
  end_sql
986
981
  end
987
982
 
@@ -992,13 +987,13 @@ module ActiveRecord
992
987
 
993
988
  # Returns just a table's primary key
994
989
  def primary_key(table)
995
- row = exec_query(<<-end_sql, 'SCHEMA', [[nil, table]]).rows.first
990
+ row = exec_query(<<-end_sql, 'SCHEMA').rows.first
996
991
  SELECT DISTINCT(attr.attname)
997
992
  FROM pg_attribute attr
998
993
  INNER JOIN pg_depend dep ON attr.attrelid = dep.refobjid AND attr.attnum = dep.refobjsubid
999
994
  INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]
1000
995
  WHERE cons.contype = 'p'
1001
- AND dep.refobjid = $1::regclass
996
+ AND dep.refobjid = '#{quote_table_name(table)}'::regclass
1002
997
  end_sql
1003
998
 
1004
999
  row && row.first
@@ -1085,7 +1080,7 @@ module ActiveRecord
1085
1080
  end
1086
1081
  when 'integer'
1087
1082
  return 'integer' unless limit
1088
-
1083
+
1089
1084
  case limit
1090
1085
  when 1, 2; 'smallint'
1091
1086
  when 3, 4; 'integer'
@@ -1108,7 +1103,7 @@ module ActiveRecord
1108
1103
 
1109
1104
  # Construct a clean list of column names from the ORDER BY clause, removing
1110
1105
  # any ASC/DESC modifiers
1111
- order_columns = orders.collect { |s| s.gsub(/\s+(ASC|DESC)\s*/i, '') }
1106
+ order_columns = orders.collect { |s| s.gsub(/\s+(ASC|DESC)\s*(NULLS\s+(FIRST|LAST)\s*)?/i, '') }
1112
1107
  order_columns.delete_if { |c| c.blank? }
1113
1108
  order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" }
1114
1109
 
@@ -1243,7 +1238,7 @@ module ActiveRecord
1243
1238
 
1244
1239
  # Returns the current ID of a table's sequence.
1245
1240
  def last_insert_id(sequence_name) #:nodoc:
1246
- r = exec_query("SELECT currval($1)", 'SQL', [[nil, sequence_name]])
1241
+ r = exec_query("SELECT currval('#{sequence_name}')", 'SQL')
1247
1242
  Integer(r.rows.first.first)
1248
1243
  end
1249
1244
 
@@ -1281,7 +1276,8 @@ module ActiveRecord
1281
1276
  # - ::regclass is a function that gives the id for a table name
1282
1277
  def column_definitions(table_name) #:nodoc:
1283
1278
  exec_query(<<-end_sql, 'SCHEMA').rows
1284
- SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
1279
+ SELECT a.attname, format_type(a.atttypid, a.atttypmod),
1280
+ pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
1285
1281
  FROM pg_attribute a LEFT JOIN pg_attrdef d
1286
1282
  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
1287
1283
  WHERE a.attrelid = '#{quote_table_name(table_name)}'::regclass
@@ -205,7 +205,7 @@ module ActiveRecord
205
205
  value = super
206
206
  if column.type == :string && value.encoding == Encoding::ASCII_8BIT
207
207
  logger.error "Binary data inserted for `string` type on column `#{column.name}`" if logger
208
- value.encode! 'utf-8'
208
+ value = value.encode Encoding::UTF_8
209
209
  end
210
210
  value
211
211
  end
@@ -359,12 +359,12 @@ module ActiveRecord
359
359
 
360
360
  # Returns an array of indexes for the given table.
361
361
  def indexes(table_name, name = nil) #:nodoc:
362
- exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", name).map do |row|
362
+ exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", 'SCHEMA').map do |row|
363
363
  IndexDefinition.new(
364
364
  table_name,
365
365
  row['name'],
366
366
  row['unique'] != 0,
367
- exec_query("PRAGMA index_info('#{row['name']}')").map { |col|
367
+ exec_query("PRAGMA index_info('#{row['name']}')", 'SCHEMA').map { |col|
368
368
  col['name']
369
369
  })
370
370
  end
@@ -25,10 +25,14 @@ module ActiveRecord
25
25
  self.name
26
26
  end
27
27
 
28
+ if has_many_association.is_a? ActiveRecord::Reflection::ThroughReflection
29
+ has_many_association = has_many_association.through_reflection
30
+ end
31
+
28
32
  foreign_key = has_many_association.foreign_key.to_s
29
33
  child_class = has_many_association.klass
30
34
  belongs_to = child_class.reflect_on_all_associations(:belongs_to)
31
- reflection = belongs_to.find { |e| e.foreign_key.to_s == foreign_key }
35
+ reflection = belongs_to.find { |e| e.foreign_key.to_s == foreign_key && e.options[:counter_cache].present? }
32
36
  counter_name = reflection.counter_cache_column
33
37
 
34
38
  stmt = unscoped.where(arel_table[primary_key].eq(object.id)).arel.compile_update({
@@ -15,8 +15,9 @@ module ActiveRecord
15
15
  # On the other hand, we want to monitor the performance of our real database
16
16
  # queries, not the performance of the access to the query cache.
17
17
  IGNORED_PAYLOADS = %w(SCHEMA EXPLAIN CACHE)
18
+ EXPLAINED_SQLS = /\A\s*(select|update|delete|insert)/i
18
19
  def ignore_payload?(payload)
19
- payload[:exception] || IGNORED_PAYLOADS.include?(payload[:name])
20
+ payload[:exception] || IGNORED_PAYLOADS.include?(payload[:name]) || payload[:sql] !~ EXPLAINED_SQLS
20
21
  end
21
22
 
22
23
  ActiveSupport::Notifications.subscribe("sql.active_record", new)
@@ -232,7 +232,7 @@ module ActiveRecord
232
232
  # add_column :people, :salary, :integer
233
233
  # Person.reset_column_information
234
234
  # Person.all.each do |p|
235
- # p.update_column :salary, SalaryCalculator.compute(p)
235
+ # p.update_attribute :salary, SalaryCalculator.compute(p)
236
236
  # end
237
237
  # end
238
238
  # end
@@ -252,7 +252,7 @@ module ActiveRecord
252
252
  # ...
253
253
  # say_with_time "Updating salaries..." do
254
254
  # Person.all.each do |p|
255
- # p.update_column :salary, SalaryCalculator.compute(p)
255
+ # p.update_attribute :salary, SalaryCalculator.compute(p)
256
256
  # end
257
257
  # end
258
258
  # ...
@@ -162,7 +162,7 @@ module ActiveRecord
162
162
  became.instance_variable_set("@new_record", new_record?)
163
163
  became.instance_variable_set("@destroyed", destroyed?)
164
164
  became.instance_variable_set("@errors", errors)
165
- became.type = klass.name unless self.class.descends_from_active_record?
165
+ became.send("#{klass.inheritance_column}=", klass.name) unless self.class.descends_from_active_record?
166
166
  became
167
167
  end
168
168
 
@@ -174,9 +174,6 @@ module ActiveRecord
174
174
  # * updated_at/updated_on column is updated if that column is available.
175
175
  # * Updates all the attributes that are dirty in this object.
176
176
  #
177
- # This method has been deprecated in favor of <tt>update_column</tt> due to
178
- # its similarity with <tt>update_attributes</tt>.
179
- #
180
177
  def update_attribute(name, value)
181
178
  name = name.to_s
182
179
  raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name)
@@ -196,8 +193,12 @@ module ActiveRecord
196
193
  name = name.to_s
197
194
  raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name)
198
195
  raise ActiveRecordError, "can not update on a new record object" unless persisted?
196
+
197
+ updated_count = self.class.update_all({ name => value }, self.class.primary_key => id) == 1
198
+
199
199
  raw_write_attribute(name, value)
200
- self.class.update_all({ name => value }, self.class.primary_key => id) == 1
200
+
201
+ updated_count == 1
201
202
  end
202
203
 
203
204
  # Updates the attributes of the model from the passed-in hash and saves the
@@ -242,7 +243,7 @@ module ActiveRecord
242
243
  # Saving is not subjected to validation checks. Returns +true+ if the
243
244
  # record could be saved.
244
245
  def increment!(attribute, by = 1)
245
- increment(attribute, by).update_column(attribute, self[attribute])
246
+ increment(attribute, by).update_attribute(attribute, self[attribute])
246
247
  end
247
248
 
248
249
  # Initializes +attribute+ to zero if +nil+ and subtracts the value passed as +by+ (default is 1).
@@ -259,7 +260,7 @@ module ActiveRecord
259
260
  # Saving is not subjected to validation checks. Returns +true+ if the
260
261
  # record could be saved.
261
262
  def decrement!(attribute, by = 1)
262
- decrement(attribute, by).update_column(attribute, self[attribute])
263
+ decrement(attribute, by).update_attribute(attribute, self[attribute])
263
264
  end
264
265
 
265
266
  # Assigns to +attribute+ the boolean opposite of <tt>attribute?</tt>. So
@@ -276,7 +277,7 @@ module ActiveRecord
276
277
  # Saving is not subjected to validation checks. Returns +true+ if the
277
278
  # record could be saved.
278
279
  def toggle!(attribute)
279
- toggle(attribute).update_column(attribute, self[attribute])
280
+ toggle(attribute).update_attribute(attribute, self[attribute])
280
281
  end
281
282
 
282
283
  # Reloads the attributes of this object from the database.
@@ -44,7 +44,7 @@ db_namespace = namespace :db do
44
44
  def mysql_creation_options(config)
45
45
  @charset = ENV['CHARSET'] || 'utf8'
46
46
  @collation = ENV['COLLATION'] || 'utf8_unicode_ci'
47
- {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
47
+ {:charset => (config['encoding'] || @charset), :collation => (config['collation'] || @collation)}
48
48
  end
49
49
 
50
50
  def create_database(config)
@@ -96,8 +96,8 @@ db_namespace = namespace :db do
96
96
  ActiveRecord::Base.establish_connection(config)
97
97
  else
98
98
  $stderr.puts sqlerr.error
99
- $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation}"
100
- $stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
99
+ $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['encoding'] || @charset}, collation: #{config['collation'] || @collation}"
100
+ $stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['encoding']
101
101
  end
102
102
  end
103
103
  when /postgresql/
@@ -369,7 +369,7 @@ db_namespace = namespace :db do
369
369
  end
370
370
  end
371
371
 
372
- task :load_if_ruby => 'db:create' do
372
+ task :load_if_ruby => ['db:create', :environment] do
373
373
  db_namespace["schema:load"].invoke if ActiveRecord::Base.schema_format == :ruby
374
374
  end
375
375
  end
@@ -412,7 +412,7 @@ db_namespace = namespace :db do
412
412
 
413
413
  # desc "Recreate the databases from the structure.sql file"
414
414
  task :load => [:environment, :load_config] do
415
- env = ENV['RAILS_ENV'] || 'test'
415
+ env = Rails.env
416
416
 
417
417
  abcs = ActiveRecord::Base.configurations
418
418
  filename = ENV['DB_STRUCTURE'] || File.join(Rails.root, "db", "structure.sql")
@@ -445,7 +445,7 @@ db_namespace = namespace :db do
445
445
  end
446
446
  end
447
447
 
448
- task :load_if_sql => 'db:create' do
448
+ task :load_if_sql => ['db:create', :environment] do
449
449
  db_namespace["structure:load"].invoke if ActiveRecord::Base.schema_format == :sql
450
450
  end
451
451
  end
@@ -34,7 +34,7 @@ module ActiveRecord
34
34
  if current_scope
35
35
  current_scope.clone
36
36
  else
37
- scope = relation.clone
37
+ scope = relation
38
38
  scope.default_scoped = true
39
39
  scope
40
40
  end
@@ -48,7 +48,7 @@ module ActiveRecord
48
48
  if current_scope
49
49
  current_scope.scope_for_create
50
50
  else
51
- scope = relation.clone
51
+ scope = relation
52
52
  scope.default_scoped = true
53
53
  scope.scope_for_create
54
54
  end