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
@@ -92,7 +92,7 @@ module ActiveRecord
92
92
  send(method, args, &block)
93
93
  end
94
94
 
95
- def respond_to?(*args) # :nodoc:
95
+ def respond_to_missing?(*args) # :nodoc:
96
96
  super || delegate.respond_to?(*args)
97
97
  end
98
98
 
@@ -112,127 +112,127 @@ module ActiveRecord
112
112
 
113
113
  private
114
114
 
115
- module StraightReversions
116
- private
117
- { transaction: :transaction,
118
- execute_block: :execute_block,
119
- create_table: :drop_table,
120
- create_join_table: :drop_join_table,
121
- add_column: :remove_column,
122
- add_timestamps: :remove_timestamps,
123
- add_reference: :remove_reference,
124
- enable_extension: :disable_extension
125
- }.each do |cmd, inv|
126
- [[inv, cmd], [cmd, inv]].uniq.each do |method, inverse|
127
- class_eval <<-EOV, __FILE__, __LINE__ + 1
128
- def invert_#{method}(args, &block) # def invert_create_table(args, &block)
129
- [:#{inverse}, args, block] # [:drop_table, args, block]
130
- end # end
131
- EOV
132
- end
115
+ module StraightReversions
116
+ private
117
+ { transaction: :transaction,
118
+ execute_block: :execute_block,
119
+ create_table: :drop_table,
120
+ create_join_table: :drop_join_table,
121
+ add_column: :remove_column,
122
+ add_timestamps: :remove_timestamps,
123
+ add_reference: :remove_reference,
124
+ enable_extension: :disable_extension
125
+ }.each do |cmd, inv|
126
+ [[inv, cmd], [cmd, inv]].uniq.each do |method, inverse|
127
+ class_eval <<-EOV, __FILE__, __LINE__ + 1
128
+ def invert_#{method}(args, &block) # def invert_create_table(args, &block)
129
+ [:#{inverse}, args, block] # [:drop_table, args, block]
130
+ end # end
131
+ EOV
132
+ end
133
+ end
133
134
  end
134
- end
135
135
 
136
- include StraightReversions
136
+ include StraightReversions
137
137
 
138
- def invert_drop_table(args, &block)
139
- if args.size == 1 && block == nil
140
- raise ActiveRecord::IrreversibleMigration, "To avoid mistakes, drop_table is only reversible if given options or a block (can be empty)."
138
+ def invert_drop_table(args, &block)
139
+ if args.size == 1 && block == nil
140
+ raise ActiveRecord::IrreversibleMigration, "To avoid mistakes, drop_table is only reversible if given options or a block (can be empty)."
141
+ end
142
+ super
141
143
  end
142
- super
143
- end
144
144
 
145
- def invert_rename_table(args)
146
- [:rename_table, args.reverse]
147
- end
145
+ def invert_rename_table(args)
146
+ [:rename_table, args.reverse]
147
+ end
148
148
 
149
- def invert_remove_column(args)
150
- raise ActiveRecord::IrreversibleMigration, "remove_column is only reversible if given a type." if args.size <= 2
151
- super
152
- end
149
+ def invert_remove_column(args)
150
+ raise ActiveRecord::IrreversibleMigration, "remove_column is only reversible if given a type." if args.size <= 2
151
+ super
152
+ end
153
153
 
154
- def invert_rename_index(args)
155
- [:rename_index, [args.first] + args.last(2).reverse]
156
- end
154
+ def invert_rename_index(args)
155
+ [:rename_index, [args.first] + args.last(2).reverse]
156
+ end
157
157
 
158
- def invert_rename_column(args)
159
- [:rename_column, [args.first] + args.last(2).reverse]
160
- end
158
+ def invert_rename_column(args)
159
+ [:rename_column, [args.first] + args.last(2).reverse]
160
+ end
161
161
 
162
- def invert_add_index(args)
163
- table, columns, options = *args
164
- options ||= {}
162
+ def invert_add_index(args)
163
+ table, columns, options = *args
164
+ options ||= {}
165
165
 
166
- index_name = options[:name]
167
- options_hash = index_name ? { name: index_name } : { column: columns }
166
+ index_name = options[:name]
167
+ options_hash = index_name ? { name: index_name } : { column: columns }
168
168
 
169
- [:remove_index, [table, options_hash]]
170
- end
169
+ [:remove_index, [table, options_hash]]
170
+ end
171
171
 
172
- def invert_remove_index(args)
173
- table, options_or_column = *args
174
- if (options = options_or_column).is_a?(Hash)
175
- unless options[:column]
176
- raise ActiveRecord::IrreversibleMigration, "remove_index is only reversible if given a :column option."
172
+ def invert_remove_index(args)
173
+ table, options_or_column = *args
174
+ if (options = options_or_column).is_a?(Hash)
175
+ unless options[:column]
176
+ raise ActiveRecord::IrreversibleMigration, "remove_index is only reversible if given a :column option."
177
+ end
178
+ options = options.dup
179
+ [:add_index, [table, options.delete(:column), options]]
180
+ elsif (column = options_or_column).present?
181
+ [:add_index, [table, column]]
177
182
  end
178
- options = options.dup
179
- [:add_index, [table, options.delete(:column), options]]
180
- elsif (column = options_or_column).present?
181
- [:add_index, [table, column]]
182
183
  end
183
- end
184
184
 
185
- alias :invert_add_belongs_to :invert_add_reference
186
- alias :invert_remove_belongs_to :invert_remove_reference
185
+ alias :invert_add_belongs_to :invert_add_reference
186
+ alias :invert_remove_belongs_to :invert_remove_reference
187
+
188
+ def invert_change_column_default(args)
189
+ table, column, options = *args
187
190
 
188
- def invert_change_column_default(args)
189
- table, column, options = *args
191
+ unless options && options.is_a?(Hash) && options.has_key?(:from) && options.has_key?(:to)
192
+ raise ActiveRecord::IrreversibleMigration, "change_column_default is only reversible if given a :from and :to option."
193
+ end
190
194
 
191
- unless options && options.is_a?(Hash) && options.has_key?(:from) && options.has_key?(:to)
192
- raise ActiveRecord::IrreversibleMigration, "change_column_default is only reversible if given a :from and :to option."
195
+ [:change_column_default, [table, column, from: options[:to], to: options[:from]]]
193
196
  end
194
197
 
195
- [:change_column_default, [table, column, from: options[:to], to: options[:from]]]
196
- end
198
+ def invert_change_column_null(args)
199
+ args[2] = !args[2]
200
+ [:change_column_null, args]
201
+ end
197
202
 
198
- def invert_change_column_null(args)
199
- args[2] = !args[2]
200
- [:change_column_null, args]
201
- end
203
+ def invert_add_foreign_key(args)
204
+ from_table, to_table, add_options = args
205
+ add_options ||= {}
202
206
 
203
- def invert_add_foreign_key(args)
204
- from_table, to_table, add_options = args
205
- add_options ||= {}
207
+ if add_options[:name]
208
+ options = { name: add_options[:name] }
209
+ elsif add_options[:column]
210
+ options = { column: add_options[:column] }
211
+ else
212
+ options = to_table
213
+ end
206
214
 
207
- if add_options[:name]
208
- options = { name: add_options[:name] }
209
- elsif add_options[:column]
210
- options = { column: add_options[:column] }
211
- else
212
- options = to_table
215
+ [:remove_foreign_key, [from_table, options]]
213
216
  end
214
217
 
215
- [:remove_foreign_key, [from_table, options]]
216
- end
217
-
218
- def invert_remove_foreign_key(args)
219
- from_table, to_table, remove_options = args
220
- raise ActiveRecord::IrreversibleMigration, "remove_foreign_key is only reversible if given a second table" if to_table.nil? || to_table.is_a?(Hash)
218
+ def invert_remove_foreign_key(args)
219
+ from_table, to_table, remove_options = args
220
+ raise ActiveRecord::IrreversibleMigration, "remove_foreign_key is only reversible if given a second table" if to_table.nil? || to_table.is_a?(Hash)
221
221
 
222
- reversed_args = [from_table, to_table]
223
- reversed_args << remove_options if remove_options
222
+ reversed_args = [from_table, to_table]
223
+ reversed_args << remove_options if remove_options
224
224
 
225
- [:add_foreign_key, reversed_args]
226
- end
225
+ [:add_foreign_key, reversed_args]
226
+ end
227
227
 
228
- # Forwards any missing method call to the \target.
229
- def method_missing(method, *args, &block)
230
- if @delegate.respond_to?(method)
231
- @delegate.send(method, *args, &block)
232
- else
233
- super
228
+ # Forwards any missing method call to the \target.
229
+ def method_missing(method, *args, &block)
230
+ if @delegate.respond_to?(method)
231
+ @delegate.send(method, *args, &block)
232
+ else
233
+ super
234
+ end
234
235
  end
235
- end
236
236
  end
237
237
  end
238
238
  end
@@ -5,15 +5,74 @@ module ActiveRecord
5
5
  version = version.to_s
6
6
  name = "V#{version.tr('.', '_')}"
7
7
  unless const_defined?(name)
8
- versions = constants.grep(/\AV[0-9_]+\z/).map { |s| s.to_s.delete('V').tr('_', '.').inspect }
8
+ versions = constants.grep(/\AV[0-9_]+\z/).map { |s| s.to_s.delete("V").tr("_", ".").inspect }
9
9
  raise ArgumentError, "Unknown migration version #{version.inspect}; expected one of #{versions.sort.join(', ')}"
10
10
  end
11
11
  const_get(name)
12
12
  end
13
13
 
14
- V5_0 = Current
14
+ V5_1 = Current
15
15
 
16
- module FourTwoShared
16
+ class V5_0 < V5_1
17
+ module TableDefinition
18
+ def references(*args, **options)
19
+ super(*args, type: :integer, **options)
20
+ end
21
+ alias :belongs_to :references
22
+ end
23
+
24
+ def create_table(table_name, options = {})
25
+ if adapter_name == "PostgreSQL"
26
+ if options[:id] == :uuid && !options.key?(:default)
27
+ options[:default] = "uuid_generate_v4()"
28
+ end
29
+ end
30
+
31
+ unless adapter_name == "Mysql2" && options[:id] == :bigint
32
+ if [:integer, :bigint].include?(options[:id]) && !options.key?(:default)
33
+ options[:default] = nil
34
+ end
35
+ end
36
+
37
+ # Since 5.1 Postgres adapter uses bigserial type for primary
38
+ # keys by default and MySQL uses bigint. This compat layer makes old migrations utilize
39
+ # serial/int type instead -- the way it used to work before 5.1.
40
+ unless options.key?(:id)
41
+ options[:id] = :integer
42
+ end
43
+
44
+ if block_given?
45
+ super(table_name, options) do |t|
46
+ class << t
47
+ prepend TableDefinition
48
+ end
49
+ yield t
50
+ end
51
+ else
52
+ super
53
+ end
54
+ end
55
+
56
+ def change_table(table_name, options = {})
57
+ if block_given?
58
+ super(table_name, options) do |t|
59
+ class << t
60
+ prepend TableDefinition
61
+ end
62
+ yield t
63
+ end
64
+ else
65
+ super
66
+ end
67
+ end
68
+
69
+ def add_reference(table_name, ref_name, **options)
70
+ super(table_name, ref_name, type: :integer, **options)
71
+ end
72
+ alias :add_belongs_to :add_reference
73
+ end
74
+
75
+ class V4_2 < V5_0
17
76
  module TableDefinition
18
77
  def references(*, **options)
19
78
  options[:index] ||= false
@@ -21,7 +80,7 @@ module ActiveRecord
21
80
  end
22
81
  alias :belongs_to :references
23
82
 
24
- def timestamps(*, **options)
83
+ def timestamps(**options)
25
84
  options[:null] = true if options[:null].nil?
26
85
  super
27
86
  end
@@ -59,7 +118,7 @@ module ActiveRecord
59
118
  end
60
119
  alias :add_belongs_to :add_reference
61
120
 
62
- def add_timestamps(*, **options)
121
+ def add_timestamps(_, **options)
63
122
  options[:null] = true if options[:null].nil?
64
123
  super
65
124
  end
@@ -83,43 +142,23 @@ module ActiveRecord
83
142
 
84
143
  private
85
144
 
86
- def index_name_for_remove(table_name, options = {})
87
- index_name = index_name(table_name, options)
145
+ def index_name_for_remove(table_name, options = {})
146
+ index_name = index_name(table_name, options)
88
147
 
89
- unless index_name_exists?(table_name, index_name, true)
90
- if options.is_a?(Hash) && options.has_key?(:name)
91
- options_without_column = options.dup
92
- options_without_column.delete :column
93
- index_name_without_column = index_name(table_name, options_without_column)
148
+ unless index_name_exists?(table_name, index_name)
149
+ if options.is_a?(Hash) && options.has_key?(:name)
150
+ options_without_column = options.dup
151
+ options_without_column.delete :column
152
+ index_name_without_column = index_name(table_name, options_without_column)
153
+
154
+ return index_name_without_column if index_name_exists?(table_name, index_name_without_column)
155
+ end
94
156
 
95
- return index_name_without_column if index_name_exists?(table_name, index_name_without_column, false)
157
+ raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' does not exist"
96
158
  end
97
159
 
98
- raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' does not exist"
160
+ index_name
99
161
  end
100
-
101
- index_name
102
- end
103
- end
104
-
105
- class V4_2 < V5_0
106
- # 4.2 is defined as a module because it needs to be shared with
107
- # Legacy. When the time comes, V5_0 should be defined straight
108
- # in its class.
109
- include FourTwoShared
110
- end
111
-
112
- module Legacy
113
- include FourTwoShared
114
-
115
- def migrate(*)
116
- ActiveSupport::Deprecation.warn \
117
- "Directly inheriting from ActiveRecord::Migration is deprecated. " \
118
- "Please specify the Rails release the migration was written for:\n" \
119
- "\n" \
120
- " class #{self.class.name} < ActiveRecord::Migration[4.2]"
121
- super
122
- end
123
162
  end
124
163
  end
125
164
  end
@@ -3,13 +3,13 @@ module ActiveRecord
3
3
  module JoinTable #:nodoc:
4
4
  private
5
5
 
6
- def find_join_table_name(table_1, table_2, options = {})
7
- options.delete(:table_name) || join_table_name(table_1, table_2)
8
- end
6
+ def find_join_table_name(table_1, table_2, options = {})
7
+ options.delete(:table_name) || join_table_name(table_1, table_2)
8
+ end
9
9
 
10
- def join_table_name(table_1, table_2)
11
- ModelSchema.derive_join_table_name(table_1, table_2).to_sym
12
- end
10
+ def join_table_name(table_1, table_2)
11
+ ModelSchema.derive_join_table_name(table_1, table_2).to_sym
12
+ end
13
13
  end
14
14
  end
15
15
  end
@@ -1,5 +1,3 @@
1
- require "monitor"
2
-
3
1
  module ActiveRecord
4
2
  module ModelSchema
5
3
  extend ActiveSupport::Concern
@@ -113,6 +111,20 @@ module ActiveRecord
113
111
  # If true, the default table name for a Product class will be "products". If false, it would just be "product".
114
112
  # See table_name for the full rules on table/class naming. This is true, by default.
115
113
 
114
+ ##
115
+ # :singleton-method: ignored_columns
116
+ # :call-seq: ignored_columns
117
+ #
118
+ # The list of columns names the model should ignore. Ignored columns won't have attribute
119
+ # accessors defined, and won't be referenced in SQL queries.
120
+
121
+ ##
122
+ # :singleton-method: ignored_columns=
123
+ # :call-seq: ignored_columns=(columns)
124
+ #
125
+ # Sets the columns names the model should ignore. Ignored columns won't have attribute
126
+ # accessors defined, and won't be referenced in SQL queries.
127
+
116
128
  included do
117
129
  mattr_accessor :primary_key_prefix_type, instance_writer: false
118
130
 
@@ -134,12 +146,12 @@ module ActiveRecord
134
146
  class_attribute :pluralize_table_names, instance_writer: false
135
147
  self.pluralize_table_names = true
136
148
 
137
- self.inheritance_column = 'type'
149
+ class_attribute :ignored_columns, instance_accessor: false
138
150
  self.ignored_columns = [].freeze
139
151
 
140
- delegate :type_for_attribute, to: :class
152
+ self.inheritance_column = "type"
141
153
 
142
- initialize_load_schema_monitor
154
+ delegate :type_for_attribute, to: :class
143
155
  end
144
156
 
145
157
  # Derives the join table name for +first_table+ and +second_table+. The
@@ -240,11 +252,11 @@ module ActiveRecord
240
252
  end
241
253
 
242
254
  def full_table_name_prefix #:nodoc:
243
- (parents.detect{ |p| p.respond_to?(:table_name_prefix) } || self).table_name_prefix
255
+ (parents.detect { |p| p.respond_to?(:table_name_prefix) } || self).table_name_prefix
244
256
  end
245
257
 
246
258
  def full_table_name_suffix #:nodoc:
247
- (parents.detect {|p| p.respond_to?(:table_name_suffix) } || self).table_name_suffix
259
+ (parents.detect { |p| p.respond_to?(:table_name_suffix) } || self).table_name_suffix
248
260
  end
249
261
 
250
262
  # Defines the name of the table column which will store the class name on single-table
@@ -266,22 +278,6 @@ module ActiveRecord
266
278
  @explicit_inheritance_column = true
267
279
  end
268
280
 
269
- # The list of columns names the model should ignore. Ignored columns won't have attribute
270
- # accessors defined, and won't be referenced in SQL queries.
271
- def ignored_columns
272
- if defined?(@ignored_columns)
273
- @ignored_columns
274
- else
275
- superclass.ignored_columns
276
- end
277
- end
278
-
279
- # Sets the columns names the model should ignore. Ignored columns won't have attribute
280
- # accessors defined, and won't be referenced in SQL queries.
281
- def ignored_columns=(columns)
282
- @ignored_columns = columns.map(&:to_s)
283
- end
284
-
285
281
  def sequence_name
286
282
  if base_class == self
287
283
  @sequence_name ||= reset_sequence_name
@@ -296,7 +292,7 @@ module ActiveRecord
296
292
  end
297
293
 
298
294
  # Sets the name of the sequence to use when generating ids to the given
299
- # value, or (if the value is nil or false) to the value returned by the
295
+ # value, or (if the value is +nil+ or +false+) to the value returned by the
300
296
  # given block. This is required for Oracle and is useful for any
301
297
  # database which relies on sequences for primary key generation.
302
298
  #
@@ -332,11 +328,11 @@ module ActiveRecord
332
328
  end
333
329
 
334
330
  def attributes_builder # :nodoc:
335
- unless defined?(@attributes_builder) && @attributes_builder
336
- defaults = _default_attributes.except(*(column_names - [primary_key]))
337
- @attributes_builder = AttributeSet::Builder.new(attribute_types, defaults)
331
+ @attributes_builder ||= AttributeSet::Builder.new(attribute_types, primary_key) do |name|
332
+ unless columns_hash.key?(name)
333
+ _default_attributes[name].dup
334
+ end
338
335
  end
339
- @attributes_builder
340
336
  end
341
337
 
342
338
  def columns_hash # :nodoc:
@@ -351,7 +347,11 @@ module ActiveRecord
351
347
 
352
348
  def attribute_types # :nodoc:
353
349
  load_schema
354
- @attribute_types ||= Hash.new(Type::Value.new)
350
+ @attribute_types ||= Hash.new(Type.default_value)
351
+ end
352
+
353
+ def yaml_encoder # :nodoc:
354
+ @yaml_encoder ||= AttributeSet::YAMLEncoder.new(attribute_types)
355
355
  end
356
356
 
357
357
  # Returns the type of the attribute with the given name, after applying
@@ -392,7 +392,12 @@ module ActiveRecord
392
392
  # Returns an array of column objects where the primary id, all columns ending in "_id" or "_count",
393
393
  # and columns used for single table inheritance have been removed.
394
394
  def content_columns
395
- @content_columns ||= columns.reject { |c| c.name == primary_key || c.name =~ /(_id|_count)$/ || c.name == inheritance_column }
395
+ @content_columns ||= columns.reject do |c|
396
+ c.name == primary_key ||
397
+ c.name == inheritance_column ||
398
+ c.name.end_with?("_id") ||
399
+ c.name.end_with?("_count")
400
+ end
396
401
  end
397
402
 
398
403
  # Resets all the cached information about columns, which will cause them
@@ -430,110 +435,71 @@ module ActiveRecord
430
435
  initialize_find_by_cache
431
436
  end
432
437
 
433
- protected
434
-
435
- def initialize_load_schema_monitor
436
- @load_schema_monitor = Monitor.new
437
- end
438
-
439
438
  private
440
439
 
441
- def inherited(child_class)
442
- super
443
- child_class.initialize_load_schema_monitor
444
- end
445
-
446
- def schema_loaded?
447
- defined?(@schema_loaded) && @schema_loaded
448
- end
449
-
450
- def load_schema
451
- return if schema_loaded?
452
- @load_schema_monitor.synchronize do
453
- return if defined?(@columns_hash) && @columns_hash
454
-
455
- load_schema!
456
-
457
- @schema_loaded = true
440
+ def schema_loaded?
441
+ defined?(@columns_hash) && @columns_hash
458
442
  end
459
- end
460
443
 
461
- def load_schema!
462
- @columns_hash = connection.schema_cache.columns_hash(table_name).except(*ignored_columns)
463
- @columns_hash.each do |name, column|
464
- warn_if_deprecated_type(column)
465
- define_attribute(
466
- name,
467
- connection.lookup_cast_type_from_column(column),
468
- default: column.default,
469
- user_provided_default: false
470
- )
444
+ def load_schema
445
+ unless schema_loaded?
446
+ load_schema!
447
+ end
471
448
  end
472
- end
473
449
 
474
- def reload_schema_from_cache
475
- @arel_engine = nil
476
- @arel_table = nil
477
- @column_names = nil
478
- @attribute_types = nil
479
- @content_columns = nil
480
- @default_attributes = nil
481
- @inheritance_column = nil unless defined?(@explicit_inheritance_column) && @explicit_inheritance_column
482
- @attributes_builder = nil
483
- @columns = nil
484
- @columns_hash = nil
485
- @schema_loaded = false
486
- @attribute_names = nil
487
- direct_descendants.each do |descendant|
488
- descendant.send(:reload_schema_from_cache)
450
+ def load_schema!
451
+ @columns_hash = connection.schema_cache.columns_hash(table_name).except(*ignored_columns)
452
+ @columns_hash.each do |name, column|
453
+ define_attribute(
454
+ name,
455
+ connection.lookup_cast_type_from_column(column),
456
+ default: column.default,
457
+ user_provided_default: false
458
+ )
459
+ end
489
460
  end
490
- end
491
-
492
- # Guesses the table name, but does not decorate it with prefix and suffix information.
493
- def undecorated_table_name(class_name = base_class.name)
494
- table_name = class_name.to_s.demodulize.underscore
495
- pluralize_table_names ? table_name.pluralize : table_name
496
- end
497
461
 
498
- # Computes and returns a table name according to default conventions.
499
- def compute_table_name
500
- base = base_class
501
- if self == base
502
- # Nested classes are prefixed with singular parent table name.
503
- if parent < Base && !parent.abstract_class?
504
- contained = parent.table_name
505
- contained = contained.singularize if parent.pluralize_table_names
506
- contained += '_'
462
+ def reload_schema_from_cache
463
+ @arel_engine = nil
464
+ @arel_table = nil
465
+ @column_names = nil
466
+ @attribute_types = nil
467
+ @content_columns = nil
468
+ @default_attributes = nil
469
+ @inheritance_column = nil unless defined?(@explicit_inheritance_column) && @explicit_inheritance_column
470
+ @attributes_builder = nil
471
+ @columns = nil
472
+ @columns_hash = nil
473
+ @attribute_names = nil
474
+ @yaml_encoder = nil
475
+ direct_descendants.each do |descendant|
476
+ descendant.send(:reload_schema_from_cache)
507
477
  end
478
+ end
508
479
 
509
- "#{full_table_name_prefix}#{contained}#{undecorated_table_name(name)}#{full_table_name_suffix}"
510
- else
511
- # STI subclasses always use their superclass' table.
512
- base.table_name
480
+ # Guesses the table name, but does not decorate it with prefix and suffix information.
481
+ def undecorated_table_name(class_name = base_class.name)
482
+ table_name = class_name.to_s.demodulize.underscore
483
+ pluralize_table_names ? table_name.pluralize : table_name
513
484
  end
514
- end
515
485
 
516
- def warn_if_deprecated_type(column)
517
- return if attributes_to_define_after_schema_loads.key?(column.name)
518
- if column.respond_to?(:oid) && column.sql_type.start_with?("point")
519
- if column.array?
520
- array_arguments = ", array: true"
486
+ # Computes and returns a table name according to default conventions.
487
+ def compute_table_name
488
+ base = base_class
489
+ if self == base
490
+ # Nested classes are prefixed with singular parent table name.
491
+ if parent < Base && !parent.abstract_class?
492
+ contained = parent.table_name
493
+ contained = contained.singularize if parent.pluralize_table_names
494
+ contained += "_"
495
+ end
496
+
497
+ "#{full_table_name_prefix}#{contained}#{undecorated_table_name(name)}#{full_table_name_suffix}"
521
498
  else
522
- array_arguments = ""
499
+ # STI subclasses always use their superclass' table.
500
+ base.table_name
523
501
  end
524
- ActiveSupport::Deprecation.warn(<<-WARNING.strip_heredoc)
525
- The behavior of the `:point` type will be changing in Rails 5.1 to
526
- return a `Point` object, rather than an `Array`. If you'd like to
527
- keep the old behavior, you can add this line to #{self.name}:
528
-
529
- attribute :#{column.name}, :legacy_point#{array_arguments}
530
-
531
- If you'd like the new behavior today, you can add this line:
532
-
533
- attribute :#{column.name}, :point#{array_arguments}
534
- WARNING
535
502
  end
536
- end
537
503
  end
538
504
  end
539
505
  end