activerecord 5.0.7.2 → 5.1.0.beta1

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 (216) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +389 -2252
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +1 -1
  5. data/examples/performance.rb +28 -28
  6. data/examples/simple.rb +3 -3
  7. data/lib/active_record.rb +20 -20
  8. data/lib/active_record/aggregations.rb +244 -244
  9. data/lib/active_record/association_relation.rb +5 -5
  10. data/lib/active_record/associations.rb +1579 -1569
  11. data/lib/active_record/associations/alias_tracker.rb +1 -1
  12. data/lib/active_record/associations/association.rb +23 -15
  13. data/lib/active_record/associations/association_scope.rb +83 -81
  14. data/lib/active_record/associations/belongs_to_association.rb +0 -1
  15. data/lib/active_record/associations/builder/belongs_to.rb +16 -14
  16. data/lib/active_record/associations/builder/collection_association.rb +1 -2
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +27 -27
  18. data/lib/active_record/associations/collection_association.rb +74 -241
  19. data/lib/active_record/associations/collection_proxy.rb +144 -70
  20. data/lib/active_record/associations/has_many_association.rb +15 -19
  21. data/lib/active_record/associations/has_many_through_association.rb +12 -5
  22. data/lib/active_record/associations/has_one_association.rb +22 -28
  23. data/lib/active_record/associations/has_one_through_association.rb +5 -1
  24. data/lib/active_record/associations/join_dependency.rb +117 -115
  25. data/lib/active_record/associations/join_dependency/join_association.rb +16 -13
  26. data/lib/active_record/associations/join_dependency/join_base.rb +1 -1
  27. data/lib/active_record/associations/join_dependency/join_part.rb +1 -1
  28. data/lib/active_record/associations/preloader.rb +94 -94
  29. data/lib/active_record/associations/preloader/association.rb +87 -64
  30. data/lib/active_record/associations/preloader/belongs_to.rb +0 -2
  31. data/lib/active_record/associations/preloader/collection_association.rb +6 -6
  32. data/lib/active_record/associations/preloader/has_many.rb +0 -2
  33. data/lib/active_record/associations/preloader/singular_association.rb +6 -8
  34. data/lib/active_record/associations/preloader/through_association.rb +34 -41
  35. data/lib/active_record/associations/singular_association.rb +8 -25
  36. data/lib/active_record/associations/through_association.rb +3 -6
  37. data/lib/active_record/attribute.rb +98 -71
  38. data/lib/active_record/attribute/user_provided_default.rb +4 -2
  39. data/lib/active_record/attribute_assignment.rb +61 -61
  40. data/lib/active_record/attribute_decorators.rb +35 -13
  41. data/lib/active_record/attribute_methods.rb +56 -65
  42. data/lib/active_record/attribute_methods/before_type_cast.rb +7 -7
  43. data/lib/active_record/attribute_methods/dirty.rb +216 -34
  44. data/lib/active_record/attribute_methods/primary_key.rb +78 -73
  45. data/lib/active_record/attribute_methods/read.rb +39 -35
  46. data/lib/active_record/attribute_methods/serialization.rb +7 -7
  47. data/lib/active_record/attribute_methods/time_zone_conversion.rb +35 -58
  48. data/lib/active_record/attribute_methods/write.rb +36 -30
  49. data/lib/active_record/attribute_mutation_tracker.rb +53 -10
  50. data/lib/active_record/attribute_set.rb +9 -6
  51. data/lib/active_record/attribute_set/builder.rb +41 -49
  52. data/lib/active_record/attribute_set/yaml_encoder.rb +41 -0
  53. data/lib/active_record/attributes.rb +21 -21
  54. data/lib/active_record/autosave_association.rb +13 -13
  55. data/lib/active_record/base.rb +24 -22
  56. data/lib/active_record/callbacks.rb +52 -14
  57. data/lib/active_record/coders/yaml_column.rb +9 -11
  58. data/lib/active_record/collection_cache_key.rb +6 -17
  59. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +320 -278
  60. data/lib/active_record/connection_adapters/abstract/database_limits.rb +1 -3
  61. data/lib/active_record/connection_adapters/abstract/database_statements.rb +22 -34
  62. data/lib/active_record/connection_adapters/abstract/query_cache.rb +31 -27
  63. data/lib/active_record/connection_adapters/abstract/quoting.rb +44 -57
  64. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +9 -19
  65. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +78 -79
  66. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +53 -41
  67. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +99 -93
  68. data/lib/active_record/connection_adapters/abstract/transaction.rb +1 -5
  69. data/lib/active_record/connection_adapters/abstract_adapter.rb +156 -128
  70. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +424 -382
  71. data/lib/active_record/connection_adapters/column.rb +27 -5
  72. data/lib/active_record/connection_adapters/connection_specification.rb +128 -118
  73. data/lib/active_record/connection_adapters/mysql/column.rb +6 -31
  74. data/lib/active_record/connection_adapters/mysql/database_statements.rb +45 -43
  75. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +22 -22
  76. data/lib/active_record/connection_adapters/mysql/quoting.rb +6 -12
  77. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +49 -45
  78. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +16 -19
  79. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +49 -31
  80. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +5 -6
  81. data/lib/active_record/connection_adapters/mysql2_adapter.rb +24 -26
  82. data/lib/active_record/connection_adapters/postgresql/column.rb +1 -28
  83. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +46 -35
  84. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +3 -3
  85. data/lib/active_record/connection_adapters/postgresql/oid.rb +22 -21
  86. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +9 -9
  87. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +5 -3
  88. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +1 -1
  89. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +1 -1
  90. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -2
  91. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +3 -3
  92. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +16 -16
  93. data/lib/active_record/connection_adapters/postgresql/oid/{rails_5_1_point.rb → legacy_point.rb} +9 -16
  94. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +2 -2
  95. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +13 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +28 -8
  97. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +28 -30
  98. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -1
  99. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +51 -51
  100. data/lib/active_record/connection_adapters/postgresql/quoting.rb +38 -36
  101. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +15 -0
  102. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +37 -24
  103. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +19 -23
  104. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +161 -170
  105. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +4 -4
  106. data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -7
  107. data/lib/active_record/connection_adapters/postgresql_adapter.rb +179 -152
  108. data/lib/active_record/connection_adapters/schema_cache.rb +16 -7
  109. data/lib/active_record/connection_adapters/sql_type_metadata.rb +3 -3
  110. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +1 -1
  111. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +16 -20
  112. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +1 -8
  113. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +28 -0
  114. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +17 -0
  115. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +187 -130
  116. data/lib/active_record/connection_adapters/statement_pool.rb +7 -7
  117. data/lib/active_record/connection_handling.rb +14 -26
  118. data/lib/active_record/core.rb +110 -93
  119. data/lib/active_record/counter_cache.rb +62 -13
  120. data/lib/active_record/define_callbacks.rb +20 -0
  121. data/lib/active_record/dynamic_matchers.rb +80 -79
  122. data/lib/active_record/enum.rb +8 -6
  123. data/lib/active_record/errors.rb +58 -15
  124. data/lib/active_record/explain.rb +1 -2
  125. data/lib/active_record/explain_registry.rb +1 -1
  126. data/lib/active_record/explain_subscriber.rb +7 -4
  127. data/lib/active_record/fixture_set/file.rb +11 -8
  128. data/lib/active_record/fixtures.rb +66 -53
  129. data/lib/active_record/gem_version.rb +3 -3
  130. data/lib/active_record/inheritance.rb +93 -79
  131. data/lib/active_record/integration.rb +7 -7
  132. data/lib/active_record/internal_metadata.rb +3 -16
  133. data/lib/active_record/legacy_yaml_adapter.rb +1 -1
  134. data/lib/active_record/locking/optimistic.rb +64 -56
  135. data/lib/active_record/locking/pessimistic.rb +10 -1
  136. data/lib/active_record/log_subscriber.rb +29 -29
  137. data/lib/active_record/migration.rb +155 -172
  138. data/lib/active_record/migration/command_recorder.rb +94 -94
  139. data/lib/active_record/migration/compatibility.rb +76 -37
  140. data/lib/active_record/migration/join_table.rb +6 -6
  141. data/lib/active_record/model_schema.rb +85 -119
  142. data/lib/active_record/nested_attributes.rb +200 -199
  143. data/lib/active_record/null_relation.rb +10 -33
  144. data/lib/active_record/persistence.rb +45 -38
  145. data/lib/active_record/query_cache.rb +4 -8
  146. data/lib/active_record/querying.rb +2 -3
  147. data/lib/active_record/railtie.rb +16 -17
  148. data/lib/active_record/railties/controller_runtime.rb +6 -2
  149. data/lib/active_record/railties/databases.rake +125 -140
  150. data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
  151. data/lib/active_record/readonly_attributes.rb +2 -2
  152. data/lib/active_record/reflection.rb +79 -96
  153. data/lib/active_record/relation.rb +72 -115
  154. data/lib/active_record/relation/batches.rb +87 -58
  155. data/lib/active_record/relation/batches/batch_enumerator.rb +1 -1
  156. data/lib/active_record/relation/calculations.rb +154 -160
  157. data/lib/active_record/relation/delegation.rb +30 -29
  158. data/lib/active_record/relation/finder_methods.rb +195 -226
  159. data/lib/active_record/relation/merger.rb +58 -62
  160. data/lib/active_record/relation/predicate_builder.rb +92 -89
  161. data/lib/active_record/relation/predicate_builder/array_handler.rb +7 -5
  162. data/lib/active_record/relation/predicate_builder/association_query_handler.rb +23 -23
  163. data/lib/active_record/relation/predicate_builder/base_handler.rb +3 -1
  164. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +0 -8
  165. data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +12 -10
  166. data/lib/active_record/relation/predicate_builder/range_handler.rb +0 -8
  167. data/lib/active_record/relation/query_attribute.rb +1 -1
  168. data/lib/active_record/relation/query_methods.rb +247 -295
  169. data/lib/active_record/relation/record_fetch_warning.rb +3 -3
  170. data/lib/active_record/relation/spawn_methods.rb +4 -5
  171. data/lib/active_record/relation/where_clause.rb +79 -65
  172. data/lib/active_record/relation/where_clause_factory.rb +47 -8
  173. data/lib/active_record/result.rb +29 -31
  174. data/lib/active_record/runtime_registry.rb +3 -3
  175. data/lib/active_record/sanitization.rb +182 -197
  176. data/lib/active_record/schema.rb +3 -3
  177. data/lib/active_record/schema_dumper.rb +14 -37
  178. data/lib/active_record/schema_migration.rb +3 -3
  179. data/lib/active_record/scoping.rb +9 -10
  180. data/lib/active_record/scoping/default.rb +87 -91
  181. data/lib/active_record/scoping/named.rb +16 -28
  182. data/lib/active_record/secure_token.rb +2 -2
  183. data/lib/active_record/statement_cache.rb +13 -15
  184. data/lib/active_record/store.rb +31 -32
  185. data/lib/active_record/suppressor.rb +2 -1
  186. data/lib/active_record/table_metadata.rb +9 -5
  187. data/lib/active_record/tasks/database_tasks.rb +72 -65
  188. data/lib/active_record/tasks/mysql_database_tasks.rb +75 -72
  189. data/lib/active_record/tasks/postgresql_database_tasks.rb +53 -48
  190. data/lib/active_record/tasks/sqlite_database_tasks.rb +18 -16
  191. data/lib/active_record/timestamp.rb +39 -25
  192. data/lib/active_record/touch_later.rb +1 -2
  193. data/lib/active_record/transactions.rb +98 -110
  194. data/lib/active_record/type.rb +17 -13
  195. data/lib/active_record/type/adapter_specific_registry.rb +46 -42
  196. data/lib/active_record/type/decimal_without_scale.rb +9 -0
  197. data/lib/active_record/type/hash_lookup_type_map.rb +3 -3
  198. data/lib/active_record/type/serialized.rb +8 -8
  199. data/lib/active_record/type/text.rb +9 -0
  200. data/lib/active_record/type/time.rb +0 -1
  201. data/lib/active_record/type/type_map.rb +11 -15
  202. data/lib/active_record/type/unsigned_integer.rb +15 -0
  203. data/lib/active_record/type_caster.rb +2 -2
  204. data/lib/active_record/type_caster/connection.rb +8 -6
  205. data/lib/active_record/type_caster/map.rb +3 -1
  206. data/lib/active_record/validations.rb +4 -4
  207. data/lib/active_record/validations/associated.rb +1 -1
  208. data/lib/active_record/validations/presence.rb +2 -2
  209. data/lib/active_record/validations/uniqueness.rb +8 -39
  210. data/lib/active_record/version.rb +1 -1
  211. data/lib/rails/generators/active_record.rb +4 -4
  212. data/lib/rails/generators/active_record/migration.rb +2 -2
  213. data/lib/rails/generators/active_record/migration/migration_generator.rb +37 -34
  214. data/lib/rails/generators/active_record/model/model_generator.rb +9 -9
  215. metadata +22 -13
  216. data/lib/active_record/relation/predicate_builder/class_handler.rb +0 -27
@@ -1,9 +1,5 @@
1
1
  module ActiveRecord
2
2
  module NullRelation # :nodoc:
3
- def exec_queries
4
- @records = [].freeze
5
- end
6
-
7
3
  def pluck(*column_names)
8
4
  []
9
5
  end
@@ -20,10 +16,6 @@ module ActiveRecord
20
16
  0
21
17
  end
22
18
 
23
- def size
24
- calculate :size, nil
25
- end
26
-
27
19
  def empty?
28
20
  true
29
21
  end
@@ -48,33 +40,12 @@ module ActiveRecord
48
40
  ""
49
41
  end
50
42
 
51
- def count(*)
52
- calculate :count, nil
53
- end
54
-
55
- def sum(*)
56
- calculate :sum, nil
57
- end
58
-
59
- def average(*)
60
- calculate :average, nil
61
- end
62
-
63
- def minimum(*)
64
- calculate :minimum, nil
65
- end
66
-
67
- def maximum(*)
68
- calculate :maximum, nil
69
- end
70
-
71
43
  def calculate(operation, _column_name)
72
- if [:count, :sum, :size].include? operation
44
+ case operation
45
+ when :count, :sum
73
46
  group_values.any? ? Hash.new : 0
74
- elsif [:average, :minimum, :maximum].include?(operation) && group_values.any?
75
- Hash.new
76
- else
77
- nil
47
+ when :average, :minimum, :maximum
48
+ group_values.any? ? Hash.new : nil
78
49
  end
79
50
  end
80
51
 
@@ -85,5 +56,11 @@ module ActiveRecord
85
56
  def or(other)
86
57
  other.spawn
87
58
  end
59
+
60
+ private
61
+
62
+ def exec_queries
63
+ @records = [].freeze
64
+ end
88
65
  end
89
66
  end
@@ -100,10 +100,6 @@ module ActiveRecord
100
100
  !(@new_record || @destroyed)
101
101
  end
102
102
 
103
- ##
104
- # :call-seq:
105
- # save(*args)
106
- #
107
103
  # Saves the model.
108
104
  #
109
105
  # If the model is new, a record gets created in the database, otherwise
@@ -111,7 +107,7 @@ module ActiveRecord
111
107
  #
112
108
  # By default, save always runs validations. If any of them fail the action
113
109
  # is cancelled and #save returns +false+, and the record won't be saved. However, if you supply
114
- # validate: false, validations are bypassed altogether. See
110
+ # <tt>validate: false</tt>, validations are bypassed altogether. See
115
111
  # ActiveRecord::Validations for more information.
116
112
  #
117
113
  # By default, #save also sets the +updated_at+/+updated_on+ attributes to
@@ -125,16 +121,12 @@ module ActiveRecord
125
121
  #
126
122
  # Attributes marked as readonly are silently ignored if the record is
127
123
  # being updated.
128
- def save(*args, &block)
129
- create_or_update(*args, &block)
124
+ def save(*args)
125
+ create_or_update(*args)
130
126
  rescue ActiveRecord::RecordInvalid
131
127
  false
132
128
  end
133
129
 
134
- ##
135
- # :call-seq:
136
- # save!(*args)
137
- #
138
130
  # Saves the model.
139
131
  #
140
132
  # If the model is new, a record gets created in the database, otherwise
@@ -142,7 +134,7 @@ module ActiveRecord
142
134
  #
143
135
  # By default, #save! always runs validations. If any of them fail
144
136
  # ActiveRecord::RecordInvalid gets raised, and the record won't be saved. However, if you supply
145
- # validate: false, validations are bypassed altogether. See
137
+ # <tt>validate: false</tt>, validations are bypassed altogether. See
146
138
  # ActiveRecord::Validations for more information.
147
139
  #
148
140
  # By default, #save! also sets the +updated_at+/+updated_on+ attributes to
@@ -156,8 +148,10 @@ module ActiveRecord
156
148
  #
157
149
  # Attributes marked as readonly are silently ignored if the record is
158
150
  # being updated.
159
- def save!(*args, &block)
160
- create_or_update(*args, &block) || raise(RecordNotSaved.new("Failed to save the record", self))
151
+ #
152
+ # Unless an error is raised, returns true.
153
+ def save!(*args)
154
+ create_or_update(*args) || raise(RecordNotSaved.new("Failed to save the record", self))
161
155
  end
162
156
 
163
157
  # Deletes the record in the database and freezes this instance to
@@ -186,10 +180,14 @@ module ActiveRecord
186
180
  # and #destroy returns +false+.
187
181
  # See ActiveRecord::Callbacks for further details.
188
182
  def destroy
189
- raise ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
183
+ _raise_readonly_record_error if readonly?
190
184
  destroy_associations
191
185
  self.class.connection.add_transaction_record(self)
192
- destroy_row if persisted?
186
+ @_trigger_destroy_callback = if persisted?
187
+ destroy_row > 0
188
+ else
189
+ true
190
+ end
193
191
  @destroyed = true
194
192
  freeze
195
193
  end
@@ -261,7 +259,11 @@ module ActiveRecord
261
259
  verify_readonly_attribute(name)
262
260
  public_send("#{name}=", value)
263
261
 
264
- changed? ? save(validate: false) : true
262
+ if has_changes_to_save?
263
+ save(validate: false)
264
+ else
265
+ true
266
+ end
265
267
  end
266
268
 
267
269
  # Updates the attributes of the model from the passed-in hash and saves the
@@ -341,11 +343,13 @@ module ActiveRecord
341
343
  # Wrapper around #increment that writes the update to the database.
342
344
  # Only +attribute+ is updated; the record itself is not saved.
343
345
  # This means that any other modified attributes will still be dirty.
344
- # Validations and callbacks are skipped. Returns +self+.
345
- def increment!(attribute, by = 1)
346
+ # Validations and callbacks are skipped. Supports the `touch` option from
347
+ # +update_counters+, see that for more.
348
+ # Returns +self+.
349
+ def increment!(attribute, by = 1, touch: nil)
346
350
  increment(attribute, by)
347
- change = public_send(attribute) - (attribute_was(attribute.to_s) || 0)
348
- self.class.update_counters(id, attribute => change)
351
+ change = public_send(attribute) - (attribute_in_database(attribute.to_s) || 0)
352
+ self.class.update_counters(id, attribute => change, touch: touch)
349
353
  clear_attribute_change(attribute) # eww
350
354
  self
351
355
  end
@@ -360,9 +364,11 @@ module ActiveRecord
360
364
  # Wrapper around #decrement that writes the update to the database.
361
365
  # Only +attribute+ is updated; the record itself is not saved.
362
366
  # This means that any other modified attributes will still be dirty.
363
- # Validations and callbacks are skipped. Returns +self+.
364
- def decrement!(attribute, by = 1)
365
- increment!(attribute, -by)
367
+ # Validations and callbacks are skipped. Supports the `touch` option from
368
+ # +update_counters+, see that for more.
369
+ # Returns +self+.
370
+ def decrement!(attribute, by = 1, touch: nil)
371
+ increment!(attribute, -by, touch: touch)
366
372
  end
367
373
 
368
374
  # Assigns to +attribute+ the boolean opposite of <tt>attribute?</tt>. So
@@ -392,8 +398,8 @@ module ActiveRecord
392
398
 
393
399
  # Reloads the record from the database.
394
400
  #
395
- # This method finds record by its primary key (which could be assigned manually) and
396
- # modifies the receiver in-place:
401
+ # This method finds the record by its primary key (which could be assigned
402
+ # manually) and modifies the receiver in-place:
397
403
  #
398
404
  # account = Account.new
399
405
  # # => #<Account id: nil, email: nil>
@@ -448,7 +454,7 @@ module ActiveRecord
448
454
  self.class.unscoped { self.class.find(id) }
449
455
  end
450
456
 
451
- @attributes = fresh_object.instance_variable_get('@attributes')
457
+ @attributes = fresh_object.instance_variable_get("@attributes")
452
458
  @new_record = false
453
459
  self
454
460
  end
@@ -507,7 +513,6 @@ module ActiveRecord
507
513
  changes[column] = write_attribute(column, time)
508
514
  end
509
515
 
510
- clear_attribute_changes(changes.keys)
511
516
  primary_key = self.class.primary_key
512
517
  scope = self.class.unscoped.where(primary_key => _read_attribute(primary_key))
513
518
 
@@ -517,12 +522,14 @@ module ActiveRecord
517
522
  changes[locking_column] = increment_lock
518
523
  end
519
524
 
525
+ clear_attribute_changes(changes.keys)
520
526
  result = scope.update_all(changes) == 1
521
527
 
522
528
  if !result && locking_enabled?
523
529
  raise ActiveRecord::StaleObjectError.new(self, "touch")
524
530
  end
525
531
 
532
+ @_trigger_update_callback = result
526
533
  result
527
534
  else
528
535
  true
@@ -543,9 +550,9 @@ module ActiveRecord
543
550
  self.class.unscoped.where(self.class.primary_key => id)
544
551
  end
545
552
 
546
- def create_or_update(*args, &block)
547
- raise ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
548
- result = new_record? ? _create_record(&block) : _update_record(*args, &block)
553
+ def create_or_update(*args)
554
+ _raise_readonly_record_error if readonly?
555
+ result = new_record? ? _create_record : _update_record(*args)
549
556
  result != false
550
557
  end
551
558
 
@@ -555,12 +562,11 @@ module ActiveRecord
555
562
  attributes_values = arel_attributes_with_values_for_update(attribute_names)
556
563
  if attributes_values.empty?
557
564
  rows_affected = 0
565
+ @_trigger_update_callback = true
558
566
  else
559
- rows_affected = self.class.unscoped._update_record attributes_values, id, id_was
567
+ rows_affected = self.class.unscoped._update_record attributes_values, id, id_in_database
568
+ @_trigger_update_callback = rows_affected > 0
560
569
  end
561
-
562
- yield(self) if block_given?
563
-
564
570
  rows_affected
565
571
  end
566
572
 
@@ -573,9 +579,6 @@ module ActiveRecord
573
579
  self.id ||= new_id if self.class.primary_key
574
580
 
575
581
  @new_record = false
576
-
577
- yield(self) if block_given?
578
-
579
582
  id
580
583
  end
581
584
 
@@ -593,5 +596,9 @@ module ActiveRecord
593
596
  def belongs_to_touch_method
594
597
  :touch
595
598
  end
599
+
600
+ def _raise_readonly_record_error
601
+ raise ReadOnlyRecord, "#{self.class} is marked as readonly"
602
+ end
596
603
  end
597
604
  end
@@ -5,7 +5,7 @@ module ActiveRecord
5
5
  # Enable the query cache within the block if Active Record is configured.
6
6
  # If it's not, it will execute the given block.
7
7
  def cache(&block)
8
- if connected? || !configurations.empty?
8
+ if connected?
9
9
  connection.cache(&block)
10
10
  else
11
11
  yield
@@ -15,7 +15,7 @@ module ActiveRecord
15
15
  # Disable the query cache within the block if Active Record is configured.
16
16
  # If it's not, it will execute the given block.
17
17
  def uncached(&block)
18
- if connected? || !configurations.empty?
18
+ if connected?
19
19
  connection.uncached(&block)
20
20
  else
21
21
  yield
@@ -24,19 +24,15 @@ module ActiveRecord
24
24
  end
25
25
 
26
26
  def self.run
27
- connection_id = ActiveRecord::Base.connection_id
28
-
29
27
  caching_pool = ActiveRecord::Base.connection_pool
30
28
  caching_was_enabled = caching_pool.query_cache_enabled
31
29
 
32
30
  caching_pool.enable_query_cache!
33
31
 
34
- [caching_pool, caching_was_enabled, connection_id]
32
+ [caching_pool, caching_was_enabled]
35
33
  end
36
34
 
37
- def self.complete((caching_pool, caching_was_enabled, connection_id))
38
- ActiveRecord::Base.connection_id = connection_id
39
-
35
+ def self.complete((caching_pool, caching_was_enabled))
40
36
  caching_pool.disable_query_cache! unless caching_was_enabled
41
37
 
42
38
  ActiveRecord::Base.connection_handler.connection_pool_list.each do |pool|
@@ -9,7 +9,7 @@ module ActiveRecord
9
9
  delegate :find_each, :find_in_batches, :in_batches, to: :all
10
10
  delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins, :left_joins, :left_outer_joins, :or,
11
11
  :where, :rewhere, :preload, :eager_load, :includes, :from, :lock, :readonly,
12
- :having, :create_with, :uniq, :distinct, :references, :none, :unscope, to: :all
12
+ :having, :create_with, :uniq, :distinct, :references, :none, :unscope, :merge, to: :all
13
13
  delegate :count, :average, :minimum, :maximum, :sum, :calculate, to: :all
14
14
  delegate :pluck, :ids, to: :all
15
15
 
@@ -62,8 +62,7 @@ module ActiveRecord
62
62
  #
63
63
  # * +sql+ - An SQL statement which should return a count query from the database, see the example above.
64
64
  def count_by_sql(sql)
65
- sql = sanitize_conditions(sql)
66
- connection.select_value(sql, "#{name} Count").to_i
65
+ connection.select_value(sanitize_sql(sql), "#{name} Count").to_i
67
66
  end
68
67
  end
69
68
  end
@@ -3,7 +3,7 @@ require "rails"
3
3
  require "active_model/railtie"
4
4
 
5
5
  # For now, action_controller must always be present with
6
- # rails, so let's make sure that it gets required before
6
+ # Rails, so let's make sure that it gets required before
7
7
  # here. This is needed for correctly setting up the middleware.
8
8
  # In the future, this might become an optional require.
9
9
  require "action_controller/railtie"
@@ -13,17 +13,16 @@ module ActiveRecord
13
13
  class Railtie < Rails::Railtie # :nodoc:
14
14
  config.active_record = ActiveSupport::OrderedOptions.new
15
15
 
16
- config.app_generators.orm :active_record, :migration => true,
17
- :timestamps => true
16
+ config.app_generators.orm :active_record, migration: true,
17
+ timestamps: true
18
18
 
19
19
  config.action_dispatch.rescue_responses.merge!(
20
- 'ActiveRecord::RecordNotFound' => :not_found,
21
- 'ActiveRecord::StaleObjectError' => :conflict,
22
- 'ActiveRecord::RecordInvalid' => :unprocessable_entity,
23
- 'ActiveRecord::RecordNotSaved' => :unprocessable_entity
20
+ "ActiveRecord::RecordNotFound" => :not_found,
21
+ "ActiveRecord::StaleObjectError" => :conflict,
22
+ "ActiveRecord::RecordInvalid" => :unprocessable_entity,
23
+ "ActiveRecord::RecordNotSaved" => :unprocessable_entity
24
24
  )
25
25
 
26
-
27
26
  config.active_record.use_schema_cache_dump = true
28
27
  config.active_record.maintain_test_schema = true
29
28
 
@@ -35,8 +34,8 @@ module ActiveRecord
35
34
  ActiveRecord::Tasks::DatabaseTasks.database_configuration = Rails.application.config.database_configuration
36
35
 
37
36
  if defined?(ENGINE_ROOT) && engine = Rails::Engine.find(ENGINE_ROOT)
38
- if engine.paths['db/migrate'].existent
39
- ActiveRecord::Tasks::DatabaseTasks.migrations_paths += engine.paths['db/migrate'].to_a
37
+ if engine.paths["db/migrate"].existent
38
+ ActiveRecord::Tasks::DatabaseTasks.migrations_paths += engine.paths["db/migrate"].to_a
40
39
  end
41
40
  end
42
41
  end
@@ -83,15 +82,15 @@ module ActiveRecord
83
82
  if config.active_record.delete(:use_schema_cache_dump)
84
83
  config.after_initialize do |app|
85
84
  ActiveSupport.on_load(:active_record) do
86
- filename = File.join(app.config.paths["db"].first, "schema_cache.dump")
85
+ filename = File.join(app.config.paths["db"].first, "schema_cache.yml")
87
86
 
88
87
  if File.file?(filename)
89
- cache = Marshal.load File.binread filename
88
+ cache = YAML.load(File.read(filename))
90
89
  if cache.version == ActiveRecord::Migrator.current_version
91
- self.connection.schema_cache = cache
92
- self.connection_pool.schema_cache = cache.dup
90
+ connection.schema_cache = cache
91
+ connection_pool.schema_cache = cache.dup
93
92
  else
94
- warn "Ignoring db/schema_cache.dump because it has expired. The current schema version is #{ActiveRecord::Migrator.current_version}, but the one in the cache is #{cache.version}."
93
+ warn "Ignoring db/schema_cache.yml because it has expired. The current schema version is #{ActiveRecord::Migrator.current_version}, but the one in the cache is #{cache.version}."
95
94
  end
96
95
  end
97
96
  end
@@ -102,14 +101,14 @@ module ActiveRecord
102
101
  initializer "active_record.warn_on_records_fetched_greater_than" do
103
102
  if config.active_record.warn_on_records_fetched_greater_than
104
103
  ActiveSupport.on_load(:active_record) do
105
- require 'active_record/relation/record_fetch_warning'
104
+ require "active_record/relation/record_fetch_warning"
106
105
  end
107
106
  end
108
107
  end
109
108
 
110
109
  initializer "active_record.set_configs" do |app|
111
110
  ActiveSupport.on_load(:active_record) do
112
- app.config.active_record.each do |k,v|
111
+ app.config.active_record.each do |k, v|
113
112
  send "#{k}=", v
114
113
  end
115
114
  end
@@ -1,15 +1,19 @@
1
- require 'active_support/core_ext/module/attr_internal'
2
- require 'active_record/log_subscriber'
1
+ require "active_support/core_ext/module/attr_internal"
2
+ require "active_record/log_subscriber"
3
3
 
4
4
  module ActiveRecord
5
5
  module Railties # :nodoc:
6
6
  module ControllerRuntime #:nodoc:
7
7
  extend ActiveSupport::Concern
8
8
 
9
+ # TODO Change this to private once we've dropped Ruby 2.2 support.
10
+ # Workaround for Ruby 2.2 "private attribute?" warning.
9
11
  protected
10
12
 
11
13
  attr_internal :db_runtime
12
14
 
15
+ private
16
+
13
17
  def process_action(action, *args)
14
18
  # We also need to reset the runtime before each action
15
19
  # because of queries in middleware or in cases we are streaming
@@ -1,4 +1,4 @@
1
- require 'active_record'
1
+ require "active_record"
2
2
 
3
3
  db_namespace = namespace :db do
4
4
  desc "Set the environment value for the database"
@@ -7,7 +7,7 @@ db_namespace = namespace :db do
7
7
  ActiveRecord::InternalMetadata[:environment] = ActiveRecord::Migrator.current_environment
8
8
  end
9
9
 
10
- task :check_protected_environments => [:environment, :load_config] do
10
+ task check_protected_environments: [:environment, :load_config] do
11
11
  ActiveRecord::Tasks::DatabaseTasks.check_protected_environments!
12
12
  end
13
13
 
@@ -17,24 +17,24 @@ db_namespace = namespace :db do
17
17
  end
18
18
 
19
19
  namespace :create do
20
- task :all => :load_config do
20
+ task all: :load_config do
21
21
  ActiveRecord::Tasks::DatabaseTasks.create_all
22
22
  end
23
23
  end
24
24
 
25
- desc 'Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to create all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to creating the development and test databases.'
26
- task :create => [:load_config] do
25
+ desc "Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to create all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to creating the development and test databases."
26
+ task create: [:load_config] do
27
27
  ActiveRecord::Tasks::DatabaseTasks.create_current
28
28
  end
29
29
 
30
30
  namespace :drop do
31
- task :all => [:load_config, :check_protected_environments] do
31
+ task all: [:load_config, :check_protected_environments] do
32
32
  ActiveRecord::Tasks::DatabaseTasks.drop_all
33
33
  end
34
34
  end
35
35
 
36
- desc 'Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to dropping the development and test databases.'
37
- task :drop => [:load_config, :check_protected_environments] do
36
+ desc "Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to dropping the development and test databases."
37
+ task drop: [:load_config, :check_protected_environments] do
38
38
  db_namespace["drop:_unsafe"].invoke
39
39
  end
40
40
 
@@ -43,20 +43,20 @@ db_namespace = namespace :db do
43
43
  end
44
44
 
45
45
  namespace :purge do
46
- task :all => [:load_config, :check_protected_environments] do
46
+ task all: [:load_config, :check_protected_environments] do
47
47
  ActiveRecord::Tasks::DatabaseTasks.purge_all
48
48
  end
49
49
  end
50
50
 
51
51
  # desc "Empty the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:purge:all to purge all databases in the config). Without RAILS_ENV it defaults to purging the development and test databases."
52
- task :purge => [:load_config, :check_protected_environments] do
52
+ task purge: [:load_config, :check_protected_environments] do
53
53
  ActiveRecord::Tasks::DatabaseTasks.purge_current
54
54
  end
55
55
 
56
56
  desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
57
- task :migrate => [:environment, :load_config] do
57
+ task migrate: [:environment, :load_config] do
58
58
  ActiveRecord::Tasks::DatabaseTasks.migrate
59
- db_namespace['_dump'].invoke
59
+ db_namespace["_dump"].invoke
60
60
  end
61
61
 
62
62
  # IMPORTANT: This task won't dump the schema if ActiveRecord::Base.dump_schema_after_migration is set to false
@@ -71,148 +71,160 @@ db_namespace = namespace :db do
71
71
  end
72
72
  # Allow this task to be called as many times as required. An example is the
73
73
  # migrate:redo task, which calls other two internally that depend on this one.
74
- db_namespace['_dump'].reenable
74
+ db_namespace["_dump"].reenable
75
75
  end
76
76
 
77
77
  namespace :migrate do
78
78
  # desc 'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
79
- task :redo => [:environment, :load_config] do
80
- raise "Empty VERSION provided" if ENV["VERSION"] && ENV["VERSION"].empty?
81
-
82
- if ENV['VERSION']
83
- db_namespace['migrate:down'].invoke
84
- db_namespace['migrate:up'].invoke
79
+ task redo: [:environment, :load_config] do
80
+ if ENV["VERSION"]
81
+ db_namespace["migrate:down"].invoke
82
+ db_namespace["migrate:up"].invoke
85
83
  else
86
- db_namespace['rollback'].invoke
87
- db_namespace['migrate'].invoke
84
+ db_namespace["rollback"].invoke
85
+ db_namespace["migrate"].invoke
88
86
  end
89
87
  end
90
88
 
91
89
  # desc 'Resets your database using your migrations for the current environment'
92
- task :reset => ['db:drop', 'db:create', 'db:migrate']
90
+ task reset: ["db:drop", "db:create", "db:migrate"]
93
91
 
94
92
  # desc 'Runs the "up" for a given migration VERSION.'
95
- task :up => [:environment, :load_config] do
96
- raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
97
-
98
- version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
93
+ task up: [:environment, :load_config] do
94
+ version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
95
+ raise "VERSION is required" unless version
99
96
  ActiveRecord::Migrator.run(:up, ActiveRecord::Tasks::DatabaseTasks.migrations_paths, version)
100
- db_namespace['_dump'].invoke
97
+ db_namespace["_dump"].invoke
101
98
  end
102
99
 
103
100
  # desc 'Runs the "down" for a given migration VERSION.'
104
- task :down => [:environment, :load_config] do
105
- raise "VERSION is required - To go down one migration, use db:rollback" if !ENV["VERSION"] || ENV["VERSION"].empty?
106
- version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
101
+ task down: [:environment, :load_config] do
102
+ version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
103
+ raise "VERSION is required - To go down one migration, run db:rollback" unless version
107
104
  ActiveRecord::Migrator.run(:down, ActiveRecord::Tasks::DatabaseTasks.migrations_paths, version)
108
- db_namespace['_dump'].invoke
105
+ db_namespace["_dump"].invoke
109
106
  end
110
107
 
111
- desc 'Display status of migrations'
112
- task :status => [:environment, :load_config] do
108
+ desc "Display status of migrations"
109
+ task status: [:environment, :load_config] do
113
110
  unless ActiveRecord::SchemaMigration.table_exists?
114
- abort 'Schema migrations table does not exist yet.'
111
+ abort "Schema migrations table does not exist yet."
115
112
  end
113
+ db_list = ActiveRecord::SchemaMigration.normalized_versions
114
+
115
+ file_list =
116
+ ActiveRecord::Tasks::DatabaseTasks.migrations_paths.flat_map do |path|
117
+ Dir.foreach(path).map do |file|
118
+ next unless ActiveRecord::Migrator.match_to_migration_filename?(file)
119
+
120
+ version, name, scope = ActiveRecord::Migrator.parse_migration_filename(file)
121
+ version = ActiveRecord::SchemaMigration.normalize_migration_number(version)
122
+ status = db_list.delete(version) ? "up" : "down"
123
+ [status, version, (name + scope).humanize]
124
+ end.compact
125
+ end
116
126
 
127
+ db_list.map! do |version|
128
+ ["up", version, "********** NO FILE **********"]
129
+ end
117
130
  # output
118
131
  puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
119
132
  puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
120
133
  puts "-" * 50
121
- paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
122
- ActiveRecord::Migrator.migrations_status(paths).each do |status, version, name|
134
+ (db_list + file_list).sort_by { |_, version, _| version }.each do |status, version, name|
123
135
  puts "#{status.center(8)} #{version.ljust(14)} #{name}"
124
136
  end
125
137
  puts
126
138
  end
127
139
  end
128
140
 
129
- desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'
130
- task :rollback => [:environment, :load_config] do
131
- step = ENV['STEP'] ? ENV['STEP'].to_i : 1
141
+ desc "Rolls the schema back to the previous version (specify steps w/ STEP=n)."
142
+ task rollback: [:environment, :load_config] do
143
+ step = ENV["STEP"] ? ENV["STEP"].to_i : 1
132
144
  ActiveRecord::Migrator.rollback(ActiveRecord::Tasks::DatabaseTasks.migrations_paths, step)
133
- db_namespace['_dump'].invoke
145
+ db_namespace["_dump"].invoke
134
146
  end
135
147
 
136
148
  # desc 'Pushes the schema to the next version (specify steps w/ STEP=n).'
137
- task :forward => [:environment, :load_config] do
138
- step = ENV['STEP'] ? ENV['STEP'].to_i : 1
149
+ task forward: [:environment, :load_config] do
150
+ step = ENV["STEP"] ? ENV["STEP"].to_i : 1
139
151
  ActiveRecord::Migrator.forward(ActiveRecord::Tasks::DatabaseTasks.migrations_paths, step)
140
- db_namespace['_dump'].invoke
152
+ db_namespace["_dump"].invoke
141
153
  end
142
154
 
143
155
  # desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
144
- task :reset => [ 'db:drop', 'db:setup' ]
156
+ task reset: [ "db:drop", "db:setup" ]
145
157
 
146
158
  # desc "Retrieves the charset for the current environment's database"
147
- task :charset => [:environment, :load_config] do
159
+ task charset: [:environment, :load_config] do
148
160
  puts ActiveRecord::Tasks::DatabaseTasks.charset_current
149
161
  end
150
162
 
151
163
  # desc "Retrieves the collation for the current environment's database"
152
- task :collation => [:environment, :load_config] do
164
+ task collation: [:environment, :load_config] do
153
165
  begin
154
166
  puts ActiveRecord::Tasks::DatabaseTasks.collation_current
155
167
  rescue NoMethodError
156
- $stderr.puts 'Sorry, your database adapter is not supported yet. Feel free to submit a patch.'
168
+ $stderr.puts "Sorry, your database adapter is not supported yet. Feel free to submit a patch."
157
169
  end
158
170
  end
159
171
 
160
- desc 'Retrieves the current schema version number'
161
- task :version => [:environment, :load_config] do
172
+ desc "Retrieves the current schema version number"
173
+ task version: [:environment, :load_config] do
162
174
  puts "Current version: #{ActiveRecord::Migrator.current_version}"
163
175
  end
164
176
 
165
177
  # desc "Raises an error if there are pending migrations"
166
- task :abort_if_pending_migrations => [:environment, :load_config] do
178
+ task abort_if_pending_migrations: [:environment, :load_config] do
167
179
  pending_migrations = ActiveRecord::Migrator.open(ActiveRecord::Tasks::DatabaseTasks.migrations_paths).pending_migrations
168
180
 
169
181
  if pending_migrations.any?
170
182
  puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
171
183
  pending_migrations.each do |pending_migration|
172
- puts ' %4d %s' % [pending_migration.version, pending_migration.name]
184
+ puts " %4d %s" % [pending_migration.version, pending_migration.name]
173
185
  end
174
186
  abort %{Run `rails db:migrate` to update your database then try again.}
175
187
  end
176
188
  end
177
189
 
178
- desc 'Creates the database, loads the schema, and initializes with the seed data (use db:reset to also drop the database first)'
179
- task :setup => ['db:schema:load_if_ruby', 'db:structure:load_if_sql', :seed]
190
+ desc "Creates the database, loads the schema, and initializes with the seed data (use db:reset to also drop the database first)"
191
+ task setup: ["db:schema:load_if_ruby", "db:structure:load_if_sql", :seed]
180
192
 
181
- desc 'Loads the seed data from db/seeds.rb'
193
+ desc "Loads the seed data from db/seeds.rb"
182
194
  task :seed do
183
- db_namespace['abort_if_pending_migrations'].invoke
195
+ db_namespace["abort_if_pending_migrations"].invoke
184
196
  ActiveRecord::Tasks::DatabaseTasks.load_seed
185
197
  end
186
198
 
187
199
  namespace :fixtures do
188
200
  desc "Loads fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y. Load from subdirectory in test/fixtures using FIXTURES_DIR=z. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
189
- task :load => [:environment, :load_config] do
190
- require 'active_record/fixtures'
201
+ task load: [:environment, :load_config] do
202
+ require "active_record/fixtures"
191
203
 
192
204
  base_dir = ActiveRecord::Tasks::DatabaseTasks.fixtures_path
193
205
 
194
- fixtures_dir = if ENV['FIXTURES_DIR']
195
- File.join base_dir, ENV['FIXTURES_DIR']
196
- else
197
- base_dir
198
- end
206
+ fixtures_dir = if ENV["FIXTURES_DIR"]
207
+ File.join base_dir, ENV["FIXTURES_DIR"]
208
+ else
209
+ base_dir
210
+ end
199
211
 
200
- fixture_files = if ENV['FIXTURES']
201
- ENV['FIXTURES'].split(',')
202
- else
203
- # The use of String#[] here is to support namespaced fixtures
204
- Dir["#{fixtures_dir}/**/*.yml"].map {|f| f[(fixtures_dir.size + 1)..-5] }
205
- end
212
+ fixture_files = if ENV["FIXTURES"]
213
+ ENV["FIXTURES"].split(",")
214
+ else
215
+ # The use of String#[] here is to support namespaced fixtures.
216
+ Dir["#{fixtures_dir}/**/*.yml"].map { |f| f[(fixtures_dir.size + 1)..-5] }
217
+ end
206
218
 
207
219
  ActiveRecord::FixtureSet.create_fixtures(fixtures_dir, fixture_files)
208
220
  end
209
221
 
210
222
  # desc "Search for a fixture given a LABEL or ID. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
211
- task :identify => [:environment, :load_config] do
212
- require 'active_record/fixtures'
223
+ task identify: [:environment, :load_config] do
224
+ require "active_record/fixtures"
213
225
 
214
- label, id = ENV['LABEL'], ENV['ID']
215
- raise 'LABEL or ID required' if label.blank? && id.blank?
226
+ label, id = ENV["LABEL"], ENV["ID"]
227
+ raise "LABEL or ID required" if label.blank? && id.blank?
216
228
 
217
229
  puts %Q(The fixture ID for "#{label}" is #{ActiveRecord::FixtureSet.identify(label)}.) if label
218
230
 
@@ -233,39 +245,36 @@ db_namespace = namespace :db do
233
245
  end
234
246
 
235
247
  namespace :schema do
236
- desc 'Creates a db/schema.rb file that is portable against any DB supported by Active Record'
237
- task :dump => [:environment, :load_config] do
238
- require 'active_record/schema_dumper'
239
- filename = ENV['SCHEMA'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'schema.rb')
248
+ desc "Creates a db/schema.rb file that is portable against any DB supported by Active Record"
249
+ task dump: [:environment, :load_config] do
250
+ require "active_record/schema_dumper"
251
+ filename = ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema.rb")
240
252
  File.open(filename, "w:utf-8") do |file|
241
253
  ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
242
254
  end
243
- db_namespace['schema:dump'].reenable
255
+ db_namespace["schema:dump"].reenable
244
256
  end
245
257
 
246
- desc 'Loads a schema.rb file into the database'
247
- task :load => [:environment, :load_config, :check_protected_environments] do
248
- ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV['SCHEMA'])
258
+ desc "Loads a schema.rb file into the database"
259
+ task load: [:environment, :load_config, :check_protected_environments] do
260
+ ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV["SCHEMA"])
249
261
  end
250
262
 
251
- task :load_if_ruby => ['db:create', :environment] do
263
+ task load_if_ruby: ["db:create", :environment] do
252
264
  db_namespace["schema:load"].invoke if ActiveRecord::Base.schema_format == :ruby
253
265
  end
254
266
 
255
267
  namespace :cache do
256
- desc 'Creates a db/schema_cache.dump file.'
257
- task :dump => [:environment, :load_config] do
258
- con = ActiveRecord::Base.connection
259
- filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.dump")
260
-
261
- con.schema_cache.clear!
262
- con.data_sources.each { |table| con.schema_cache.add(table) }
263
- open(filename, 'wb') { |f| f.write(Marshal.dump(con.schema_cache)) }
268
+ desc "Creates a db/schema_cache.yml file."
269
+ task dump: [:environment, :load_config] do
270
+ conn = ActiveRecord::Base.connection
271
+ filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.yml")
272
+ ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(conn, filename)
264
273
  end
265
274
 
266
- desc 'Clears a db/schema_cache.dump file.'
267
- task :clear => [:environment, :load_config] do
268
- filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.dump")
275
+ desc "Clears a db/schema_cache.yml file."
276
+ task clear: [:environment, :load_config] do
277
+ filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.yml")
269
278
  rm_f filename, verbose: false
270
279
  end
271
280
  end
@@ -273,9 +282,9 @@ db_namespace = namespace :db do
273
282
  end
274
283
 
275
284
  namespace :structure do
276
- desc 'Dumps the database structure to db/structure.sql. Specify another file with SCHEMA=db/my_structure.sql'
277
- task :dump => [:environment, :load_config] do
278
- filename = ENV['SCHEMA'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "structure.sql")
285
+ desc "Dumps the database structure to db/structure.sql. Specify another file with SCHEMA=db/my_structure.sql"
286
+ task dump: [:environment, :load_config] do
287
+ filename = ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "structure.sql")
279
288
  current_config = ActiveRecord::Tasks::DatabaseTasks.current_config
280
289
  ActiveRecord::Tasks::DatabaseTasks.structure_dump(current_config, filename)
281
290
 
@@ -286,44 +295,36 @@ db_namespace = namespace :db do
286
295
  f.print "\n"
287
296
  end
288
297
  end
289
- db_namespace['structure:dump'].reenable
298
+ db_namespace["structure:dump"].reenable
290
299
  end
291
300
 
292
301
  desc "Recreates the databases from the structure.sql file"
293
- task :load => [:environment, :load_config, :check_protected_environments] do
294
- ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:sql, ENV['SCHEMA'])
302
+ task load: [:environment, :load_config, :check_protected_environments] do
303
+ ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:sql, ENV["SCHEMA"])
295
304
  end
296
305
 
297
- task :load_if_sql => ['db:create', :environment] do
306
+ task load_if_sql: ["db:create", :environment] do
298
307
  db_namespace["structure:load"].invoke if ActiveRecord::Base.schema_format == :sql
299
308
  end
300
309
  end
301
310
 
302
311
  namespace :test do
303
-
304
- task :deprecated do
305
- Rake.application.top_level_tasks.grep(/^db:test:/).each do |task|
306
- $stderr.puts "WARNING: #{task} is deprecated. The Rails test helper now maintains " \
307
- "your test schema automatically, see the release notes for details."
308
- end
309
- end
310
-
311
312
  # desc "Recreate the test database from the current schema"
312
- task :load => %w(db:test:purge) do
313
+ task load: %w(db:test:purge) do
313
314
  case ActiveRecord::Base.schema_format
314
- when :ruby
315
- db_namespace["test:load_schema"].invoke
316
- when :sql
317
- db_namespace["test:load_structure"].invoke
315
+ when :ruby
316
+ db_namespace["test:load_schema"].invoke
317
+ when :sql
318
+ db_namespace["test:load_structure"].invoke
318
319
  end
319
320
  end
320
321
 
321
322
  # desc "Recreate the test database from an existent schema.rb file"
322
- task :load_schema => %w(db:test:purge) do
323
+ task load_schema: %w(db:test:purge) do
323
324
  begin
324
325
  should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
325
326
  ActiveRecord::Schema.verbose = false
326
- ActiveRecord::Tasks::DatabaseTasks.load_schema ActiveRecord::Base.configurations["test"], :ruby, ENV["SCHEMA"], "test"
327
+ ActiveRecord::Tasks::DatabaseTasks.load_schema ActiveRecord::Base.configurations["test"], :ruby, ENV["SCHEMA"]
327
328
  ensure
328
329
  if should_reconnect
329
330
  ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ActiveRecord::Tasks::DatabaseTasks.env])
@@ -332,35 +333,19 @@ db_namespace = namespace :db do
332
333
  end
333
334
 
334
335
  # desc "Recreate the test database from an existent structure.sql file"
335
- task :load_structure => %w(db:test:purge) do
336
- ActiveRecord::Tasks::DatabaseTasks.load_schema ActiveRecord::Base.configurations["test"], :sql, ENV["SCHEMA"], "test"
336
+ task load_structure: %w(db:test:purge) do
337
+ ActiveRecord::Tasks::DatabaseTasks.load_schema ActiveRecord::Base.configurations["test"], :sql, ENV["SCHEMA"]
337
338
  end
338
339
 
339
- # desc "Recreate the test database from a fresh schema"
340
- task :clone => %w(db:test:deprecated environment) do
341
- case ActiveRecord::Base.schema_format
342
- when :ruby
343
- db_namespace["test:clone_schema"].invoke
344
- when :sql
345
- db_namespace["test:clone_structure"].invoke
346
- end
347
- end
348
-
349
- # desc "Recreate the test database from a fresh schema.rb file"
350
- task :clone_schema => %w(db:test:deprecated db:schema:dump db:test:load_schema)
351
-
352
- # desc "Recreate the test database from a fresh structure.sql file"
353
- task :clone_structure => %w(db:test:deprecated db:structure:dump db:test:load_structure)
354
-
355
340
  # desc "Empty the test database"
356
- task :purge => %w(environment load_config check_protected_environments) do
357
- ActiveRecord::Tasks::DatabaseTasks.purge ActiveRecord::Base.configurations['test']
341
+ task purge: %w(environment load_config check_protected_environments) do
342
+ ActiveRecord::Tasks::DatabaseTasks.purge ActiveRecord::Base.configurations["test"]
358
343
  end
359
344
 
360
345
  # desc 'Load the test schema'
361
- task :prepare => %w(environment load_config) do
346
+ task prepare: %w(environment load_config) do
362
347
  unless ActiveRecord::Base.configurations.blank?
363
- db_namespace['test:load'].invoke
348
+ db_namespace["test:load"].invoke
364
349
  end
365
350
  end
366
351
  end
@@ -369,13 +354,13 @@ end
369
354
  namespace :railties do
370
355
  namespace :install do
371
356
  # desc "Copies missing migrations from Railties (e.g. engines). You can specify Railties to use with FROM=railtie1,railtie2"
372
- task :migrations => :'db:load_config' do
373
- to_load = ENV['FROM'].blank? ? :all : ENV['FROM'].split(",").map(&:strip)
357
+ task migrations: :'db:load_config' do
358
+ to_load = ENV["FROM"].blank? ? :all : ENV["FROM"].split(",").map(&:strip)
374
359
  railties = {}
375
360
  Rails.application.migration_railties.each do |railtie|
376
361
  next unless to_load == :all || to_load.include?(railtie.railtie_name)
377
362
 
378
- if railtie.respond_to?(:paths) && (path = railtie.paths['db/migrate'].first)
363
+ if railtie.respond_to?(:paths) && (path = railtie.paths["db/migrate"].first)
379
364
  railties[railtie.railtie_name] = path
380
365
  end
381
366
  end
@@ -389,7 +374,7 @@ namespace :railties do
389
374
  end
390
375
 
391
376
  ActiveRecord::Migration.copy(ActiveRecord::Tasks::DatabaseTasks.migrations_paths.first, railties,
392
- :on_skip => on_skip, :on_copy => on_copy)
377
+ on_skip: on_skip, on_copy: on_copy)
393
378
  end
394
379
  end
395
380
  end