activerecord 5.0.6 → 6.0.1

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 (358) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +638 -2023
  3. data/MIT-LICENSE +3 -1
  4. data/README.rdoc +8 -6
  5. data/examples/performance.rb +31 -29
  6. data/examples/simple.rb +5 -3
  7. data/lib/active_record/aggregations.rb +249 -246
  8. data/lib/active_record/association_relation.rb +24 -13
  9. data/lib/active_record/associations/alias_tracker.rb +24 -33
  10. data/lib/active_record/associations/association.rb +119 -56
  11. data/lib/active_record/associations/association_scope.rb +94 -94
  12. data/lib/active_record/associations/belongs_to_association.rb +58 -42
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -12
  14. data/lib/active_record/associations/builder/association.rb +18 -25
  15. data/lib/active_record/associations/builder/belongs_to.rb +43 -54
  16. data/lib/active_record/associations/builder/collection_association.rb +7 -18
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +42 -61
  18. data/lib/active_record/associations/builder/has_many.rb +4 -0
  19. data/lib/active_record/associations/builder/has_one.rb +37 -1
  20. data/lib/active_record/associations/builder/singular_association.rb +4 -0
  21. data/lib/active_record/associations/collection_association.rb +80 -252
  22. data/lib/active_record/associations/collection_proxy.rb +158 -121
  23. data/lib/active_record/associations/foreign_association.rb +9 -0
  24. data/lib/active_record/associations/has_many_association.rb +23 -29
  25. data/lib/active_record/associations/has_many_through_association.rb +58 -44
  26. data/lib/active_record/associations/has_one_association.rb +59 -54
  27. data/lib/active_record/associations/has_one_through_association.rb +20 -11
  28. data/lib/active_record/associations/join_dependency/join_association.rb +38 -90
  29. data/lib/active_record/associations/join_dependency/join_base.rb +10 -9
  30. data/lib/active_record/associations/join_dependency/join_part.rb +12 -12
  31. data/lib/active_record/associations/join_dependency.rb +134 -176
  32. data/lib/active_record/associations/preloader/association.rb +84 -125
  33. data/lib/active_record/associations/preloader/through_association.rb +82 -75
  34. data/lib/active_record/associations/preloader.rb +90 -102
  35. data/lib/active_record/associations/singular_association.rb +12 -45
  36. data/lib/active_record/associations/through_association.rb +26 -14
  37. data/lib/active_record/associations.rb +1603 -1592
  38. data/lib/active_record/attribute_assignment.rb +54 -60
  39. data/lib/active_record/attribute_decorators.rb +38 -15
  40. data/lib/active_record/attribute_methods/before_type_cast.rb +12 -7
  41. data/lib/active_record/attribute_methods/dirty.rb +179 -109
  42. data/lib/active_record/attribute_methods/primary_key.rb +86 -91
  43. data/lib/active_record/attribute_methods/query.rb +4 -3
  44. data/lib/active_record/attribute_methods/read.rb +21 -49
  45. data/lib/active_record/attribute_methods/serialization.rb +30 -7
  46. data/lib/active_record/attribute_methods/time_zone_conversion.rb +39 -64
  47. data/lib/active_record/attribute_methods/write.rb +35 -33
  48. data/lib/active_record/attribute_methods.rb +66 -106
  49. data/lib/active_record/attributes.rb +38 -24
  50. data/lib/active_record/autosave_association.rb +53 -32
  51. data/lib/active_record/base.rb +27 -24
  52. data/lib/active_record/callbacks.rb +63 -33
  53. data/lib/active_record/coders/json.rb +2 -0
  54. data/lib/active_record/coders/yaml_column.rb +11 -11
  55. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +553 -321
  56. data/lib/active_record/connection_adapters/abstract/database_limits.rb +23 -5
  57. data/lib/active_record/connection_adapters/abstract/database_statements.rb +213 -94
  58. data/lib/active_record/connection_adapters/abstract/query_cache.rb +59 -28
  59. data/lib/active_record/connection_adapters/abstract/quoting.rb +119 -75
  60. data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
  61. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +33 -27
  62. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +207 -126
  63. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +68 -80
  64. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +369 -199
  65. data/lib/active_record/connection_adapters/abstract/transaction.rb +169 -78
  66. data/lib/active_record/connection_adapters/abstract_adapter.rb +363 -202
  67. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +405 -551
  68. data/lib/active_record/connection_adapters/column.rb +41 -13
  69. data/lib/active_record/connection_adapters/connection_specification.rb +172 -138
  70. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +11 -4
  71. data/lib/active_record/connection_adapters/mysql/column.rb +8 -31
  72. data/lib/active_record/connection_adapters/mysql/database_statements.rb +143 -49
  73. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +24 -22
  74. data/lib/active_record/connection_adapters/mysql/quoting.rb +50 -20
  75. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +50 -45
  76. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +58 -56
  77. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +70 -36
  78. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +264 -0
  79. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +12 -13
  80. data/lib/active_record/connection_adapters/mysql2_adapter.rb +49 -30
  81. data/lib/active_record/connection_adapters/postgresql/column.rb +22 -7
  82. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +60 -54
  83. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +5 -3
  84. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +22 -10
  85. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +6 -5
  86. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +3 -1
  89. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +4 -2
  91. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
  92. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +5 -3
  93. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +19 -17
  94. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
  96. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +7 -5
  98. data/lib/active_record/connection_adapters/postgresql/oid/{json.rb → oid.rb} +6 -1
  99. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +31 -9
  100. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +34 -30
  101. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +4 -1
  102. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +58 -54
  103. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +9 -4
  104. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid.rb +24 -21
  107. data/lib/active_record/connection_adapters/postgresql/quoting.rb +95 -35
  108. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
  109. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -0
  110. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +147 -105
  111. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +35 -32
  112. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +380 -300
  113. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +26 -25
  114. data/lib/active_record/connection_adapters/postgresql/utils.rb +10 -6
  115. data/lib/active_record/connection_adapters/postgresql_adapter.rb +382 -275
  116. data/lib/active_record/connection_adapters/schema_cache.rb +46 -12
  117. data/lib/active_record/connection_adapters/sql_type_metadata.rb +13 -8
  118. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +120 -0
  119. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +3 -1
  120. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +74 -19
  121. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +3 -8
  122. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
  123. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
  124. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +137 -0
  125. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +254 -262
  126. data/lib/active_record/connection_adapters/statement_pool.rb +9 -7
  127. data/lib/active_record/connection_handling.rb +159 -40
  128. data/lib/active_record/core.rb +202 -162
  129. data/lib/active_record/counter_cache.rb +57 -28
  130. data/lib/active_record/database_configurations/database_config.rb +37 -0
  131. data/lib/active_record/database_configurations/hash_config.rb +50 -0
  132. data/lib/active_record/database_configurations/url_config.rb +79 -0
  133. data/lib/active_record/database_configurations.rb +233 -0
  134. data/lib/active_record/define_callbacks.rb +22 -0
  135. data/lib/active_record/dynamic_matchers.rb +87 -86
  136. data/lib/active_record/enum.rb +60 -23
  137. data/lib/active_record/errors.rb +114 -18
  138. data/lib/active_record/explain.rb +4 -3
  139. data/lib/active_record/explain_registry.rb +3 -1
  140. data/lib/active_record/explain_subscriber.rb +9 -4
  141. data/lib/active_record/fixture_set/file.rb +13 -8
  142. data/lib/active_record/fixture_set/model_metadata.rb +33 -0
  143. data/lib/active_record/fixture_set/render_context.rb +17 -0
  144. data/lib/active_record/fixture_set/table_row.rb +153 -0
  145. data/lib/active_record/fixture_set/table_rows.rb +47 -0
  146. data/lib/active_record/fixtures.rb +195 -502
  147. data/lib/active_record/gem_version.rb +4 -2
  148. data/lib/active_record/inheritance.rb +151 -97
  149. data/lib/active_record/insert_all.rb +179 -0
  150. data/lib/active_record/integration.rb +116 -25
  151. data/lib/active_record/internal_metadata.rb +15 -18
  152. data/lib/active_record/legacy_yaml_adapter.rb +4 -2
  153. data/lib/active_record/locking/optimistic.rb +78 -87
  154. data/lib/active_record/locking/pessimistic.rb +18 -6
  155. data/lib/active_record/log_subscriber.rb +48 -29
  156. data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
  157. data/lib/active_record/middleware/database_selector/resolver.rb +88 -0
  158. data/lib/active_record/middleware/database_selector.rb +75 -0
  159. data/lib/active_record/migration/command_recorder.rb +143 -97
  160. data/lib/active_record/migration/compatibility.rb +174 -56
  161. data/lib/active_record/migration/join_table.rb +8 -6
  162. data/lib/active_record/migration.rb +367 -300
  163. data/lib/active_record/model_schema.rb +145 -139
  164. data/lib/active_record/nested_attributes.rb +214 -201
  165. data/lib/active_record/no_touching.rb +10 -1
  166. data/lib/active_record/null_relation.rb +13 -34
  167. data/lib/active_record/persistence.rb +442 -72
  168. data/lib/active_record/query_cache.rb +15 -14
  169. data/lib/active_record/querying.rb +36 -23
  170. data/lib/active_record/railtie.rb +128 -36
  171. data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
  172. data/lib/active_record/railties/console_sandbox.rb +2 -0
  173. data/lib/active_record/railties/controller_runtime.rb +34 -33
  174. data/lib/active_record/railties/databases.rake +309 -177
  175. data/lib/active_record/readonly_attributes.rb +5 -4
  176. data/lib/active_record/reflection.rb +211 -249
  177. data/lib/active_record/relation/batches/batch_enumerator.rb +3 -1
  178. data/lib/active_record/relation/batches.rb +99 -52
  179. data/lib/active_record/relation/calculations.rb +211 -172
  180. data/lib/active_record/relation/delegation.rb +67 -65
  181. data/lib/active_record/relation/finder_methods.rb +208 -247
  182. data/lib/active_record/relation/from_clause.rb +2 -8
  183. data/lib/active_record/relation/merger.rb +78 -61
  184. data/lib/active_record/relation/predicate_builder/array_handler.rb +20 -14
  185. data/lib/active_record/relation/predicate_builder/association_query_value.rb +43 -0
  186. data/lib/active_record/relation/predicate_builder/base_handler.rb +4 -3
  187. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +6 -4
  188. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +53 -0
  189. data/lib/active_record/relation/predicate_builder/range_handler.rb +7 -18
  190. data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
  191. data/lib/active_record/relation/predicate_builder.rb +86 -104
  192. data/lib/active_record/relation/query_attribute.rb +33 -2
  193. data/lib/active_record/relation/query_methods.rb +458 -329
  194. data/lib/active_record/relation/record_fetch_warning.rb +5 -3
  195. data/lib/active_record/relation/spawn_methods.rb +8 -7
  196. data/lib/active_record/relation/where_clause.rb +111 -95
  197. data/lib/active_record/relation/where_clause_factory.rb +6 -11
  198. data/lib/active_record/relation.rb +429 -318
  199. data/lib/active_record/result.rb +69 -39
  200. data/lib/active_record/runtime_registry.rb +5 -3
  201. data/lib/active_record/sanitization.rb +83 -99
  202. data/lib/active_record/schema.rb +7 -14
  203. data/lib/active_record/schema_dumper.rb +71 -69
  204. data/lib/active_record/schema_migration.rb +15 -5
  205. data/lib/active_record/scoping/default.rb +93 -95
  206. data/lib/active_record/scoping/named.rb +45 -25
  207. data/lib/active_record/scoping.rb +20 -19
  208. data/lib/active_record/secure_token.rb +4 -2
  209. data/lib/active_record/serialization.rb +2 -0
  210. data/lib/active_record/statement_cache.rb +63 -28
  211. data/lib/active_record/store.rb +121 -41
  212. data/lib/active_record/suppressor.rb +4 -1
  213. data/lib/active_record/table_metadata.rb +26 -20
  214. data/lib/active_record/tasks/database_tasks.rb +276 -85
  215. data/lib/active_record/tasks/mysql_database_tasks.rb +54 -90
  216. data/lib/active_record/tasks/postgresql_database_tasks.rb +78 -47
  217. data/lib/active_record/tasks/sqlite_database_tasks.rb +34 -16
  218. data/lib/active_record/test_databases.rb +23 -0
  219. data/lib/active_record/test_fixtures.rb +224 -0
  220. data/lib/active_record/timestamp.rb +70 -35
  221. data/lib/active_record/touch_later.rb +7 -4
  222. data/lib/active_record/transactions.rb +133 -149
  223. data/lib/active_record/translation.rb +3 -1
  224. data/lib/active_record/type/adapter_specific_registry.rb +44 -45
  225. data/lib/active_record/type/date.rb +2 -0
  226. data/lib/active_record/type/date_time.rb +2 -0
  227. data/lib/active_record/type/decimal_without_scale.rb +15 -0
  228. data/lib/active_record/type/hash_lookup_type_map.rb +5 -3
  229. data/lib/active_record/type/internal/timezone.rb +2 -0
  230. data/lib/active_record/type/json.rb +30 -0
  231. data/lib/active_record/type/serialized.rb +16 -8
  232. data/lib/active_record/type/text.rb +11 -0
  233. data/lib/active_record/type/time.rb +2 -1
  234. data/lib/active_record/type/type_map.rb +13 -15
  235. data/lib/active_record/type/unsigned_integer.rb +17 -0
  236. data/lib/active_record/type.rb +23 -17
  237. data/lib/active_record/type_caster/connection.rb +17 -12
  238. data/lib/active_record/type_caster/map.rb +5 -4
  239. data/lib/active_record/type_caster.rb +4 -2
  240. data/lib/active_record/validations/absence.rb +2 -0
  241. data/lib/active_record/validations/associated.rb +3 -1
  242. data/lib/active_record/validations/length.rb +2 -0
  243. data/lib/active_record/validations/presence.rb +4 -2
  244. data/lib/active_record/validations/uniqueness.rb +29 -42
  245. data/lib/active_record/validations.rb +7 -4
  246. data/lib/active_record/version.rb +3 -1
  247. data/lib/active_record.rb +36 -22
  248. data/lib/arel/alias_predication.rb +9 -0
  249. data/lib/arel/attributes/attribute.rb +37 -0
  250. data/lib/arel/attributes.rb +22 -0
  251. data/lib/arel/collectors/bind.rb +24 -0
  252. data/lib/arel/collectors/composite.rb +31 -0
  253. data/lib/arel/collectors/plain_string.rb +20 -0
  254. data/lib/arel/collectors/sql_string.rb +20 -0
  255. data/lib/arel/collectors/substitute_binds.rb +28 -0
  256. data/lib/arel/crud.rb +42 -0
  257. data/lib/arel/delete_manager.rb +18 -0
  258. data/lib/arel/errors.rb +9 -0
  259. data/lib/arel/expressions.rb +29 -0
  260. data/lib/arel/factory_methods.rb +49 -0
  261. data/lib/arel/insert_manager.rb +49 -0
  262. data/lib/arel/math.rb +45 -0
  263. data/lib/arel/nodes/and.rb +32 -0
  264. data/lib/arel/nodes/ascending.rb +23 -0
  265. data/lib/arel/nodes/binary.rb +52 -0
  266. data/lib/arel/nodes/bind_param.rb +36 -0
  267. data/lib/arel/nodes/case.rb +55 -0
  268. data/lib/arel/nodes/casted.rb +50 -0
  269. data/lib/arel/nodes/comment.rb +29 -0
  270. data/lib/arel/nodes/count.rb +12 -0
  271. data/lib/arel/nodes/delete_statement.rb +45 -0
  272. data/lib/arel/nodes/descending.rb +23 -0
  273. data/lib/arel/nodes/equality.rb +18 -0
  274. data/lib/arel/nodes/extract.rb +24 -0
  275. data/lib/arel/nodes/false.rb +16 -0
  276. data/lib/arel/nodes/full_outer_join.rb +8 -0
  277. data/lib/arel/nodes/function.rb +44 -0
  278. data/lib/arel/nodes/grouping.rb +8 -0
  279. data/lib/arel/nodes/in.rb +8 -0
  280. data/lib/arel/nodes/infix_operation.rb +80 -0
  281. data/lib/arel/nodes/inner_join.rb +8 -0
  282. data/lib/arel/nodes/insert_statement.rb +37 -0
  283. data/lib/arel/nodes/join_source.rb +20 -0
  284. data/lib/arel/nodes/matches.rb +18 -0
  285. data/lib/arel/nodes/named_function.rb +23 -0
  286. data/lib/arel/nodes/node.rb +50 -0
  287. data/lib/arel/nodes/node_expression.rb +13 -0
  288. data/lib/arel/nodes/outer_join.rb +8 -0
  289. data/lib/arel/nodes/over.rb +15 -0
  290. data/lib/arel/nodes/regexp.rb +16 -0
  291. data/lib/arel/nodes/right_outer_join.rb +8 -0
  292. data/lib/arel/nodes/select_core.rb +67 -0
  293. data/lib/arel/nodes/select_statement.rb +41 -0
  294. data/lib/arel/nodes/sql_literal.rb +16 -0
  295. data/lib/arel/nodes/string_join.rb +11 -0
  296. data/lib/arel/nodes/table_alias.rb +27 -0
  297. data/lib/arel/nodes/terminal.rb +16 -0
  298. data/lib/arel/nodes/true.rb +16 -0
  299. data/lib/arel/nodes/unary.rb +45 -0
  300. data/lib/arel/nodes/unary_operation.rb +20 -0
  301. data/lib/arel/nodes/unqualified_column.rb +22 -0
  302. data/lib/arel/nodes/update_statement.rb +41 -0
  303. data/lib/arel/nodes/values_list.rb +9 -0
  304. data/lib/arel/nodes/window.rb +126 -0
  305. data/lib/arel/nodes/with.rb +11 -0
  306. data/lib/arel/nodes.rb +68 -0
  307. data/lib/arel/order_predications.rb +13 -0
  308. data/lib/arel/predications.rb +257 -0
  309. data/lib/arel/select_manager.rb +271 -0
  310. data/lib/arel/table.rb +110 -0
  311. data/lib/arel/tree_manager.rb +72 -0
  312. data/lib/arel/update_manager.rb +34 -0
  313. data/lib/arel/visitors/depth_first.rb +204 -0
  314. data/lib/arel/visitors/dot.rb +297 -0
  315. data/lib/arel/visitors/ibm_db.rb +34 -0
  316. data/lib/arel/visitors/informix.rb +62 -0
  317. data/lib/arel/visitors/mssql.rb +157 -0
  318. data/lib/arel/visitors/mysql.rb +83 -0
  319. data/lib/arel/visitors/oracle.rb +159 -0
  320. data/lib/arel/visitors/oracle12.rb +66 -0
  321. data/lib/arel/visitors/postgresql.rb +110 -0
  322. data/lib/arel/visitors/sqlite.rb +39 -0
  323. data/lib/arel/visitors/to_sql.rb +889 -0
  324. data/lib/arel/visitors/visitor.rb +46 -0
  325. data/lib/arel/visitors/where_sql.rb +23 -0
  326. data/lib/arel/visitors.rb +20 -0
  327. data/lib/arel/window_predications.rb +9 -0
  328. data/lib/arel.rb +58 -0
  329. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  330. data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
  331. data/lib/rails/generators/active_record/migration/migration_generator.rb +37 -35
  332. data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +1 -1
  333. data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +4 -2
  334. data/lib/rails/generators/active_record/migration.rb +17 -2
  335. data/lib/rails/generators/active_record/model/model_generator.rb +9 -29
  336. data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +10 -1
  337. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
  338. data/lib/rails/generators/active_record.rb +7 -5
  339. metadata +133 -50
  340. data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
  341. data/lib/active_record/associations/preloader/collection_association.rb +0 -17
  342. data/lib/active_record/associations/preloader/has_many.rb +0 -17
  343. data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
  344. data/lib/active_record/associations/preloader/has_one.rb +0 -15
  345. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  346. data/lib/active_record/associations/preloader/singular_association.rb +0 -20
  347. data/lib/active_record/attribute/user_provided_default.rb +0 -28
  348. data/lib/active_record/attribute.rb +0 -213
  349. data/lib/active_record/attribute_mutation_tracker.rb +0 -70
  350. data/lib/active_record/attribute_set/builder.rb +0 -130
  351. data/lib/active_record/attribute_set.rb +0 -110
  352. data/lib/active_record/collection_cache_key.rb +0 -50
  353. data/lib/active_record/connection_adapters/postgresql/oid/rails_5_1_point.rb +0 -50
  354. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  355. data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
  356. data/lib/active_record/relation/predicate_builder/class_handler.rb +0 -27
  357. data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -57
  358. data/lib/active_record/type/internal/abstract_json.rb +0 -33
@@ -1,40 +1,60 @@
1
- require 'active_record'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+
5
+ databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
2
6
 
3
7
  db_namespace = namespace :db do
4
8
  desc "Set the environment value for the database"
5
- task "environment:set" => [:environment, :load_config] do
9
+ task "environment:set" => :load_config do
6
10
  ActiveRecord::InternalMetadata.create_table
7
- ActiveRecord::InternalMetadata[:environment] = ActiveRecord::Migrator.current_environment
11
+ ActiveRecord::InternalMetadata[:environment] = ActiveRecord::Base.connection.migration_context.current_environment
8
12
  end
9
13
 
10
- task :check_protected_environments => [:environment, :load_config] do
14
+ task check_protected_environments: :load_config do
11
15
  ActiveRecord::Tasks::DatabaseTasks.check_protected_environments!
12
16
  end
13
17
 
14
- task :load_config do
18
+ task load_config: :environment do
15
19
  ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {}
16
20
  ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
17
21
  end
18
22
 
19
23
  namespace :create do
20
- task :all => :load_config do
24
+ task all: :load_config do
21
25
  ActiveRecord::Tasks::DatabaseTasks.create_all
22
26
  end
27
+
28
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
29
+ desc "Create #{spec_name} database for current environment"
30
+ task spec_name => :load_config do
31
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
32
+ ActiveRecord::Tasks::DatabaseTasks.create(db_config.config)
33
+ end
34
+ end
23
35
  end
24
36
 
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
37
+ 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."
38
+ task create: [:load_config] do
27
39
  ActiveRecord::Tasks::DatabaseTasks.create_current
28
40
  end
29
41
 
30
42
  namespace :drop do
31
- task :all => [:load_config, :check_protected_environments] do
43
+ task all: [:load_config, :check_protected_environments] do
32
44
  ActiveRecord::Tasks::DatabaseTasks.drop_all
33
45
  end
46
+
47
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
48
+ desc "Drop #{spec_name} database for current environment"
49
+ task spec_name => [:load_config, :check_protected_environments] do
50
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
51
+ ActiveRecord::Tasks::DatabaseTasks.drop(db_config.config)
52
+ end
53
+ end
34
54
  end
35
55
 
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
56
+ 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."
57
+ task drop: [:load_config, :check_protected_environments] do
38
58
  db_namespace["drop:_unsafe"].invoke
39
59
  end
40
60
 
@@ -43,20 +63,28 @@ db_namespace = namespace :db do
43
63
  end
44
64
 
45
65
  namespace :purge do
46
- task :all => [:load_config, :check_protected_environments] do
66
+ task all: [:load_config, :check_protected_environments] do
47
67
  ActiveRecord::Tasks::DatabaseTasks.purge_all
48
68
  end
49
69
  end
50
70
 
71
+ # desc "Truncates tables of each database for current environment"
72
+ task truncate_all: [:load_config, :check_protected_environments] do
73
+ ActiveRecord::Tasks::DatabaseTasks.truncate_all
74
+ end
75
+
51
76
  # 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
77
+ task purge: [:load_config, :check_protected_environments] do
53
78
  ActiveRecord::Tasks::DatabaseTasks.purge_current
54
79
  end
55
80
 
56
81
  desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
57
- task :migrate => [:environment, :load_config] do
58
- ActiveRecord::Tasks::DatabaseTasks.migrate
59
- db_namespace['_dump'].invoke
82
+ task migrate: :load_config do
83
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
84
+ ActiveRecord::Base.establish_connection(db_config.config)
85
+ ActiveRecord::Tasks::DatabaseTasks.migrate
86
+ end
87
+ db_namespace["_dump"].invoke
60
88
  end
61
89
 
62
90
  # IMPORTANT: This task won't dump the schema if ActiveRecord::Base.dump_schema_after_migration is set to false
@@ -71,155 +99,276 @@ db_namespace = namespace :db do
71
99
  end
72
100
  # Allow this task to be called as many times as required. An example is the
73
101
  # migrate:redo task, which calls other two internally that depend on this one.
74
- db_namespace['_dump'].reenable
102
+ db_namespace["_dump"].reenable
75
103
  end
76
104
 
77
105
  namespace :migrate do
106
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
107
+ desc "Migrate #{spec_name} database for current environment"
108
+ task spec_name => :load_config do
109
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
110
+ ActiveRecord::Base.establish_connection(db_config.config)
111
+ ActiveRecord::Tasks::DatabaseTasks.migrate
112
+ end
113
+ end
114
+
78
115
  # desc 'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
79
- task :redo => [:environment, :load_config] do
116
+ task redo: :load_config do
80
117
  raise "Empty VERSION provided" if ENV["VERSION"] && ENV["VERSION"].empty?
81
118
 
82
- if ENV['VERSION']
83
- db_namespace['migrate:down'].invoke
84
- db_namespace['migrate:up'].invoke
119
+ if ENV["VERSION"]
120
+ db_namespace["migrate:down"].invoke
121
+ db_namespace["migrate:up"].invoke
85
122
  else
86
- db_namespace['rollback'].invoke
87
- db_namespace['migrate'].invoke
123
+ db_namespace["rollback"].invoke
124
+ db_namespace["migrate"].invoke
88
125
  end
89
126
  end
90
127
 
91
128
  # desc 'Resets your database using your migrations for the current environment'
92
- task :reset => ['db:drop', 'db:create', 'db:migrate']
129
+ task reset: ["db:drop", "db:create", "db:migrate"]
93
130
 
94
131
  # desc 'Runs the "up" for a given migration VERSION.'
95
- task :up => [:environment, :load_config] do
132
+ task up: :load_config do
133
+ ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:migrate:up")
134
+
96
135
  raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
97
136
 
98
- version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
99
- ActiveRecord::Migrator.run(:up, ActiveRecord::Tasks::DatabaseTasks.migrations_paths, version)
100
- db_namespace['_dump'].invoke
137
+ ActiveRecord::Tasks::DatabaseTasks.check_target_version
138
+
139
+ ActiveRecord::Base.connection.migration_context.run(
140
+ :up,
141
+ ActiveRecord::Tasks::DatabaseTasks.target_version
142
+ )
143
+ db_namespace["_dump"].invoke
144
+ end
145
+
146
+ namespace :up do
147
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
148
+ task spec_name => :load_config do
149
+ raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
150
+
151
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
152
+
153
+ ActiveRecord::Base.establish_connection(db_config.config)
154
+ ActiveRecord::Tasks::DatabaseTasks.check_target_version
155
+ ActiveRecord::Base.connection.migration_context.run(
156
+ :up,
157
+ ActiveRecord::Tasks::DatabaseTasks.target_version
158
+ )
159
+
160
+ db_namespace["_dump"].invoke
161
+ end
162
+ end
101
163
  end
102
164
 
103
165
  # desc 'Runs the "down" for a given migration VERSION.'
104
- task :down => [:environment, :load_config] do
166
+ task down: :load_config do
167
+ ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:migrate:down")
168
+
105
169
  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
107
- ActiveRecord::Migrator.run(:down, ActiveRecord::Tasks::DatabaseTasks.migrations_paths, version)
108
- db_namespace['_dump'].invoke
170
+
171
+ ActiveRecord::Tasks::DatabaseTasks.check_target_version
172
+
173
+ ActiveRecord::Base.connection.migration_context.run(
174
+ :down,
175
+ ActiveRecord::Tasks::DatabaseTasks.target_version
176
+ )
177
+ db_namespace["_dump"].invoke
178
+ end
179
+
180
+ namespace :down do
181
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
182
+ task spec_name => :load_config do
183
+ raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
184
+
185
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
186
+
187
+ ActiveRecord::Base.establish_connection(db_config.config)
188
+ ActiveRecord::Tasks::DatabaseTasks.check_target_version
189
+ ActiveRecord::Base.connection.migration_context.run(
190
+ :down,
191
+ ActiveRecord::Tasks::DatabaseTasks.target_version
192
+ )
193
+
194
+ db_namespace["_dump"].invoke
195
+ end
196
+ end
109
197
  end
110
198
 
111
- desc 'Display status of migrations'
112
- task :status => [:environment, :load_config] do
113
- unless ActiveRecord::SchemaMigration.table_exists?
114
- abort 'Schema migrations table does not exist yet.'
199
+ desc "Display status of migrations"
200
+ task status: :load_config do
201
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
202
+ ActiveRecord::Base.establish_connection(db_config.config)
203
+ ActiveRecord::Tasks::DatabaseTasks.migrate_status
115
204
  end
205
+ end
116
206
 
117
- # output
118
- puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
119
- puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
120
- puts "-" * 50
121
- paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
122
- ActiveRecord::Migrator.migrations_status(paths).each do |status, version, name|
123
- puts "#{status.center(8)} #{version.ljust(14)} #{name}"
207
+ namespace :status do
208
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
209
+ desc "Display status of migrations for #{spec_name} database"
210
+ task spec_name => :load_config do
211
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
212
+ ActiveRecord::Base.establish_connection(db_config.config)
213
+ ActiveRecord::Tasks::DatabaseTasks.migrate_status
214
+ end
124
215
  end
125
- puts
126
216
  end
127
217
  end
128
218
 
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
132
- ActiveRecord::Migrator.rollback(ActiveRecord::Tasks::DatabaseTasks.migrations_paths, step)
133
- db_namespace['_dump'].invoke
219
+ desc "Rolls the schema back to the previous version (specify steps w/ STEP=n)."
220
+ task rollback: :load_config do
221
+ step = ENV["STEP"] ? ENV["STEP"].to_i : 1
222
+ ActiveRecord::Base.connection.migration_context.rollback(step)
223
+ db_namespace["_dump"].invoke
134
224
  end
135
225
 
136
226
  # 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
139
- ActiveRecord::Migrator.forward(ActiveRecord::Tasks::DatabaseTasks.migrations_paths, step)
140
- db_namespace['_dump'].invoke
227
+ task forward: :load_config do
228
+ step = ENV["STEP"] ? ENV["STEP"].to_i : 1
229
+ ActiveRecord::Base.connection.migration_context.forward(step)
230
+ db_namespace["_dump"].invoke
141
231
  end
142
232
 
143
233
  # 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' ]
234
+ task reset: [ "db:drop", "db:setup" ]
145
235
 
146
236
  # desc "Retrieves the charset for the current environment's database"
147
- task :charset => [:environment, :load_config] do
237
+ task charset: :load_config do
148
238
  puts ActiveRecord::Tasks::DatabaseTasks.charset_current
149
239
  end
150
240
 
151
241
  # desc "Retrieves the collation for the current environment's database"
152
- task :collation => [:environment, :load_config] do
153
- begin
154
- puts ActiveRecord::Tasks::DatabaseTasks.collation_current
155
- rescue NoMethodError
156
- $stderr.puts 'Sorry, your database adapter is not supported yet. Feel free to submit a patch.'
157
- end
242
+ task collation: :load_config do
243
+ puts ActiveRecord::Tasks::DatabaseTasks.collation_current
244
+ rescue NoMethodError
245
+ $stderr.puts "Sorry, your database adapter is not supported yet. Feel free to submit a patch."
158
246
  end
159
247
 
160
- desc 'Retrieves the current schema version number'
161
- task :version => [:environment, :load_config] do
162
- puts "Current version: #{ActiveRecord::Migrator.current_version}"
248
+ desc "Retrieves the current schema version number"
249
+ task version: :load_config do
250
+ puts "Current version: #{ActiveRecord::Base.connection.migration_context.current_version}"
163
251
  end
164
252
 
165
253
  # desc "Raises an error if there are pending migrations"
166
- task :abort_if_pending_migrations => [:environment, :load_config] do
167
- pending_migrations = ActiveRecord::Migrator.open(ActiveRecord::Tasks::DatabaseTasks.migrations_paths).pending_migrations
254
+ task abort_if_pending_migrations: :load_config do
255
+ pending_migrations = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).flat_map do |db_config|
256
+ ActiveRecord::Base.establish_connection(db_config.config)
257
+
258
+ ActiveRecord::Base.connection.migration_context.open.pending_migrations
259
+ end
168
260
 
169
261
  if pending_migrations.any?
170
262
  puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
171
263
  pending_migrations.each do |pending_migration|
172
- puts ' %4d %s' % [pending_migration.version, pending_migration.name]
264
+ puts " %4d %s" % [pending_migration.version, pending_migration.name]
173
265
  end
174
266
  abort %{Run `rails db:migrate` to update your database then try again.}
175
267
  end
268
+ ensure
269
+ ActiveRecord::Base.establish_connection(ActiveRecord::Tasks::DatabaseTasks.env.to_sym)
270
+ end
271
+
272
+ namespace :abort_if_pending_migrations do
273
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
274
+ # desc "Raises an error if there are pending migrations for #{spec_name} database"
275
+ task spec_name => :load_config do
276
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
277
+ ActiveRecord::Base.establish_connection(db_config.config)
278
+
279
+ pending_migrations = ActiveRecord::Base.connection.migration_context.open.pending_migrations
280
+
281
+ if pending_migrations.any?
282
+ puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
283
+ pending_migrations.each do |pending_migration|
284
+ puts " %4d %s" % [pending_migration.version, pending_migration.name]
285
+ end
286
+ abort %{Run `rails db:migrate:#{spec_name}` to update your database then try again.}
287
+ end
288
+ end
289
+ end
176
290
  end
177
291
 
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]
292
+ desc "Creates the database, loads the schema, and initializes with the seed data (use db:reset to also drop the database first)"
293
+ task setup: ["db:schema:load_if_ruby", "db:structure:load_if_sql", :seed]
294
+
295
+ desc "Runs setup if database does not exist, or runs migrations if it does"
296
+ task prepare: :load_config do
297
+ seed = false
298
+
299
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
300
+ ActiveRecord::Base.establish_connection(db_config.config)
301
+
302
+ # Skipped when no database
303
+ ActiveRecord::Tasks::DatabaseTasks.migrate
304
+ if ActiveRecord::Base.dump_schema_after_migration
305
+ ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config.config, ActiveRecord::Base.schema_format, db_config.spec_name)
306
+ end
307
+
308
+ rescue ActiveRecord::NoDatabaseError
309
+ ActiveRecord::Tasks::DatabaseTasks.create_current(db_config.env_name, db_config.spec_name)
310
+ ActiveRecord::Tasks::DatabaseTasks.load_schema(
311
+ db_config.config,
312
+ ActiveRecord::Base.schema_format,
313
+ nil,
314
+ db_config.env_name,
315
+ db_config.spec_name
316
+ )
317
+
318
+ seed = true
319
+ end
180
320
 
181
- desc 'Loads the seed data from db/seeds.rb'
182
- task :seed do
183
- db_namespace['abort_if_pending_migrations'].invoke
321
+ ActiveRecord::Base.establish_connection
322
+ ActiveRecord::Tasks::DatabaseTasks.load_seed if seed
323
+ end
324
+
325
+ desc "Loads the seed data from db/seeds.rb"
326
+ task seed: :load_config do
327
+ db_namespace["abort_if_pending_migrations"].invoke
184
328
  ActiveRecord::Tasks::DatabaseTasks.load_seed
185
329
  end
186
330
 
331
+ namespace :seed do
332
+ desc "Truncates tables of each database for current environment and loads the seeds"
333
+ task replant: [:load_config, :truncate_all, :seed]
334
+ end
335
+
187
336
  namespace :fixtures do
188
337
  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'
338
+ task load: :load_config do
339
+ require "active_record/fixtures"
191
340
 
192
341
  base_dir = ActiveRecord::Tasks::DatabaseTasks.fixtures_path
193
342
 
194
- fixtures_dir = if ENV['FIXTURES_DIR']
195
- File.join base_dir, ENV['FIXTURES_DIR']
196
- else
197
- base_dir
198
- end
343
+ fixtures_dir = if ENV["FIXTURES_DIR"]
344
+ File.join base_dir, ENV["FIXTURES_DIR"]
345
+ else
346
+ base_dir
347
+ end
199
348
 
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
349
+ fixture_files = if ENV["FIXTURES"]
350
+ ENV["FIXTURES"].split(",")
351
+ else
352
+ # The use of String#[] here is to support namespaced fixtures.
353
+ Dir["#{fixtures_dir}/**/*.yml"].map { |f| f[(fixtures_dir.size + 1)..-5] }
354
+ end
206
355
 
207
356
  ActiveRecord::FixtureSet.create_fixtures(fixtures_dir, fixture_files)
208
357
  end
209
358
 
210
359
  # 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'
360
+ task identify: :load_config do
361
+ require "active_record/fixtures"
213
362
 
214
- label, id = ENV['LABEL'], ENV['ID']
215
- raise 'LABEL or ID required' if label.blank? && id.blank?
363
+ label, id = ENV["LABEL"], ENV["ID"]
364
+ raise "LABEL or ID required" if label.blank? && id.blank?
216
365
 
217
366
  puts %Q(The fixture ID for "#{label}" is #{ActiveRecord::FixtureSet.identify(label)}.) if label
218
367
 
219
368
  base_dir = ActiveRecord::Tasks::DatabaseTasks.fixtures_path
220
369
 
221
370
  Dir["#{base_dir}/**/*.yml"].each do |file|
222
- if data = YAML::load(ERB.new(IO.read(file)).result)
371
+ if data = YAML.load(ERB.new(IO.read(file)).result)
223
372
  data.each_key do |key|
224
373
  key_id = ActiveRecord::FixtureSet.identify(key)
225
374
 
@@ -233,134 +382,113 @@ db_namespace = namespace :db do
233
382
  end
234
383
 
235
384
  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')
240
- File.open(filename, "w:utf-8") do |file|
241
- ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
385
+ desc "Creates a db/schema.rb file that is portable against any DB supported by Active Record"
386
+ task dump: :load_config do
387
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
388
+ ActiveRecord::Base.establish_connection(db_config.config)
389
+ ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config.config, :ruby, db_config.spec_name)
242
390
  end
243
- db_namespace['schema:dump'].reenable
391
+
392
+ db_namespace["schema:dump"].reenable
244
393
  end
245
394
 
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'])
395
+ desc "Loads a schema.rb file into the database"
396
+ task load: [:load_config, :check_protected_environments] do
397
+ ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV["SCHEMA"])
249
398
  end
250
399
 
251
- task :load_if_ruby => ['db:create', :environment] do
400
+ task load_if_ruby: ["db:create", :environment] do
252
401
  db_namespace["schema:load"].invoke if ActiveRecord::Base.schema_format == :ruby
253
402
  end
254
403
 
255
404
  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)) }
405
+ desc "Creates a db/schema_cache.yml file."
406
+ task dump: :load_config do
407
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
408
+ ActiveRecord::Base.establish_connection(db_config.config)
409
+ filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config.spec_name)
410
+ ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(
411
+ ActiveRecord::Base.connection,
412
+ filename,
413
+ )
414
+ end
264
415
  end
265
416
 
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")
269
- rm_f filename, verbose: false
417
+ desc "Clears a db/schema_cache.yml file."
418
+ task clear: :load_config do
419
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
420
+ filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config.spec_name)
421
+ rm_f filename, verbose: false
422
+ end
270
423
  end
271
424
  end
272
-
273
425
  end
274
426
 
275
427
  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")
279
- current_config = ActiveRecord::Tasks::DatabaseTasks.current_config
280
- ActiveRecord::Tasks::DatabaseTasks.structure_dump(current_config, filename)
281
-
282
- if ActiveRecord::Base.connection.supports_migrations? &&
283
- ActiveRecord::SchemaMigration.table_exists?
284
- File.open(filename, "a") do |f|
285
- f.puts ActiveRecord::Base.connection.dump_schema_information
286
- f.print "\n"
287
- end
428
+ desc "Dumps the database structure to db/structure.sql. Specify another file with SCHEMA=db/my_structure.sql"
429
+ task dump: :load_config do
430
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
431
+ ActiveRecord::Base.establish_connection(db_config.config)
432
+ ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config.config, :sql, db_config.spec_name)
288
433
  end
289
- db_namespace['structure:dump'].reenable
434
+
435
+ db_namespace["structure:dump"].reenable
290
436
  end
291
437
 
292
438
  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'])
439
+ task load: [:load_config, :check_protected_environments] do
440
+ ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:sql, ENV["SCHEMA"])
295
441
  end
296
442
 
297
- task :load_if_sql => ['db:create', :environment] do
443
+ task load_if_sql: ["db:create", :environment] do
298
444
  db_namespace["structure:load"].invoke if ActiveRecord::Base.schema_format == :sql
299
445
  end
300
446
  end
301
447
 
302
448
  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
449
  # desc "Recreate the test database from the current schema"
312
- task :load => %w(db:test:purge) do
450
+ task load: %w(db:test:purge) do
313
451
  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
452
+ when :ruby
453
+ db_namespace["test:load_schema"].invoke
454
+ when :sql
455
+ db_namespace["test:load_structure"].invoke
318
456
  end
319
457
  end
320
458
 
321
459
  # desc "Recreate the test database from an existent schema.rb file"
322
- task :load_schema => %w(db:test:purge) do
323
- begin
324
- should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
325
- ActiveRecord::Schema.verbose = false
326
- ActiveRecord::Tasks::DatabaseTasks.load_schema ActiveRecord::Base.configurations['test'], :ruby, ENV['SCHEMA']
327
- ensure
328
- if should_reconnect
329
- ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ActiveRecord::Tasks::DatabaseTasks.env])
330
- end
460
+ task load_schema: %w(db:test:purge) do
461
+ should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
462
+ ActiveRecord::Schema.verbose = false
463
+ ActiveRecord::Base.configurations.configs_for(env_name: "test").each do |db_config|
464
+ filename = ActiveRecord::Tasks::DatabaseTasks.dump_filename(db_config.spec_name, :ruby)
465
+ ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config.config, :ruby, filename, "test")
466
+ end
467
+ ensure
468
+ if should_reconnect
469
+ ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations.default_hash(ActiveRecord::Tasks::DatabaseTasks.env))
331
470
  end
332
471
  end
333
472
 
334
473
  # 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']
337
- end
338
-
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
474
+ task load_structure: %w(db:test:purge) do
475
+ ActiveRecord::Base.configurations.configs_for(env_name: "test").each do |db_config|
476
+ filename = ActiveRecord::Tasks::DatabaseTasks.dump_filename(db_config.spec_name, :sql)
477
+ ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config.config, :sql, filename, "test")
346
478
  end
347
479
  end
348
480
 
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
481
  # 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']
482
+ task purge: %w(load_config check_protected_environments) do
483
+ ActiveRecord::Base.configurations.configs_for(env_name: "test").each do |db_config|
484
+ ActiveRecord::Tasks::DatabaseTasks.purge(db_config.config)
485
+ end
358
486
  end
359
487
 
360
488
  # desc 'Load the test schema'
361
- task :prepare => %w(environment load_config) do
489
+ task prepare: :load_config do
362
490
  unless ActiveRecord::Base.configurations.blank?
363
- db_namespace['test:load'].invoke
491
+ db_namespace["test:load"].invoke
364
492
  end
365
493
  end
366
494
  end
@@ -369,15 +497,19 @@ end
369
497
  namespace :railties do
370
498
  namespace :install do
371
499
  # 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)
500
+ task migrations: :'db:load_config' do
501
+ to_load = ENV["FROM"].blank? ? :all : ENV["FROM"].split(",").map(&:strip)
374
502
  railties = {}
375
503
  Rails.application.migration_railties.each do |railtie|
376
504
  next unless to_load == :all || to_load.include?(railtie.railtie_name)
377
505
 
378
- if railtie.respond_to?(:paths) && (path = railtie.paths['db/migrate'].first)
506
+ if railtie.respond_to?(:paths) && (path = railtie.paths["db/migrate"].first)
379
507
  railties[railtie.railtie_name] = path
380
508
  end
509
+
510
+ unless ENV["MIGRATIONS_PATH"].blank?
511
+ railties[railtie.railtie_name] = railtie.root + ENV["MIGRATIONS_PATH"]
512
+ end
381
513
  end
382
514
 
383
515
  on_skip = Proc.new do |name, migration|
@@ -389,7 +521,7 @@ namespace :railties do
389
521
  end
390
522
 
391
523
  ActiveRecord::Migration.copy(ActiveRecord::Tasks::DatabaseTasks.migrations_paths.first, railties,
392
- :on_skip => on_skip, :on_copy => on_copy)
524
+ on_skip: on_skip, on_copy: on_copy)
393
525
  end
394
526
  end
395
527
  end