activerecord 3.2.6 → 6.0.0

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 (371) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +611 -6417
  3. data/MIT-LICENSE +4 -2
  4. data/README.rdoc +44 -47
  5. data/examples/performance.rb +79 -71
  6. data/examples/simple.rb +6 -5
  7. data/lib/active_record/aggregations.rb +268 -238
  8. data/lib/active_record/association_relation.rb +40 -0
  9. data/lib/active_record/associations/alias_tracker.rb +47 -42
  10. data/lib/active_record/associations/association.rb +173 -81
  11. data/lib/active_record/associations/association_scope.rb +124 -92
  12. data/lib/active_record/associations/belongs_to_association.rb +83 -38
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +11 -9
  14. data/lib/active_record/associations/builder/association.rb +113 -32
  15. data/lib/active_record/associations/builder/belongs_to.rb +105 -60
  16. data/lib/active_record/associations/builder/collection_association.rb +53 -56
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +98 -41
  18. data/lib/active_record/associations/builder/has_many.rb +11 -63
  19. data/lib/active_record/associations/builder/has_one.rb +47 -45
  20. data/lib/active_record/associations/builder/singular_association.rb +30 -18
  21. data/lib/active_record/associations/collection_association.rb +217 -295
  22. data/lib/active_record/associations/collection_proxy.rb +1074 -77
  23. data/lib/active_record/associations/foreign_association.rb +20 -0
  24. data/lib/active_record/associations/has_many_association.rb +78 -50
  25. data/lib/active_record/associations/has_many_through_association.rb +99 -61
  26. data/lib/active_record/associations/has_one_association.rb +75 -30
  27. data/lib/active_record/associations/has_one_through_association.rb +20 -11
  28. data/lib/active_record/associations/join_dependency/join_association.rb +45 -119
  29. data/lib/active_record/associations/join_dependency/join_base.rb +11 -12
  30. data/lib/active_record/associations/join_dependency/join_part.rb +35 -42
  31. data/lib/active_record/associations/join_dependency.rb +208 -164
  32. data/lib/active_record/associations/preloader/association.rb +93 -87
  33. data/lib/active_record/associations/preloader/through_association.rb +87 -38
  34. data/lib/active_record/associations/preloader.rb +134 -110
  35. data/lib/active_record/associations/singular_association.rb +19 -24
  36. data/lib/active_record/associations/through_association.rb +61 -27
  37. data/lib/active_record/associations.rb +1766 -1505
  38. data/lib/active_record/attribute_assignment.rb +57 -193
  39. data/lib/active_record/attribute_decorators.rb +90 -0
  40. data/lib/active_record/attribute_methods/before_type_cast.rb +58 -8
  41. data/lib/active_record/attribute_methods/dirty.rb +187 -67
  42. data/lib/active_record/attribute_methods/primary_key.rb +100 -78
  43. data/lib/active_record/attribute_methods/query.rb +10 -8
  44. data/lib/active_record/attribute_methods/read.rb +29 -118
  45. data/lib/active_record/attribute_methods/serialization.rb +60 -72
  46. data/lib/active_record/attribute_methods/time_zone_conversion.rb +69 -42
  47. data/lib/active_record/attribute_methods/write.rb +36 -44
  48. data/lib/active_record/attribute_methods.rb +306 -161
  49. data/lib/active_record/attributes.rb +279 -0
  50. data/lib/active_record/autosave_association.rb +324 -238
  51. data/lib/active_record/base.rb +114 -507
  52. data/lib/active_record/callbacks.rb +147 -83
  53. data/lib/active_record/coders/json.rb +15 -0
  54. data/lib/active_record/coders/yaml_column.rb +32 -23
  55. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +962 -279
  56. data/lib/active_record/connection_adapters/abstract/database_limits.rb +32 -5
  57. data/lib/active_record/connection_adapters/abstract/database_statements.rb +331 -209
  58. data/lib/active_record/connection_adapters/abstract/query_cache.rb +95 -23
  59. data/lib/active_record/connection_adapters/abstract/quoting.rb +201 -65
  60. data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
  61. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +153 -0
  62. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +510 -289
  63. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +93 -0
  64. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1182 -313
  65. data/lib/active_record/connection_adapters/abstract/transaction.rb +323 -0
  66. data/lib/active_record/connection_adapters/abstract_adapter.rb +585 -120
  67. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +610 -463
  68. data/lib/active_record/connection_adapters/column.rb +58 -233
  69. data/lib/active_record/connection_adapters/connection_specification.rb +297 -0
  70. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +29 -0
  71. data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
  72. data/lib/active_record/connection_adapters/mysql/database_statements.rb +200 -0
  73. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
  74. data/lib/active_record/connection_adapters/mysql/quoting.rb +81 -0
  75. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +72 -0
  76. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +95 -0
  77. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +88 -0
  78. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +264 -0
  79. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +31 -0
  80. data/lib/active_record/connection_adapters/mysql2_adapter.rb +75 -207
  81. data/lib/active_record/connection_adapters/postgresql/column.rb +30 -0
  82. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +182 -0
  83. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
  84. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +53 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
  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 +23 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
  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 +41 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +113 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +26 -0
  104. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
  107. data/lib/active_record/connection_adapters/postgresql/quoting.rb +205 -0
  108. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
  109. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -0
  110. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +222 -0
  111. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
  112. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +776 -0
  113. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +36 -0
  114. data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
  115. data/lib/active_record/connection_adapters/postgresql_adapter.rb +695 -1052
  116. data/lib/active_record/connection_adapters/schema_cache.rb +115 -24
  117. data/lib/active_record/connection_adapters/sql_type_metadata.rb +37 -0
  118. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +118 -0
  119. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
  120. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +103 -0
  121. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +17 -0
  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 +528 -26
  126. data/lib/active_record/connection_adapters/statement_pool.rb +34 -13
  127. data/lib/active_record/connection_handling.rb +267 -0
  128. data/lib/active_record/core.rb +599 -0
  129. data/lib/active_record/counter_cache.rb +177 -103
  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 +107 -64
  136. data/lib/active_record/enum.rb +274 -0
  137. data/lib/active_record/errors.rb +254 -61
  138. data/lib/active_record/explain.rb +35 -70
  139. data/lib/active_record/explain_registry.rb +32 -0
  140. data/lib/active_record/explain_subscriber.rb +18 -8
  141. data/lib/active_record/fixture_set/file.rb +82 -0
  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 +291 -475
  147. data/lib/active_record/gem_version.rb +17 -0
  148. data/lib/active_record/inheritance.rb +219 -100
  149. data/lib/active_record/insert_all.rb +179 -0
  150. data/lib/active_record/integration.rb +175 -17
  151. data/lib/active_record/internal_metadata.rb +53 -0
  152. data/lib/active_record/legacy_yaml_adapter.rb +48 -0
  153. data/lib/active_record/locale/en.yml +9 -1
  154. data/lib/active_record/locking/optimistic.rb +106 -92
  155. data/lib/active_record/locking/pessimistic.rb +23 -11
  156. data/lib/active_record/log_subscriber.rb +80 -30
  157. data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
  158. data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
  159. data/lib/active_record/middleware/database_selector.rb +75 -0
  160. data/lib/active_record/migration/command_recorder.rb +235 -56
  161. data/lib/active_record/migration/compatibility.rb +244 -0
  162. data/lib/active_record/migration/join_table.rb +17 -0
  163. data/lib/active_record/migration.rb +917 -301
  164. data/lib/active_record/model_schema.rb +351 -175
  165. data/lib/active_record/nested_attributes.rb +366 -235
  166. data/lib/active_record/no_touching.rb +65 -0
  167. data/lib/active_record/null_relation.rb +68 -0
  168. data/lib/active_record/persistence.rb +761 -166
  169. data/lib/active_record/query_cache.rb +22 -44
  170. data/lib/active_record/querying.rb +55 -31
  171. data/lib/active_record/railtie.rb +185 -47
  172. data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
  173. data/lib/active_record/railties/console_sandbox.rb +5 -4
  174. data/lib/active_record/railties/controller_runtime.rb +35 -33
  175. data/lib/active_record/railties/databases.rake +366 -463
  176. data/lib/active_record/readonly_attributes.rb +4 -6
  177. data/lib/active_record/reflection.rb +736 -228
  178. data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
  179. data/lib/active_record/relation/batches.rb +252 -52
  180. data/lib/active_record/relation/calculations.rb +340 -270
  181. data/lib/active_record/relation/delegation.rb +117 -36
  182. data/lib/active_record/relation/finder_methods.rb +439 -286
  183. data/lib/active_record/relation/from_clause.rb +26 -0
  184. data/lib/active_record/relation/merger.rb +184 -0
  185. data/lib/active_record/relation/predicate_builder/array_handler.rb +49 -0
  186. data/lib/active_record/relation/predicate_builder/association_query_value.rb +43 -0
  187. data/lib/active_record/relation/predicate_builder/base_handler.rb +18 -0
  188. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +19 -0
  189. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +53 -0
  190. data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -0
  191. data/lib/active_record/relation/predicate_builder/relation_handler.rb +19 -0
  192. data/lib/active_record/relation/predicate_builder.rb +131 -39
  193. data/lib/active_record/relation/query_attribute.rb +50 -0
  194. data/lib/active_record/relation/query_methods.rb +1163 -221
  195. data/lib/active_record/relation/record_fetch_warning.rb +51 -0
  196. data/lib/active_record/relation/spawn_methods.rb +49 -120
  197. data/lib/active_record/relation/where_clause.rb +190 -0
  198. data/lib/active_record/relation/where_clause_factory.rb +33 -0
  199. data/lib/active_record/relation.rb +671 -349
  200. data/lib/active_record/result.rb +149 -15
  201. data/lib/active_record/runtime_registry.rb +24 -0
  202. data/lib/active_record/sanitization.rb +153 -133
  203. data/lib/active_record/schema.rb +22 -19
  204. data/lib/active_record/schema_dumper.rb +178 -112
  205. data/lib/active_record/schema_migration.rb +60 -0
  206. data/lib/active_record/scoping/default.rb +107 -98
  207. data/lib/active_record/scoping/named.rb +130 -115
  208. data/lib/active_record/scoping.rb +77 -123
  209. data/lib/active_record/secure_token.rb +40 -0
  210. data/lib/active_record/serialization.rb +10 -6
  211. data/lib/active_record/statement_cache.rb +148 -0
  212. data/lib/active_record/store.rb +256 -16
  213. data/lib/active_record/suppressor.rb +61 -0
  214. data/lib/active_record/table_metadata.rb +75 -0
  215. data/lib/active_record/tasks/database_tasks.rb +506 -0
  216. data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
  217. data/lib/active_record/tasks/postgresql_database_tasks.rb +141 -0
  218. data/lib/active_record/tasks/sqlite_database_tasks.rb +77 -0
  219. data/lib/active_record/test_databases.rb +23 -0
  220. data/lib/active_record/test_fixtures.rb +224 -0
  221. data/lib/active_record/timestamp.rb +93 -39
  222. data/lib/active_record/touch_later.rb +66 -0
  223. data/lib/active_record/transactions.rb +260 -129
  224. data/lib/active_record/translation.rb +3 -1
  225. data/lib/active_record/type/adapter_specific_registry.rb +129 -0
  226. data/lib/active_record/type/date.rb +9 -0
  227. data/lib/active_record/type/date_time.rb +9 -0
  228. data/lib/active_record/type/decimal_without_scale.rb +15 -0
  229. data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
  230. data/lib/active_record/type/internal/timezone.rb +17 -0
  231. data/lib/active_record/type/json.rb +30 -0
  232. data/lib/active_record/type/serialized.rb +71 -0
  233. data/lib/active_record/type/text.rb +11 -0
  234. data/lib/active_record/type/time.rb +21 -0
  235. data/lib/active_record/type/type_map.rb +62 -0
  236. data/lib/active_record/type/unsigned_integer.rb +17 -0
  237. data/lib/active_record/type.rb +78 -0
  238. data/lib/active_record/type_caster/connection.rb +34 -0
  239. data/lib/active_record/type_caster/map.rb +20 -0
  240. data/lib/active_record/type_caster.rb +9 -0
  241. data/lib/active_record/validations/absence.rb +25 -0
  242. data/lib/active_record/validations/associated.rb +35 -18
  243. data/lib/active_record/validations/length.rb +26 -0
  244. data/lib/active_record/validations/presence.rb +68 -0
  245. data/lib/active_record/validations/uniqueness.rb +123 -77
  246. data/lib/active_record/validations.rb +54 -43
  247. data/lib/active_record/version.rb +7 -7
  248. data/lib/active_record.rb +97 -49
  249. data/lib/arel/alias_predication.rb +9 -0
  250. data/lib/arel/attributes/attribute.rb +37 -0
  251. data/lib/arel/attributes.rb +22 -0
  252. data/lib/arel/collectors/bind.rb +24 -0
  253. data/lib/arel/collectors/composite.rb +31 -0
  254. data/lib/arel/collectors/plain_string.rb +20 -0
  255. data/lib/arel/collectors/sql_string.rb +20 -0
  256. data/lib/arel/collectors/substitute_binds.rb +28 -0
  257. data/lib/arel/crud.rb +42 -0
  258. data/lib/arel/delete_manager.rb +18 -0
  259. data/lib/arel/errors.rb +9 -0
  260. data/lib/arel/expressions.rb +29 -0
  261. data/lib/arel/factory_methods.rb +49 -0
  262. data/lib/arel/insert_manager.rb +49 -0
  263. data/lib/arel/math.rb +45 -0
  264. data/lib/arel/nodes/and.rb +32 -0
  265. data/lib/arel/nodes/ascending.rb +23 -0
  266. data/lib/arel/nodes/binary.rb +52 -0
  267. data/lib/arel/nodes/bind_param.rb +36 -0
  268. data/lib/arel/nodes/case.rb +55 -0
  269. data/lib/arel/nodes/casted.rb +50 -0
  270. data/lib/arel/nodes/comment.rb +29 -0
  271. data/lib/arel/nodes/count.rb +12 -0
  272. data/lib/arel/nodes/delete_statement.rb +45 -0
  273. data/lib/arel/nodes/descending.rb +23 -0
  274. data/lib/arel/nodes/equality.rb +18 -0
  275. data/lib/arel/nodes/extract.rb +24 -0
  276. data/lib/arel/nodes/false.rb +16 -0
  277. data/lib/arel/nodes/full_outer_join.rb +8 -0
  278. data/lib/arel/nodes/function.rb +44 -0
  279. data/lib/arel/nodes/grouping.rb +8 -0
  280. data/lib/arel/nodes/in.rb +8 -0
  281. data/lib/arel/nodes/infix_operation.rb +80 -0
  282. data/lib/arel/nodes/inner_join.rb +8 -0
  283. data/lib/arel/nodes/insert_statement.rb +37 -0
  284. data/lib/arel/nodes/join_source.rb +20 -0
  285. data/lib/arel/nodes/matches.rb +18 -0
  286. data/lib/arel/nodes/named_function.rb +23 -0
  287. data/lib/arel/nodes/node.rb +50 -0
  288. data/lib/arel/nodes/node_expression.rb +13 -0
  289. data/lib/arel/nodes/outer_join.rb +8 -0
  290. data/lib/arel/nodes/over.rb +15 -0
  291. data/lib/arel/nodes/regexp.rb +16 -0
  292. data/lib/arel/nodes/right_outer_join.rb +8 -0
  293. data/lib/arel/nodes/select_core.rb +67 -0
  294. data/lib/arel/nodes/select_statement.rb +41 -0
  295. data/lib/arel/nodes/sql_literal.rb +16 -0
  296. data/lib/arel/nodes/string_join.rb +11 -0
  297. data/lib/arel/nodes/table_alias.rb +27 -0
  298. data/lib/arel/nodes/terminal.rb +16 -0
  299. data/lib/arel/nodes/true.rb +16 -0
  300. data/lib/arel/nodes/unary.rb +45 -0
  301. data/lib/arel/nodes/unary_operation.rb +20 -0
  302. data/lib/arel/nodes/unqualified_column.rb +22 -0
  303. data/lib/arel/nodes/update_statement.rb +41 -0
  304. data/lib/arel/nodes/values_list.rb +9 -0
  305. data/lib/arel/nodes/window.rb +126 -0
  306. data/lib/arel/nodes/with.rb +11 -0
  307. data/lib/arel/nodes.rb +68 -0
  308. data/lib/arel/order_predications.rb +13 -0
  309. data/lib/arel/predications.rb +257 -0
  310. data/lib/arel/select_manager.rb +271 -0
  311. data/lib/arel/table.rb +110 -0
  312. data/lib/arel/tree_manager.rb +72 -0
  313. data/lib/arel/update_manager.rb +34 -0
  314. data/lib/arel/visitors/depth_first.rb +204 -0
  315. data/lib/arel/visitors/dot.rb +297 -0
  316. data/lib/arel/visitors/ibm_db.rb +34 -0
  317. data/lib/arel/visitors/informix.rb +62 -0
  318. data/lib/arel/visitors/mssql.rb +157 -0
  319. data/lib/arel/visitors/mysql.rb +83 -0
  320. data/lib/arel/visitors/oracle.rb +159 -0
  321. data/lib/arel/visitors/oracle12.rb +66 -0
  322. data/lib/arel/visitors/postgresql.rb +110 -0
  323. data/lib/arel/visitors/sqlite.rb +39 -0
  324. data/lib/arel/visitors/to_sql.rb +889 -0
  325. data/lib/arel/visitors/visitor.rb +46 -0
  326. data/lib/arel/visitors/where_sql.rb +23 -0
  327. data/lib/arel/visitors.rb +20 -0
  328. data/lib/arel/window_predications.rb +9 -0
  329. data/lib/arel.rb +51 -0
  330. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  331. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
  332. data/lib/rails/generators/active_record/migration/migration_generator.rb +59 -9
  333. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
  334. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +48 -0
  335. data/lib/rails/generators/active_record/migration.rb +41 -8
  336. data/lib/rails/generators/active_record/model/model_generator.rb +24 -22
  337. data/lib/rails/generators/active_record/model/templates/model.rb.tt +22 -0
  338. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +1 -1
  339. data/lib/rails/generators/active_record.rb +10 -16
  340. metadata +285 -149
  341. data/examples/associations.png +0 -0
  342. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +0 -63
  343. data/lib/active_record/associations/join_helper.rb +0 -55
  344. data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
  345. data/lib/active_record/associations/preloader/collection_association.rb +0 -24
  346. data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +0 -60
  347. data/lib/active_record/associations/preloader/has_many.rb +0 -17
  348. data/lib/active_record/associations/preloader/has_many_through.rb +0 -15
  349. data/lib/active_record/associations/preloader/has_one.rb +0 -23
  350. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  351. data/lib/active_record/associations/preloader/singular_association.rb +0 -21
  352. data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +0 -32
  353. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -188
  354. data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -426
  355. data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -579
  356. data/lib/active_record/dynamic_finder_match.rb +0 -68
  357. data/lib/active_record/dynamic_scope_match.rb +0 -23
  358. data/lib/active_record/fixtures/file.rb +0 -65
  359. data/lib/active_record/identity_map.rb +0 -162
  360. data/lib/active_record/observer.rb +0 -121
  361. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  362. data/lib/active_record/serializers/xml_serializer.rb +0 -203
  363. data/lib/active_record/session_store.rb +0 -358
  364. data/lib/active_record/test_case.rb +0 -73
  365. data/lib/rails/generators/active_record/migration/templates/migration.rb +0 -34
  366. data/lib/rails/generators/active_record/model/templates/migration.rb +0 -15
  367. data/lib/rails/generators/active_record/model/templates/model.rb +0 -12
  368. data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
  369. data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
  370. data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
  371. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -12
@@ -1,342 +1,376 @@
1
- require 'active_support/core_ext/object/inclusion'
2
- require 'active_record'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+
5
+ databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
3
6
 
4
7
  db_namespace = namespace :db do
5
- task :load_config => :rails_env do
6
- ActiveRecord::Base.configurations = Rails.application.config.database_configuration
7
- ActiveRecord::Migrator.migrations_paths = Rails.application.paths['db/migrate'].to_a
8
+ desc "Set the environment value for the database"
9
+ task "environment:set" => :load_config do
10
+ ActiveRecord::InternalMetadata.create_table
11
+ ActiveRecord::InternalMetadata[:environment] = ActiveRecord::Base.connection.migration_context.current_environment
12
+ end
8
13
 
9
- if defined?(ENGINE_PATH) && engine = Rails::Engine.find(ENGINE_PATH)
10
- if engine.paths['db/migrate'].existent
11
- ActiveRecord::Migrator.migrations_paths += engine.paths['db/migrate'].to_a
12
- end
13
- end
14
+ task check_protected_environments: :load_config do
15
+ ActiveRecord::Tasks::DatabaseTasks.check_protected_environments!
16
+ end
17
+
18
+ task load_config: :environment do
19
+ ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {}
20
+ ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
14
21
  end
15
22
 
16
23
  namespace :create do
17
- # desc 'Create all the local databases defined in config/database.yml'
18
- task :all => :load_config do
19
- ActiveRecord::Base.configurations.each_value do |config|
20
- # Skip entries that don't have a database key, such as the first entry here:
21
- #
22
- # defaults: &defaults
23
- # adapter: mysql
24
- # username: root
25
- # password:
26
- # host: localhost
27
- #
28
- # development:
29
- # database: blog_development
30
- # *defaults
31
- next unless config['database']
32
- # Only connect to local databases
33
- local_database?(config) { create_database(config) }
24
+ task all: :load_config do
25
+ ActiveRecord::Tasks::DatabaseTasks.create_all
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)
34
33
  end
35
34
  end
36
35
  end
37
36
 
38
- desc 'Create the database from config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)'
39
- task :create => :load_config do
40
- configs_for_environment.each { |config| create_database(config) }
41
- ActiveRecord::Base.establish_connection(configs_for_environment.first)
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
39
+ ActiveRecord::Tasks::DatabaseTasks.create_current
42
40
  end
43
41
 
44
- def mysql_creation_options(config)
45
- @charset = ENV['CHARSET'] || 'utf8'
46
- @collation = ENV['COLLATION'] || 'utf8_unicode_ci'
47
- {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
48
- end
42
+ namespace :drop do
43
+ task all: [:load_config, :check_protected_environments] do
44
+ ActiveRecord::Tasks::DatabaseTasks.drop_all
45
+ end
49
46
 
50
- def create_database(config)
51
- begin
52
- if config['adapter'] =~ /sqlite/
53
- if File.exist?(config['database'])
54
- $stderr.puts "#{config['database']} already exists"
55
- else
56
- begin
57
- # Create the SQLite database
58
- ActiveRecord::Base.establish_connection(config)
59
- ActiveRecord::Base.connection
60
- rescue Exception => e
61
- $stderr.puts e, *(e.backtrace)
62
- $stderr.puts "Couldn't create database for #{config.inspect}"
63
- end
64
- end
65
- return # Skip the else clause of begin/rescue
66
- else
67
- ActiveRecord::Base.establish_connection(config)
68
- ActiveRecord::Base.connection
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)
69
52
  end
70
- rescue
71
- case config['adapter']
72
- when /mysql/
73
- if config['adapter'] =~ /jdbc/
74
- #FIXME After Jdbcmysql gives this class
75
- require 'active_record/railties/jdbcmysql_error'
76
- error_class = ArJdbcMySQL::Error
77
- else
78
- error_class = config['adapter'] =~ /mysql2/ ? Mysql2::Error : Mysql::Error
79
- end
80
- access_denied_error = 1045
81
- begin
82
- ActiveRecord::Base.establish_connection(config.merge('database' => nil))
83
- ActiveRecord::Base.connection.create_database(config['database'], mysql_creation_options(config))
84
- ActiveRecord::Base.establish_connection(config)
85
- rescue error_class => sqlerr
86
- if sqlerr.errno == access_denied_error
87
- print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
88
- root_password = $stdin.gets.strip
89
- grant_statement = "GRANT ALL PRIVILEGES ON #{config['database']}.* " \
90
- "TO '#{config['username']}'@'localhost' " \
91
- "IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;"
92
- ActiveRecord::Base.establish_connection(config.merge(
93
- 'database' => nil, 'username' => 'root', 'password' => root_password))
94
- ActiveRecord::Base.connection.create_database(config['database'], mysql_creation_options(config))
95
- ActiveRecord::Base.connection.execute grant_statement
96
- ActiveRecord::Base.establish_connection(config)
97
- else
98
- $stderr.puts sqlerr.error
99
- $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation}"
100
- $stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
101
- end
102
- end
103
- when /postgresql/
104
- @encoding = config['encoding'] || ENV['CHARSET'] || 'utf8'
105
- begin
106
- ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
107
- ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding))
108
- ActiveRecord::Base.establish_connection(config)
109
- rescue Exception => e
110
- $stderr.puts e, *(e.backtrace)
111
- $stderr.puts "Couldn't create database for #{config.inspect}"
112
- end
113
- end
114
- else
115
- # Bug with 1.9.2 Calling return within begin still executes else
116
- $stderr.puts "#{config['database']} already exists" unless config['adapter'] =~ /sqlite/
117
53
  end
118
54
  end
119
55
 
120
- namespace :drop do
121
- # desc 'Drops all the local databases defined in config/database.yml'
122
- task :all => :load_config do
123
- ActiveRecord::Base.configurations.each_value do |config|
124
- # Skip entries that don't have a database key
125
- next unless config['database']
126
- begin
127
- # Only connect to local databases
128
- local_database?(config) { drop_database(config) }
129
- rescue Exception => e
130
- $stderr.puts "Couldn't drop #{config['database']} : #{e.inspect}"
131
- end
132
- end
133
- end
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
58
+ db_namespace["drop:_unsafe"].invoke
134
59
  end
135
60
 
136
- desc 'Drops the database for the current Rails.env (use db:drop:all to drop all databases)'
137
- task :drop => :load_config do
138
- configs_for_environment.each { |config| drop_database_and_rescue(config) }
61
+ task "drop:_unsafe" => [:load_config] do
62
+ ActiveRecord::Tasks::DatabaseTasks.drop_current
139
63
  end
140
64
 
141
- def local_database?(config, &block)
142
- if config['host'].in?(['127.0.0.1', 'localhost']) || config['host'].blank?
143
- yield
144
- else
145
- $stderr.puts "This task only modifies local databases. #{config['database']} is on a remote host."
65
+ namespace :purge do
66
+ task all: [:load_config, :check_protected_environments] do
67
+ ActiveRecord::Tasks::DatabaseTasks.purge_all
146
68
  end
147
69
  end
148
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
+
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."
77
+ task purge: [:load_config, :check_protected_environments] do
78
+ ActiveRecord::Tasks::DatabaseTasks.purge_current
79
+ end
149
80
 
150
- desc "Migrate the database (options: VERSION=x, VERBOSE=false)."
151
- task :migrate => [:environment, :load_config] do
152
- ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
153
- ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil) do |migration|
154
- ENV["SCOPE"].blank? || (ENV["SCOPE"] == migration.scope)
81
+ desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
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
155
86
  end
156
- db_namespace['_dump'].invoke
87
+ db_namespace["_dump"].invoke
157
88
  end
158
89
 
90
+ # IMPORTANT: This task won't dump the schema if ActiveRecord::Base.dump_schema_after_migration is set to false
159
91
  task :_dump do
160
- case ActiveRecord::Base.schema_format
161
- when :ruby then db_namespace["schema:dump"].invoke
162
- when :sql then db_namespace["structure:dump"].invoke
163
- else
164
- raise "unknown schema format #{ActiveRecord::Base.schema_format}"
92
+ if ActiveRecord::Base.dump_schema_after_migration
93
+ case ActiveRecord::Base.schema_format
94
+ when :ruby then db_namespace["schema:dump"].invoke
95
+ when :sql then db_namespace["structure:dump"].invoke
96
+ else
97
+ raise "unknown schema format #{ActiveRecord::Base.schema_format}"
98
+ end
165
99
  end
166
100
  # Allow this task to be called as many times as required. An example is the
167
101
  # migrate:redo task, which calls other two internally that depend on this one.
168
- db_namespace['_dump'].reenable
102
+ db_namespace["_dump"].reenable
169
103
  end
170
104
 
171
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
+
172
115
  # desc 'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
173
- task :redo => [:environment, :load_config] do
174
- if ENV['VERSION']
175
- db_namespace['migrate:down'].invoke
176
- db_namespace['migrate:up'].invoke
116
+ task redo: :load_config do
117
+ raise "Empty VERSION provided" if ENV["VERSION"] && ENV["VERSION"].empty?
118
+
119
+ if ENV["VERSION"]
120
+ db_namespace["migrate:down"].invoke
121
+ db_namespace["migrate:up"].invoke
177
122
  else
178
- db_namespace['rollback'].invoke
179
- db_namespace['migrate'].invoke
123
+ db_namespace["rollback"].invoke
124
+ db_namespace["migrate"].invoke
180
125
  end
181
126
  end
182
127
 
183
128
  # desc 'Resets your database using your migrations for the current environment'
184
- task :reset => ['db:drop', 'db:create', 'db:migrate']
129
+ task reset: ["db:drop", "db:create", "db:migrate"]
185
130
 
186
131
  # desc 'Runs the "up" for a given migration VERSION.'
187
- task :up => [:environment, :load_config] do
188
- version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
189
- raise 'VERSION is required' unless version
190
- ActiveRecord::Migrator.run(:up, ActiveRecord::Migrator.migrations_paths, version)
191
- db_namespace['_dump'].invoke
132
+ task up: :load_config do
133
+ ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:migrate:up")
134
+
135
+ raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
136
+
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
192
144
  end
193
145
 
194
- # desc 'Runs the "down" for a given migration VERSION.'
195
- task :down => [:environment, :load_config] do
196
- version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
197
- raise 'VERSION is required' unless version
198
- ActiveRecord::Migrator.run(:down, ActiveRecord::Migrator.migrations_paths, version)
199
- db_namespace['_dump'].invoke
200
- end
201
-
202
- desc 'Display status of migrations'
203
- task :status => [:environment, :load_config] do
204
- config = ActiveRecord::Base.configurations[Rails.env || 'development']
205
- ActiveRecord::Base.establish_connection(config)
206
- unless ActiveRecord::Base.connection.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
207
- puts 'Schema migrations table does not exist yet.'
208
- next # means "return" for rake task
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
209
162
  end
210
- db_list = ActiveRecord::Base.connection.select_values("SELECT version FROM #{ActiveRecord::Migrator.schema_migrations_table_name}")
211
- file_list = []
212
- ActiveRecord::Migrator.migrations_paths.each do |path|
213
- Dir.foreach(path) do |file|
214
- # only files matching "20091231235959_some_name.rb" pattern
215
- if match_data = /^(\d{14})_(.+)\.rb$/.match(file)
216
- status = db_list.delete(match_data[1]) ? 'up' : 'down'
217
- file_list << [status, match_data[1], match_data[2].humanize]
218
- end
163
+ end
164
+
165
+ # desc 'Runs the "down" for a given migration VERSION.'
166
+ task down: :load_config do
167
+ ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:migrate:down")
168
+
169
+ raise "VERSION is required - To go down one migration, use db:rollback" if !ENV["VERSION"] || ENV["VERSION"].empty?
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
219
195
  end
220
196
  end
221
- db_list.map! do |version|
222
- ['up', version, '********** NO FILE **********']
197
+ end
198
+
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
223
204
  end
224
- # output
225
- puts "\ndatabase: #{config['database']}\n\n"
226
- puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
227
- puts "-" * 50
228
- (db_list + file_list).sort_by {|migration| migration[1]}.each do |migration|
229
- puts "#{migration[0].center(8)} #{migration[1].ljust(14)} #{migration[2]}"
205
+ end
206
+
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
230
215
  end
231
- puts
232
216
  end
233
217
  end
234
218
 
235
- desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'
236
- task :rollback => [:environment, :load_config] do
237
- step = ENV['STEP'] ? ENV['STEP'].to_i : 1
238
- ActiveRecord::Migrator.rollback(ActiveRecord::Migrator.migrations_paths, step)
239
- 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
240
224
  end
241
225
 
242
226
  # desc 'Pushes the schema to the next version (specify steps w/ STEP=n).'
243
- task :forward => [:environment, :load_config] do
244
- step = ENV['STEP'] ? ENV['STEP'].to_i : 1
245
- ActiveRecord::Migrator.forward(ActiveRecord::Migrator.migrations_paths, step)
246
- 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
247
231
  end
248
232
 
249
233
  # desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
250
- task :reset => :environment do
251
- db_namespace["drop"].invoke
252
- db_namespace["setup"].invoke
253
- end
234
+ task reset: [ "db:drop", "db:setup" ]
254
235
 
255
236
  # desc "Retrieves the charset for the current environment's database"
256
- task :charset => :environment do
257
- config = ActiveRecord::Base.configurations[Rails.env || 'development']
258
- case config['adapter']
259
- when /mysql/
260
- ActiveRecord::Base.establish_connection(config)
261
- puts ActiveRecord::Base.connection.charset
262
- when /postgresql/
263
- ActiveRecord::Base.establish_connection(config)
264
- puts ActiveRecord::Base.connection.encoding
265
- when /sqlite/
266
- ActiveRecord::Base.establish_connection(config)
267
- puts ActiveRecord::Base.connection.encoding
268
- else
269
- $stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
270
- end
237
+ task charset: :load_config do
238
+ puts ActiveRecord::Tasks::DatabaseTasks.charset_current
271
239
  end
272
240
 
273
241
  # desc "Retrieves the collation for the current environment's database"
274
- task :collation => :environment do
275
- config = ActiveRecord::Base.configurations[Rails.env || 'development']
276
- case config['adapter']
277
- when /mysql/
278
- ActiveRecord::Base.establish_connection(config)
279
- puts ActiveRecord::Base.connection.collation
280
- else
281
- $stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
282
- 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."
283
246
  end
284
247
 
285
- desc 'Retrieves the current schema version number'
286
- task :version => :environment do
287
- 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}"
288
251
  end
289
252
 
290
253
  # desc "Raises an error if there are pending migrations"
291
- task :abort_if_pending_migrations => :environment do
292
- pending_migrations = ActiveRecord::Migrator.new(:up, ActiveRecord::Migrator.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
293
260
 
294
261
  if pending_migrations.any?
295
- puts "You have #{pending_migrations.size} pending migrations:"
262
+ puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
296
263
  pending_migrations.each do |pending_migration|
297
- puts ' %4d %s' % [pending_migration.version, pending_migration.name]
264
+ puts " %4d %s" % [pending_migration.version, pending_migration.name]
265
+ end
266
+ abort %{Run `rails db:migrate` to update your database then try again.}
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
290
+ end
291
+
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)
298
306
  end
299
- abort %{Run `rake db:migrate` to update your database then try again.}
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
300
319
  end
320
+
321
+ ActiveRecord::Base.establish_connection
322
+ ActiveRecord::Tasks::DatabaseTasks.load_seed if seed
301
323
  end
302
324
 
303
- desc 'Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)'
304
- task :setup => ['db:schema:load_if_ruby', 'db:structure:load_if_sql', :seed]
325
+ desc "Loads the seed data from db/seeds.rb"
326
+ task seed: :load_config do
327
+ db_namespace["abort_if_pending_migrations"].invoke
328
+ ActiveRecord::Tasks::DatabaseTasks.load_seed
329
+ end
305
330
 
306
- desc 'Load the seed data from db/seeds.rb'
307
- task :seed do
308
- db_namespace['abort_if_pending_migrations'].invoke
309
- Rails.application.load_seed
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]
310
334
  end
311
335
 
312
336
  namespace :fixtures do
313
- desc "Load 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."
314
- task :load => :environment do
315
- require 'active_record/fixtures'
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."
338
+ task load: :load_config do
339
+ require "active_record/fixtures"
316
340
 
317
- ActiveRecord::Base.establish_connection(Rails.env)
318
- base_dir = File.join [Rails.root, ENV['FIXTURES_PATH'] || %w{test fixtures}].flatten
319
- fixtures_dir = File.join [base_dir, ENV['FIXTURES_DIR']].compact
341
+ base_dir = ActiveRecord::Tasks::DatabaseTasks.fixtures_path
320
342
 
321
- (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir["#{fixtures_dir}/**/*.{yml,csv}"].map {|f| f[(fixtures_dir.size + 1)..-5] }).each do |fixture_file|
322
- ActiveRecord::Fixtures.create_fixtures(fixtures_dir, fixture_file)
343
+ fixtures_dir = if ENV["FIXTURES_DIR"]
344
+ File.join base_dir, ENV["FIXTURES_DIR"]
345
+ else
346
+ base_dir
323
347
  end
348
+
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
355
+
356
+ ActiveRecord::FixtureSet.create_fixtures(fixtures_dir, fixture_files)
324
357
  end
325
358
 
326
359
  # desc "Search for a fixture given a LABEL or ID. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
327
- task :identify => :environment do
328
- require 'active_record/fixtures'
360
+ task identify: :load_config do
361
+ require "active_record/fixtures"
362
+
363
+ label, id = ENV["LABEL"], ENV["ID"]
364
+ raise "LABEL or ID required" if label.blank? && id.blank?
329
365
 
330
- label, id = ENV['LABEL'], ENV['ID']
331
- raise 'LABEL or ID required' if label.blank? && id.blank?
366
+ puts %Q(The fixture ID for "#{label}" is #{ActiveRecord::FixtureSet.identify(label)}.) if label
332
367
 
333
- puts %Q(The fixture ID for "#{label}" is #{ActiveRecord::Fixtures.identify(label)}.) if label
368
+ base_dir = ActiveRecord::Tasks::DatabaseTasks.fixtures_path
334
369
 
335
- base_dir = ENV['FIXTURES_PATH'] ? File.join(Rails.root, ENV['FIXTURES_PATH']) : File.join(Rails.root, 'test', 'fixtures')
336
370
  Dir["#{base_dir}/**/*.yml"].each do |file|
337
- if data = YAML::load(ERB.new(IO.read(file)).result)
338
- data.keys.each do |key|
339
- key_id = ActiveRecord::Fixtures.identify(key)
371
+ if data = YAML.load(ERB.new(IO.read(file)).result)
372
+ data.each_key do |key|
373
+ key_id = ActiveRecord::FixtureSet.identify(key)
340
374
 
341
375
  if key == label || key_id == id.to_i
342
376
  puts "#{file}: #{key} (#{key_id})"
@@ -348,277 +382,146 @@ db_namespace = namespace :db do
348
382
  end
349
383
 
350
384
  namespace :schema do
351
- desc 'Create a db/schema.rb file that can be portably used against any DB supported by AR'
352
- task :dump => [:environment, :load_config] do
353
- require 'active_record/schema_dumper'
354
- filename = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
355
- File.open(filename, "w:utf-8") do |file|
356
- ActiveRecord::Base.establish_connection(Rails.env)
357
- 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)
358
390
  end
359
- db_namespace['schema:dump'].reenable
391
+
392
+ db_namespace["schema:dump"].reenable
360
393
  end
361
394
 
362
- desc 'Load a schema.rb file into the database'
363
- task :load => :environment do
364
- file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
365
- if File.exists?(file)
366
- load(file)
367
- else
368
- abort %{#{file} doesn't exist yet. Run `rake db:migrate` to create it then try again. If you do not intend to use a database, you should instead alter #{Rails.root}/config/application.rb to limit the frameworks that will be loaded}
369
- end
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"])
370
398
  end
371
399
 
372
- task :load_if_ruby => 'db:create' do
400
+ task load_if_ruby: ["db:create", :environment] do
373
401
  db_namespace["schema:load"].invoke if ActiveRecord::Base.schema_format == :ruby
374
402
  end
375
- end
376
403
 
377
- namespace :structure do
378
- desc 'Dump the database structure to db/structure.sql. Specify another file with DB_STRUCTURE=db/my_structure.sql'
379
- task :dump => :environment do
380
- abcs = ActiveRecord::Base.configurations
381
- filename = ENV['DB_STRUCTURE'] || File.join(Rails.root, "db", "structure.sql")
382
- case abcs[Rails.env]['adapter']
383
- when /mysql/, 'oci', 'oracle'
384
- ActiveRecord::Base.establish_connection(abcs[Rails.env])
385
- File.open(filename, "w:utf-8") { |f| f << ActiveRecord::Base.connection.structure_dump }
386
- when /postgresql/
387
- set_psql_env(abcs[Rails.env])
388
- search_path = abcs[Rails.env]['schema_search_path']
389
- unless search_path.blank?
390
- search_path = search_path.split(",").map{|search_path_part| "--schema=#{Shellwords.escape(search_path_part.strip)}" }.join(" ")
404
+ namespace :cache do
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
+ )
391
414
  end
392
- `pg_dump -i -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(abcs[Rails.env]['database'])}`
393
- raise 'Error dumping database' if $?.exitstatus == 1
394
- when /sqlite/
395
- dbfile = abcs[Rails.env]['database']
396
- `sqlite3 #{dbfile} .schema > #{filename}`
397
- when 'sqlserver'
398
- `smoscript -s #{abcs[Rails.env]['host']} -d #{abcs[Rails.env]['database']} -u #{abcs[Rails.env]['username']} -p #{abcs[Rails.env]['password']} -f #{filename} -A -U`
399
- when "firebird"
400
- set_firebird_env(abcs[Rails.env])
401
- db_string = firebird_db_string(abcs[Rails.env])
402
- sh "isql -a #{db_string} > #{filename}"
403
- else
404
- raise "Task not supported by '#{abcs[Rails.env]["adapter"]}'"
405
415
  end
406
416
 
407
- if ActiveRecord::Base.connection.supports_migrations?
408
- File.open(filename, "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
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
409
423
  end
410
- db_namespace['structure:dump'].reenable
411
424
  end
425
+ end
412
426
 
413
- # desc "Recreate the databases from the structure.sql file"
414
- task :load => [:environment, :load_config] do
415
- env = ENV['RAILS_ENV'] || 'test'
416
-
417
- abcs = ActiveRecord::Base.configurations
418
- filename = ENV['DB_STRUCTURE'] || File.join(Rails.root, "db", "structure.sql")
419
- case abcs[env]['adapter']
420
- when /mysql/
421
- ActiveRecord::Base.establish_connection(abcs[env])
422
- ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
423
- IO.read(filename).split("\n\n").each do |table|
424
- ActiveRecord::Base.connection.execute(table)
425
- end
426
- when /postgresql/
427
- set_psql_env(abcs[env])
428
- `psql -f "#{filename}" #{abcs[env]['database']}`
429
- when /sqlite/
430
- dbfile = abcs[env]['database']
431
- `sqlite3 #{dbfile} < "#{filename}"`
432
- when 'sqlserver'
433
- `sqlcmd -S #{abcs[env]['host']} -d #{abcs[env]['database']} -U #{abcs[env]['username']} -P #{abcs[env]['password']} -i #{filename}`
434
- when 'oci', 'oracle'
435
- ActiveRecord::Base.establish_connection(abcs[env])
436
- IO.read(filename).split(";\n\n").each do |ddl|
437
- ActiveRecord::Base.connection.execute(ddl)
438
- end
439
- when 'firebird'
440
- set_firebird_env(abcs[env])
441
- db_string = firebird_db_string(abcs[env])
442
- sh "isql -i #{filename} #{db_string}"
443
- else
444
- raise "Task not supported by '#{abcs[env]['adapter']}'"
427
+ namespace :structure do
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)
445
433
  end
434
+
435
+ db_namespace["structure:dump"].reenable
436
+ end
437
+
438
+ desc "Recreates the databases from the structure.sql file"
439
+ task load: [:load_config, :check_protected_environments] do
440
+ ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:sql, ENV["SCHEMA"])
446
441
  end
447
442
 
448
- task :load_if_sql => 'db:create' do
443
+ task load_if_sql: ["db:create", :environment] do
449
444
  db_namespace["structure:load"].invoke if ActiveRecord::Base.schema_format == :sql
450
445
  end
451
446
  end
452
447
 
453
448
  namespace :test do
454
-
455
449
  # desc "Recreate the test database from the current schema"
456
- task :load => 'db:test:purge' do
450
+ task load: %w(db:test:purge) do
457
451
  case ActiveRecord::Base.schema_format
458
- when :ruby
459
- db_namespace["test:load_schema"].invoke
460
- when :sql
461
- db_namespace["test:load_structure"].invoke
462
- end
463
- end
464
-
465
- # desc "Recreate the test database from an existent structure.sql file"
466
- task :load_structure => 'db:test:purge' do
467
- begin
468
- old_env, ENV['RAILS_ENV'] = ENV['RAILS_ENV'], 'test'
469
- db_namespace["structure:load"].invoke
470
- ensure
471
- ENV['RAILS_ENV'] = old_env
452
+ when :ruby
453
+ db_namespace["test:load_schema"].invoke
454
+ when :sql
455
+ db_namespace["test:load_structure"].invoke
472
456
  end
473
457
  end
474
458
 
475
459
  # desc "Recreate the test database from an existent schema.rb file"
476
- task :load_schema => 'db:test:purge' do
477
- ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
460
+ task load_schema: %w(db:test:purge) do
461
+ should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
478
462
  ActiveRecord::Schema.verbose = false
479
- db_namespace["schema:load"].invoke
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))
470
+ end
480
471
  end
481
472
 
482
- # desc "Recreate the test database from a fresh schema.rb file"
483
- task :clone => %w(db:schema:dump db:test:load_schema)
484
-
485
- # desc "Recreate the test database from a fresh structure.sql file"
486
- task :clone_structure => [ "db:structure:dump", "db:test:load_structure" ]
473
+ # desc "Recreate the test database from an existent structure.sql file"
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")
478
+ end
479
+ end
487
480
 
488
481
  # desc "Empty the test database"
489
- task :purge => :environment do
490
- abcs = ActiveRecord::Base.configurations
491
- case abcs['test']['adapter']
492
- when /mysql/
493
- ActiveRecord::Base.establish_connection(:test)
494
- ActiveRecord::Base.connection.recreate_database(abcs['test']['database'], mysql_creation_options(abcs['test']))
495
- when /postgresql/
496
- ActiveRecord::Base.clear_active_connections!
497
- drop_database(abcs['test'])
498
- create_database(abcs['test'])
499
- when /sqlite/
500
- dbfile = abcs['test']['database']
501
- File.delete(dbfile) if File.exist?(dbfile)
502
- when 'sqlserver'
503
- test = abcs.deep_dup['test']
504
- test_database = test['database']
505
- test['database'] = 'master'
506
- ActiveRecord::Base.establish_connection(test)
507
- ActiveRecord::Base.connection.recreate_database!(test_database)
508
- when "oci", "oracle"
509
- ActiveRecord::Base.establish_connection(:test)
510
- ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
511
- ActiveRecord::Base.connection.execute(ddl)
512
- end
513
- when 'firebird'
514
- ActiveRecord::Base.establish_connection(:test)
515
- ActiveRecord::Base.connection.recreate_database!
516
- else
517
- raise "Task not supported by '#{abcs['test']['adapter']}'"
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)
518
485
  end
519
486
  end
520
487
 
521
- # desc 'Check for pending migrations and load the test schema'
522
- task :prepare => 'db:abort_if_pending_migrations' do
488
+ # desc 'Load the test schema'
489
+ task prepare: :load_config do
523
490
  unless ActiveRecord::Base.configurations.blank?
524
- db_namespace[{ :sql => 'test:clone_structure', :ruby => 'test:load' }[ActiveRecord::Base.schema_format]].invoke
491
+ db_namespace["test:load"].invoke
525
492
  end
526
493
  end
527
494
  end
528
-
529
- namespace :sessions do
530
- # desc "Creates a sessions migration for use with ActiveRecord::SessionStore"
531
- task :create => :environment do
532
- raise 'Task unavailable to this database (no migration support)' unless ActiveRecord::Base.connection.supports_migrations?
533
- Rails.application.load_generators
534
- require 'rails/generators/rails/session_migration/session_migration_generator'
535
- Rails::Generators::SessionMigrationGenerator.start [ ENV['MIGRATION'] || 'add_sessions_table' ]
536
- end
537
-
538
- # desc "Clear the sessions table"
539
- task :clear => :environment do
540
- ActiveRecord::Base.connection.execute "DELETE FROM #{session_table_name}"
541
- end
542
- end
543
495
  end
544
496
 
545
497
  namespace :railties do
546
498
  namespace :install do
547
- # desc "Copies missing migrations from Railties (e.g. plugins, engines). You can specify Railties to use with FROM=railtie1,railtie2"
548
- task :migrations => :'db:load_config' do
549
- to_load = ENV['FROM'].blank? ? :all : ENV['FROM'].split(",").map {|n| n.strip }
550
- railties = ActiveSupport::OrderedHash.new
551
- Rails.application.railties.all do |railtie|
499
+ # desc "Copies missing migrations from Railties (e.g. engines). You can specify Railties to use with FROM=railtie1,railtie2"
500
+ task migrations: :'db:load_config' do
501
+ to_load = ENV["FROM"].blank? ? :all : ENV["FROM"].split(",").map(&:strip)
502
+ railties = {}
503
+ Rails.application.migration_railties.each do |railtie|
552
504
  next unless to_load == :all || to_load.include?(railtie.railtie_name)
553
505
 
554
- if railtie.respond_to?(:paths) && (path = railtie.paths['db/migrate'].first)
506
+ if railtie.respond_to?(:paths) && (path = railtie.paths["db/migrate"].first)
555
507
  railties[railtie.railtie_name] = path
556
508
  end
509
+
510
+ unless ENV["MIGRATIONS_PATH"].blank?
511
+ railties[railtie.railtie_name] = railtie.root + ENV["MIGRATIONS_PATH"]
512
+ end
557
513
  end
558
514
 
559
515
  on_skip = Proc.new do |name, migration|
560
516
  puts "NOTE: Migration #{migration.basename} from #{name} has been skipped. Migration with the same name already exists."
561
517
  end
562
518
 
563
- on_copy = Proc.new do |name, migration, old_path|
519
+ on_copy = Proc.new do |name, migration|
564
520
  puts "Copied migration #{migration.basename} from #{name}"
565
521
  end
566
522
 
567
- ActiveRecord::Migration.copy( ActiveRecord::Migrator.migrations_paths.first, railties,
568
- :on_skip => on_skip, :on_copy => on_copy)
523
+ ActiveRecord::Migration.copy(ActiveRecord::Tasks::DatabaseTasks.migrations_paths.first, railties,
524
+ on_skip: on_skip, on_copy: on_copy)
569
525
  end
570
526
  end
571
527
  end
572
-
573
- task 'test:prepare' => 'db:test:prepare'
574
-
575
- def drop_database(config)
576
- case config['adapter']
577
- when /mysql/
578
- ActiveRecord::Base.establish_connection(config)
579
- ActiveRecord::Base.connection.drop_database config['database']
580
- when /sqlite/
581
- require 'pathname'
582
- path = Pathname.new(config['database'])
583
- file = path.absolute? ? path.to_s : File.join(Rails.root, path)
584
-
585
- FileUtils.rm(file)
586
- when /postgresql/
587
- ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
588
- ActiveRecord::Base.connection.drop_database config['database']
589
- end
590
- end
591
-
592
- def drop_database_and_rescue(config)
593
- begin
594
- drop_database(config)
595
- rescue Exception => e
596
- $stderr.puts "Couldn't drop #{config['database']} : #{e.inspect}"
597
- end
598
- end
599
-
600
- def configs_for_environment
601
- environments = [Rails.env]
602
- environments << 'test' if Rails.env.development?
603
- ActiveRecord::Base.configurations.values_at(*environments).compact.reject { |config| config['database'].blank? }
604
- end
605
-
606
- def session_table_name
607
- ActiveRecord::SessionStore::Session.table_name
608
- end
609
-
610
- def set_firebird_env(config)
611
- ENV['ISC_USER'] = config['username'].to_s if config['username']
612
- ENV['ISC_PASSWORD'] = config['password'].to_s if config['password']
613
- end
614
-
615
- def firebird_db_string(config)
616
- FireRuby::Database.db_string_for(config.symbolize_keys)
617
- end
618
-
619
- def set_psql_env(config)
620
- ENV['PGHOST'] = config['host'] if config['host']
621
- ENV['PGPORT'] = config['port'].to_s if config['port']
622
- ENV['PGPASSWORD'] = config['password'].to_s if config['password']
623
- ENV['PGUSER'] = config['username'].to_s if config['username']
624
- end