activerecord 5.2.8.1 → 6.1.7.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (316) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1347 -624
  3. data/MIT-LICENSE +3 -1
  4. data/README.rdoc +7 -5
  5. data/examples/performance.rb +1 -1
  6. data/lib/active_record/aggregations.rb +9 -8
  7. data/lib/active_record/association_relation.rb +30 -10
  8. data/lib/active_record/associations/alias_tracker.rb +19 -16
  9. data/lib/active_record/associations/association.rb +100 -41
  10. data/lib/active_record/associations/association_scope.rb +23 -21
  11. data/lib/active_record/associations/belongs_to_association.rb +55 -48
  12. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +7 -6
  13. data/lib/active_record/associations/builder/association.rb +45 -22
  14. data/lib/active_record/associations/builder/belongs_to.rb +29 -59
  15. data/lib/active_record/associations/builder/collection_association.rb +8 -17
  16. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +17 -41
  17. data/lib/active_record/associations/builder/has_many.rb +8 -2
  18. data/lib/active_record/associations/builder/has_one.rb +33 -2
  19. data/lib/active_record/associations/builder/singular_association.rb +3 -1
  20. data/lib/active_record/associations/collection_association.rb +44 -34
  21. data/lib/active_record/associations/collection_proxy.rb +25 -21
  22. data/lib/active_record/associations/foreign_association.rb +20 -0
  23. data/lib/active_record/associations/has_many_association.rb +26 -13
  24. data/lib/active_record/associations/has_many_through_association.rb +24 -18
  25. data/lib/active_record/associations/has_one_association.rb +43 -31
  26. data/lib/active_record/associations/has_one_through_association.rb +5 -5
  27. data/lib/active_record/associations/join_dependency/join_association.rb +44 -22
  28. data/lib/active_record/associations/join_dependency/join_part.rb +5 -5
  29. data/lib/active_record/associations/join_dependency.rb +91 -60
  30. data/lib/active_record/associations/preloader/association.rb +69 -43
  31. data/lib/active_record/associations/preloader/through_association.rb +49 -40
  32. data/lib/active_record/associations/preloader.rb +47 -34
  33. data/lib/active_record/associations/singular_association.rb +3 -17
  34. data/lib/active_record/associations/through_association.rb +1 -1
  35. data/lib/active_record/associations.rb +137 -25
  36. data/lib/active_record/attribute_assignment.rb +17 -19
  37. data/lib/active_record/attribute_methods/before_type_cast.rb +13 -7
  38. data/lib/active_record/attribute_methods/dirty.rb +101 -40
  39. data/lib/active_record/attribute_methods/primary_key.rb +20 -25
  40. data/lib/active_record/attribute_methods/query.rb +4 -8
  41. data/lib/active_record/attribute_methods/read.rb +14 -56
  42. data/lib/active_record/attribute_methods/serialization.rb +12 -7
  43. data/lib/active_record/attribute_methods/time_zone_conversion.rb +12 -15
  44. data/lib/active_record/attribute_methods/write.rb +18 -34
  45. data/lib/active_record/attribute_methods.rb +81 -143
  46. data/lib/active_record/attributes.rb +46 -9
  47. data/lib/active_record/autosave_association.rb +57 -42
  48. data/lib/active_record/base.rb +4 -17
  49. data/lib/active_record/callbacks.rb +158 -43
  50. data/lib/active_record/coders/yaml_column.rb +16 -7
  51. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +272 -130
  52. data/lib/active_record/connection_adapters/abstract/database_limits.rb +7 -36
  53. data/lib/active_record/connection_adapters/abstract/database_statements.rb +167 -146
  54. data/lib/active_record/connection_adapters/abstract/query_cache.rb +18 -14
  55. data/lib/active_record/connection_adapters/abstract/quoting.rb +107 -47
  56. data/lib/active_record/connection_adapters/abstract/savepoints.rb +3 -3
  57. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +153 -110
  58. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +211 -90
  59. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +2 -4
  60. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +385 -144
  61. data/lib/active_record/connection_adapters/abstract/transaction.rb +167 -69
  62. data/lib/active_record/connection_adapters/abstract_adapter.rb +229 -99
  63. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +243 -275
  64. data/lib/active_record/connection_adapters/column.rb +30 -12
  65. data/lib/active_record/connection_adapters/deduplicable.rb +29 -0
  66. data/lib/active_record/connection_adapters/legacy_pool_manager.rb +35 -0
  67. data/lib/active_record/connection_adapters/mysql/column.rb +1 -1
  68. data/lib/active_record/connection_adapters/mysql/database_statements.rb +88 -32
  69. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +1 -2
  70. data/lib/active_record/connection_adapters/mysql/quoting.rb +59 -7
  71. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +34 -10
  72. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +48 -32
  73. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +18 -7
  74. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +142 -19
  75. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +14 -9
  76. data/lib/active_record/connection_adapters/mysql2_adapter.rb +53 -18
  77. data/lib/active_record/connection_adapters/pool_config.rb +73 -0
  78. data/lib/active_record/connection_adapters/pool_manager.rb +47 -0
  79. data/lib/active_record/connection_adapters/postgresql/column.rb +37 -28
  80. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +40 -54
  81. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -2
  82. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +1 -4
  83. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +3 -5
  84. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +2 -2
  85. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +10 -2
  86. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +0 -1
  87. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +1 -2
  88. data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +49 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +3 -4
  90. data/lib/active_record/connection_adapters/postgresql/oid/macaddr.rb +25 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +1 -1
  92. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +3 -4
  93. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +25 -7
  94. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +1 -1
  95. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +9 -7
  96. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +15 -3
  97. data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
  98. data/lib/active_record/connection_adapters/postgresql/quoting.rb +73 -10
  99. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +2 -2
  100. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +19 -4
  101. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +107 -91
  102. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +0 -1
  103. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +120 -100
  104. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +31 -26
  105. data/lib/active_record/connection_adapters/postgresql/utils.rb +0 -1
  106. data/lib/active_record/connection_adapters/postgresql_adapter.rb +225 -121
  107. data/lib/active_record/connection_adapters/schema_cache.rb +159 -21
  108. data/lib/active_record/connection_adapters/sql_type_metadata.rb +17 -6
  109. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +146 -0
  110. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +42 -7
  111. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +5 -1
  112. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +77 -13
  113. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +174 -186
  114. data/lib/active_record/connection_adapters/statement_pool.rb +0 -1
  115. data/lib/active_record/connection_adapters.rb +52 -0
  116. data/lib/active_record/connection_handling.rb +293 -33
  117. data/lib/active_record/core.rb +341 -99
  118. data/lib/active_record/counter_cache.rb +8 -30
  119. data/lib/active_record/database_configurations/connection_url_resolver.rb +99 -0
  120. data/lib/active_record/database_configurations/database_config.rb +80 -0
  121. data/lib/active_record/database_configurations/hash_config.rb +96 -0
  122. data/lib/active_record/database_configurations/url_config.rb +53 -0
  123. data/lib/active_record/database_configurations.rb +273 -0
  124. data/lib/active_record/delegated_type.rb +209 -0
  125. data/lib/active_record/destroy_association_async_job.rb +36 -0
  126. data/lib/active_record/dynamic_matchers.rb +3 -4
  127. data/lib/active_record/enum.rb +108 -36
  128. data/lib/active_record/errors.rb +62 -19
  129. data/lib/active_record/explain.rb +10 -6
  130. data/lib/active_record/explain_subscriber.rb +1 -1
  131. data/lib/active_record/fixture_set/file.rb +10 -17
  132. data/lib/active_record/fixture_set/model_metadata.rb +32 -0
  133. data/lib/active_record/fixture_set/render_context.rb +17 -0
  134. data/lib/active_record/fixture_set/table_row.rb +152 -0
  135. data/lib/active_record/fixture_set/table_rows.rb +46 -0
  136. data/lib/active_record/fixtures.rb +200 -481
  137. data/lib/active_record/gem_version.rb +4 -4
  138. data/lib/active_record/inheritance.rb +53 -24
  139. data/lib/active_record/insert_all.rb +212 -0
  140. data/lib/active_record/integration.rb +67 -17
  141. data/lib/active_record/internal_metadata.rb +28 -9
  142. data/lib/active_record/legacy_yaml_adapter.rb +7 -3
  143. data/lib/active_record/locking/optimistic.rb +37 -23
  144. data/lib/active_record/locking/pessimistic.rb +9 -5
  145. data/lib/active_record/log_subscriber.rb +35 -35
  146. data/lib/active_record/middleware/database_selector/resolver/session.rb +48 -0
  147. data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
  148. data/lib/active_record/middleware/database_selector.rb +77 -0
  149. data/lib/active_record/migration/command_recorder.rb +96 -44
  150. data/lib/active_record/migration/compatibility.rb +145 -64
  151. data/lib/active_record/migration/join_table.rb +0 -1
  152. data/lib/active_record/migration.rb +206 -157
  153. data/lib/active_record/model_schema.rb +148 -22
  154. data/lib/active_record/nested_attributes.rb +4 -7
  155. data/lib/active_record/no_touching.rb +8 -1
  156. data/lib/active_record/null_relation.rb +0 -1
  157. data/lib/active_record/persistence.rb +267 -59
  158. data/lib/active_record/query_cache.rb +21 -4
  159. data/lib/active_record/querying.rb +40 -23
  160. data/lib/active_record/railtie.rb +113 -74
  161. data/lib/active_record/railties/console_sandbox.rb +2 -4
  162. data/lib/active_record/railties/controller_runtime.rb +30 -35
  163. data/lib/active_record/railties/databases.rake +411 -80
  164. data/lib/active_record/readonly_attributes.rb +4 -0
  165. data/lib/active_record/reflection.rb +109 -93
  166. data/lib/active_record/relation/batches/batch_enumerator.rb +25 -9
  167. data/lib/active_record/relation/batches.rb +44 -35
  168. data/lib/active_record/relation/calculations.rb +157 -90
  169. data/lib/active_record/relation/delegation.rb +35 -50
  170. data/lib/active_record/relation/finder_methods.rb +64 -39
  171. data/lib/active_record/relation/from_clause.rb +5 -1
  172. data/lib/active_record/relation/merger.rb +32 -40
  173. data/lib/active_record/relation/predicate_builder/array_handler.rb +13 -13
  174. data/lib/active_record/relation/predicate_builder/association_query_value.rb +5 -9
  175. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +1 -2
  176. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +11 -10
  177. data/lib/active_record/relation/predicate_builder/range_handler.rb +3 -23
  178. data/lib/active_record/relation/predicate_builder/relation_handler.rb +1 -1
  179. data/lib/active_record/relation/predicate_builder.rb +62 -45
  180. data/lib/active_record/relation/query_attribute.rb +13 -8
  181. data/lib/active_record/relation/query_methods.rb +478 -187
  182. data/lib/active_record/relation/record_fetch_warning.rb +3 -3
  183. data/lib/active_record/relation/spawn_methods.rb +9 -9
  184. data/lib/active_record/relation/where_clause.rb +115 -62
  185. data/lib/active_record/relation.rb +379 -115
  186. data/lib/active_record/result.rb +64 -38
  187. data/lib/active_record/runtime_registry.rb +2 -2
  188. data/lib/active_record/sanitization.rb +22 -41
  189. data/lib/active_record/schema.rb +2 -11
  190. data/lib/active_record/schema_dumper.rb +54 -9
  191. data/lib/active_record/schema_migration.rb +7 -9
  192. data/lib/active_record/scoping/default.rb +4 -8
  193. data/lib/active_record/scoping/named.rb +17 -24
  194. data/lib/active_record/scoping.rb +8 -9
  195. data/lib/active_record/secure_token.rb +16 -8
  196. data/lib/active_record/serialization.rb +5 -3
  197. data/lib/active_record/signed_id.rb +116 -0
  198. data/lib/active_record/statement_cache.rb +49 -6
  199. data/lib/active_record/store.rb +94 -10
  200. data/lib/active_record/suppressor.rb +2 -2
  201. data/lib/active_record/table_metadata.rb +42 -43
  202. data/lib/active_record/tasks/database_tasks.rb +277 -81
  203. data/lib/active_record/tasks/mysql_database_tasks.rb +37 -39
  204. data/lib/active_record/tasks/postgresql_database_tasks.rb +27 -32
  205. data/lib/active_record/tasks/sqlite_database_tasks.rb +14 -17
  206. data/lib/active_record/test_databases.rb +24 -0
  207. data/lib/active_record/test_fixtures.rb +291 -0
  208. data/lib/active_record/timestamp.rb +43 -32
  209. data/lib/active_record/touch_later.rb +23 -22
  210. data/lib/active_record/transactions.rb +62 -118
  211. data/lib/active_record/translation.rb +1 -1
  212. data/lib/active_record/type/adapter_specific_registry.rb +3 -13
  213. data/lib/active_record/type/hash_lookup_type_map.rb +0 -1
  214. data/lib/active_record/type/serialized.rb +6 -3
  215. data/lib/active_record/type/time.rb +10 -0
  216. data/lib/active_record/type/type_map.rb +0 -1
  217. data/lib/active_record/type/unsigned_integer.rb +0 -1
  218. data/lib/active_record/type.rb +10 -5
  219. data/lib/active_record/type_caster/connection.rb +15 -15
  220. data/lib/active_record/type_caster/map.rb +8 -8
  221. data/lib/active_record/validations/associated.rb +1 -2
  222. data/lib/active_record/validations/numericality.rb +35 -0
  223. data/lib/active_record/validations/uniqueness.rb +38 -30
  224. data/lib/active_record/validations.rb +4 -3
  225. data/lib/active_record.rb +13 -12
  226. data/lib/arel/alias_predication.rb +9 -0
  227. data/lib/arel/attributes/attribute.rb +41 -0
  228. data/lib/arel/collectors/bind.rb +29 -0
  229. data/lib/arel/collectors/composite.rb +39 -0
  230. data/lib/arel/collectors/plain_string.rb +20 -0
  231. data/lib/arel/collectors/sql_string.rb +27 -0
  232. data/lib/arel/collectors/substitute_binds.rb +35 -0
  233. data/lib/arel/crud.rb +42 -0
  234. data/lib/arel/delete_manager.rb +18 -0
  235. data/lib/arel/errors.rb +9 -0
  236. data/lib/arel/expressions.rb +29 -0
  237. data/lib/arel/factory_methods.rb +49 -0
  238. data/lib/arel/insert_manager.rb +49 -0
  239. data/lib/arel/math.rb +45 -0
  240. data/lib/arel/nodes/and.rb +32 -0
  241. data/lib/arel/nodes/ascending.rb +23 -0
  242. data/lib/arel/nodes/binary.rb +126 -0
  243. data/lib/arel/nodes/bind_param.rb +44 -0
  244. data/lib/arel/nodes/case.rb +55 -0
  245. data/lib/arel/nodes/casted.rb +62 -0
  246. data/lib/arel/nodes/comment.rb +29 -0
  247. data/lib/arel/nodes/count.rb +12 -0
  248. data/lib/arel/nodes/delete_statement.rb +45 -0
  249. data/lib/arel/nodes/descending.rb +23 -0
  250. data/lib/arel/nodes/equality.rb +15 -0
  251. data/lib/arel/nodes/extract.rb +24 -0
  252. data/lib/arel/nodes/false.rb +16 -0
  253. data/lib/arel/nodes/full_outer_join.rb +8 -0
  254. data/lib/arel/nodes/function.rb +44 -0
  255. data/lib/arel/nodes/grouping.rb +11 -0
  256. data/lib/arel/nodes/homogeneous_in.rb +76 -0
  257. data/lib/arel/nodes/in.rb +15 -0
  258. data/lib/arel/nodes/infix_operation.rb +92 -0
  259. data/lib/arel/nodes/inner_join.rb +8 -0
  260. data/lib/arel/nodes/insert_statement.rb +37 -0
  261. data/lib/arel/nodes/join_source.rb +20 -0
  262. data/lib/arel/nodes/matches.rb +18 -0
  263. data/lib/arel/nodes/named_function.rb +23 -0
  264. data/lib/arel/nodes/node.rb +51 -0
  265. data/lib/arel/nodes/node_expression.rb +13 -0
  266. data/lib/arel/nodes/ordering.rb +27 -0
  267. data/lib/arel/nodes/outer_join.rb +8 -0
  268. data/lib/arel/nodes/over.rb +15 -0
  269. data/lib/arel/nodes/regexp.rb +16 -0
  270. data/lib/arel/nodes/right_outer_join.rb +8 -0
  271. data/lib/arel/nodes/select_core.rb +67 -0
  272. data/lib/arel/nodes/select_statement.rb +41 -0
  273. data/lib/arel/nodes/sql_literal.rb +19 -0
  274. data/lib/arel/nodes/string_join.rb +11 -0
  275. data/lib/arel/nodes/table_alias.rb +31 -0
  276. data/lib/arel/nodes/terminal.rb +16 -0
  277. data/lib/arel/nodes/true.rb +16 -0
  278. data/lib/arel/nodes/unary.rb +44 -0
  279. data/lib/arel/nodes/unary_operation.rb +20 -0
  280. data/lib/arel/nodes/unqualified_column.rb +22 -0
  281. data/lib/arel/nodes/update_statement.rb +41 -0
  282. data/lib/arel/nodes/values_list.rb +9 -0
  283. data/lib/arel/nodes/window.rb +126 -0
  284. data/lib/arel/nodes/with.rb +11 -0
  285. data/lib/arel/nodes.rb +70 -0
  286. data/lib/arel/order_predications.rb +13 -0
  287. data/lib/arel/predications.rb +250 -0
  288. data/lib/arel/select_manager.rb +270 -0
  289. data/lib/arel/table.rb +118 -0
  290. data/lib/arel/tree_manager.rb +72 -0
  291. data/lib/arel/update_manager.rb +34 -0
  292. data/lib/arel/visitors/dot.rb +308 -0
  293. data/lib/arel/visitors/mysql.rb +93 -0
  294. data/lib/arel/visitors/postgresql.rb +120 -0
  295. data/lib/arel/visitors/sqlite.rb +38 -0
  296. data/lib/arel/visitors/to_sql.rb +899 -0
  297. data/lib/arel/visitors/visitor.rb +45 -0
  298. data/lib/arel/visitors.rb +13 -0
  299. data/lib/arel/window_predications.rb +9 -0
  300. data/lib/arel.rb +54 -0
  301. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +0 -1
  302. data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -5
  303. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +3 -1
  304. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +7 -5
  305. data/lib/rails/generators/active_record/migration.rb +19 -2
  306. data/lib/rails/generators/active_record/model/model_generator.rb +39 -2
  307. data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +7 -0
  308. data/lib/rails/generators/active_record/model/templates/model.rb.tt +10 -1
  309. metadata +118 -32
  310. data/lib/active_record/attribute_decorators.rb +0 -90
  311. data/lib/active_record/collection_cache_key.rb +0 -53
  312. data/lib/active_record/connection_adapters/connection_specification.rb +0 -287
  313. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +0 -33
  314. data/lib/active_record/define_callbacks.rb +0 -22
  315. data/lib/active_record/relation/predicate_builder/base_handler.rb +0 -19
  316. data/lib/active_record/relation/where_clause_factory.rb +0 -34
@@ -1,10 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_record"
4
+ require "active_support/configuration_file"
5
+ require "active_support/deprecation"
6
+
7
+ databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
4
8
 
5
9
  db_namespace = namespace :db do
6
10
  desc "Set the environment value for the database"
7
11
  task "environment:set" => :load_config do
12
+ raise ActiveRecord::EnvironmentStorageError unless ActiveRecord::InternalMetadata.enabled?
8
13
  ActiveRecord::InternalMetadata.create_table
9
14
  ActiveRecord::InternalMetadata[:environment] = ActiveRecord::Base.connection.migration_context.current_environment
10
15
  end
@@ -14,7 +19,10 @@ db_namespace = namespace :db do
14
19
  end
15
20
 
16
21
  task load_config: :environment do
17
- ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {}
22
+ if ActiveRecord::Base.configurations.empty?
23
+ ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration
24
+ end
25
+
18
26
  ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
19
27
  end
20
28
 
@@ -22,9 +30,17 @@ db_namespace = namespace :db do
22
30
  task all: :load_config do
23
31
  ActiveRecord::Tasks::DatabaseTasks.create_all
24
32
  end
33
+
34
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
35
+ desc "Create #{name} database for current environment"
36
+ task name => :load_config do
37
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
38
+ ActiveRecord::Tasks::DatabaseTasks.create(db_config)
39
+ end
40
+ end
25
41
  end
26
42
 
27
- 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."
43
+ 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, except when DATABASE_URL is present."
28
44
  task create: [:load_config] do
29
45
  ActiveRecord::Tasks::DatabaseTasks.create_current
30
46
  end
@@ -33,9 +49,17 @@ db_namespace = namespace :db do
33
49
  task all: [:load_config, :check_protected_environments] do
34
50
  ActiveRecord::Tasks::DatabaseTasks.drop_all
35
51
  end
52
+
53
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
54
+ desc "Drop #{name} database for current environment"
55
+ task name => [:load_config, :check_protected_environments] do
56
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
57
+ ActiveRecord::Tasks::DatabaseTasks.drop(db_config)
58
+ end
59
+ end
36
60
  end
37
61
 
38
- 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."
62
+ 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, except when DATABASE_URL is present."
39
63
  task drop: [:load_config, :check_protected_environments] do
40
64
  db_namespace["drop:_unsafe"].invoke
41
65
  end
@@ -50,35 +74,70 @@ db_namespace = namespace :db do
50
74
  end
51
75
  end
52
76
 
53
- # 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
+ # desc "Truncates tables of each database for current environment"
78
+ task truncate_all: [:load_config, :check_protected_environments] do
79
+ ActiveRecord::Tasks::DatabaseTasks.truncate_all
80
+ end
81
+
82
+ # 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, except when DATABASE_URL is present."
54
83
  task purge: [:load_config, :check_protected_environments] do
55
84
  ActiveRecord::Tasks::DatabaseTasks.purge_current
56
85
  end
57
86
 
58
87
  desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
59
88
  task migrate: :load_config do
60
- ActiveRecord::Tasks::DatabaseTasks.migrate
89
+ original_db_config = ActiveRecord::Base.connection_db_config
90
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
91
+ ActiveRecord::Base.establish_connection(db_config)
92
+ ActiveRecord::Tasks::DatabaseTasks.migrate
93
+ end
61
94
  db_namespace["_dump"].invoke
95
+ ensure
96
+ ActiveRecord::Base.establish_connection(original_db_config)
62
97
  end
63
98
 
64
99
  # IMPORTANT: This task won't dump the schema if ActiveRecord::Base.dump_schema_after_migration is set to false
65
100
  task :_dump do
66
101
  if ActiveRecord::Base.dump_schema_after_migration
67
- case ActiveRecord::Base.schema_format
68
- when :ruby then db_namespace["schema:dump"].invoke
69
- when :sql then db_namespace["structure:dump"].invoke
70
- else
71
- raise "unknown schema format #{ActiveRecord::Base.schema_format}"
72
- end
102
+ db_namespace["schema:dump"].invoke
73
103
  end
74
104
  # Allow this task to be called as many times as required. An example is the
75
105
  # migrate:redo task, which calls other two internally that depend on this one.
76
106
  db_namespace["_dump"].reenable
77
107
  end
78
108
 
109
+ namespace :_dump do
110
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
111
+ # IMPORTANT: This task won't dump the schema if ActiveRecord::Base.dump_schema_after_migration is set to false
112
+ task name do
113
+ if ActiveRecord::Base.dump_schema_after_migration
114
+ db_namespace["schema:dump:#{name}"].invoke
115
+ end
116
+ # Allow this task to be called as many times as required. An example is the
117
+ # migrate:redo task, which calls other two internally that depend on this one.
118
+ db_namespace["_dump:#{name}"].reenable
119
+ end
120
+ end
121
+ end
122
+
79
123
  namespace :migrate do
80
- # desc 'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
124
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
125
+ desc "Migrate #{name} database for current environment"
126
+ task name => :load_config do
127
+ original_db_config = ActiveRecord::Base.connection_db_config
128
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
129
+ ActiveRecord::Base.establish_connection(db_config)
130
+ ActiveRecord::Tasks::DatabaseTasks.migrate
131
+ db_namespace["_dump:#{name}"].invoke
132
+ ensure
133
+ ActiveRecord::Base.establish_connection(original_db_config)
134
+ end
135
+ end
136
+
137
+ desc "Rolls back the database one migration and re-migrates up (options: STEP=x, VERSION=x)."
81
138
  task redo: :load_config do
139
+ ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:migrate:redo")
140
+
82
141
  raise "Empty VERSION provided" if ENV["VERSION"] && ENV["VERSION"].empty?
83
142
 
84
143
  if ENV["VERSION"]
@@ -90,11 +149,30 @@ db_namespace = namespace :db do
90
149
  end
91
150
  end
92
151
 
152
+ namespace :redo do
153
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
154
+ desc "Rolls back #{name} database one migration and re-migrates up (options: STEP=x, VERSION=x)."
155
+ task name => :load_config do
156
+ raise "Empty VERSION provided" if ENV["VERSION"] && ENV["VERSION"].empty?
157
+
158
+ if ENV["VERSION"]
159
+ db_namespace["migrate:down:#{name}"].invoke
160
+ db_namespace["migrate:up:#{name}"].invoke
161
+ else
162
+ db_namespace["rollback:#{name}"].invoke
163
+ db_namespace["migrate:#{name}"].invoke
164
+ end
165
+ end
166
+ end
167
+ end
168
+
93
169
  # desc 'Resets your database using your migrations for the current environment'
94
170
  task reset: ["db:drop", "db:create", "db:migrate"]
95
171
 
96
- # desc 'Runs the "up" for a given migration VERSION.'
172
+ desc 'Runs the "up" for a given migration VERSION.'
97
173
  task up: :load_config do
174
+ ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:migrate:up")
175
+
98
176
  raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
99
177
 
100
178
  ActiveRecord::Tasks::DatabaseTasks.check_target_version
@@ -106,8 +184,29 @@ db_namespace = namespace :db do
106
184
  db_namespace["_dump"].invoke
107
185
  end
108
186
 
109
- # desc 'Runs the "down" for a given migration VERSION.'
187
+ namespace :up do
188
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
189
+ task name => :load_config do
190
+ raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
191
+
192
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
193
+
194
+ ActiveRecord::Base.establish_connection(db_config)
195
+ ActiveRecord::Tasks::DatabaseTasks.check_target_version
196
+ ActiveRecord::Base.connection.migration_context.run(
197
+ :up,
198
+ ActiveRecord::Tasks::DatabaseTasks.target_version
199
+ )
200
+
201
+ db_namespace["_dump"].invoke
202
+ end
203
+ end
204
+ end
205
+
206
+ desc 'Runs the "down" for a given migration VERSION.'
110
207
  task down: :load_config do
208
+ ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:migrate:down")
209
+
111
210
  raise "VERSION is required - To go down one migration, use db:rollback" if !ENV["VERSION"] || ENV["VERSION"].empty?
112
211
 
113
212
  ActiveRecord::Tasks::DatabaseTasks.check_target_version
@@ -119,27 +218,68 @@ db_namespace = namespace :db do
119
218
  db_namespace["_dump"].invoke
120
219
  end
121
220
 
221
+ namespace :down do
222
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
223
+ task name => :load_config do
224
+ raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
225
+
226
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
227
+
228
+ ActiveRecord::Base.establish_connection(db_config)
229
+ ActiveRecord::Tasks::DatabaseTasks.check_target_version
230
+ ActiveRecord::Base.connection.migration_context.run(
231
+ :down,
232
+ ActiveRecord::Tasks::DatabaseTasks.target_version
233
+ )
234
+
235
+ db_namespace["_dump"].invoke
236
+ end
237
+ end
238
+ end
239
+
122
240
  desc "Display status of migrations"
123
241
  task status: :load_config do
124
- unless ActiveRecord::SchemaMigration.table_exists?
125
- abort "Schema migrations table does not exist yet."
242
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
243
+ ActiveRecord::Base.establish_connection(db_config)
244
+ ActiveRecord::Tasks::DatabaseTasks.migrate_status
245
+ end
246
+ end
247
+
248
+ namespace :status do
249
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
250
+ desc "Display status of migrations for #{name} database"
251
+ task name => :load_config do
252
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
253
+ ActiveRecord::Base.establish_connection(db_config)
254
+ ActiveRecord::Tasks::DatabaseTasks.migrate_status
255
+ end
126
256
  end
257
+ end
258
+ end
259
+
260
+ namespace :rollback do
261
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
262
+ desc "Rollback #{name} database for current environment (specify steps w/ STEP=n)."
263
+ task name => :load_config do
264
+ step = ENV["STEP"] ? ENV["STEP"].to_i : 1
265
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
127
266
 
128
- # output
129
- puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
130
- puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
131
- puts "-" * 50
132
- ActiveRecord::Base.connection.migration_context.migrations_status.each do |status, version, name|
133
- puts "#{status.center(8)} #{version.ljust(14)} #{name}"
267
+ ActiveRecord::Base.establish_connection(db_config)
268
+ ActiveRecord::Base.connection.migration_context.rollback(step)
269
+
270
+ db_namespace["_dump"].invoke
134
271
  end
135
- puts
136
272
  end
137
273
  end
138
274
 
139
275
  desc "Rolls the schema back to the previous version (specify steps w/ STEP=n)."
140
276
  task rollback: :load_config do
277
+ ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:rollback")
278
+
141
279
  step = ENV["STEP"] ? ENV["STEP"].to_i : 1
280
+
142
281
  ActiveRecord::Base.connection.migration_context.rollback(step)
282
+
143
283
  db_namespace["_dump"].invoke
144
284
  end
145
285
 
@@ -150,7 +290,7 @@ db_namespace = namespace :db do
150
290
  db_namespace["_dump"].invoke
151
291
  end
152
292
 
153
- # desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
293
+ desc "Drops and recreates the database from db/schema.rb for the current environment and loads the seeds."
154
294
  task reset: [ "db:drop", "db:setup" ]
155
295
 
156
296
  # desc "Retrieves the charset for the current environment's database"
@@ -160,11 +300,9 @@ db_namespace = namespace :db do
160
300
 
161
301
  # desc "Retrieves the collation for the current environment's database"
162
302
  task collation: :load_config do
163
- begin
164
- puts ActiveRecord::Tasks::DatabaseTasks.collation_current
165
- rescue NoMethodError
166
- $stderr.puts "Sorry, your database adapter is not supported yet. Feel free to submit a patch."
167
- end
303
+ puts ActiveRecord::Tasks::DatabaseTasks.collation_current
304
+ rescue NoMethodError
305
+ $stderr.puts "Sorry, your database adapter is not supported yet. Feel free to submit a patch."
168
306
  end
169
307
 
170
308
  desc "Retrieves the current schema version number"
@@ -174,28 +312,93 @@ db_namespace = namespace :db do
174
312
 
175
313
  # desc "Raises an error if there are pending migrations"
176
314
  task abort_if_pending_migrations: :load_config do
177
- pending_migrations = ActiveRecord::Base.connection.migration_context.open.pending_migrations
315
+ pending_migrations = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).flat_map do |db_config|
316
+ ActiveRecord::Base.establish_connection(db_config)
317
+
318
+ ActiveRecord::Base.connection.migration_context.open.pending_migrations
319
+ end
178
320
 
179
321
  if pending_migrations.any?
180
322
  puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
181
323
  pending_migrations.each do |pending_migration|
182
324
  puts " %4d %s" % [pending_migration.version, pending_migration.name]
183
325
  end
184
- abort %{Run `rails db:migrate` to update your database then try again.}
326
+ abort %{Run `bin/rails db:migrate` to update your database then try again.}
327
+ end
328
+ ensure
329
+ ActiveRecord::Base.establish_connection(ActiveRecord::Tasks::DatabaseTasks.env.to_sym)
330
+ end
331
+
332
+ namespace :abort_if_pending_migrations do
333
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
334
+ # desc "Raises an error if there are pending migrations for #{name} database"
335
+ task name => :load_config do
336
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
337
+ ActiveRecord::Base.establish_connection(db_config)
338
+
339
+ pending_migrations = ActiveRecord::Base.connection.migration_context.open.pending_migrations
340
+
341
+ if pending_migrations.any?
342
+ puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
343
+ pending_migrations.each do |pending_migration|
344
+ puts " %4d %s" % [pending_migration.version, pending_migration.name]
345
+ end
346
+ abort %{Run `bin/rails db:migrate:#{name}` to update your database then try again.}
347
+ end
348
+ end
185
349
  end
186
350
  end
187
351
 
188
352
  desc "Creates the database, loads the schema, and initializes with the seed data (use db:reset to also drop the database first)"
189
- task setup: ["db:schema:load_if_ruby", "db:structure:load_if_sql", :seed]
353
+ task setup: ["db:create", :environment, "db:schema:load", :seed]
354
+
355
+ desc "Runs setup if database does not exist, or runs migrations if it does"
356
+ task prepare: :load_config do
357
+ seed = false
358
+
359
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
360
+ ActiveRecord::Base.establish_connection(db_config)
361
+
362
+ # Skipped when no database
363
+ ActiveRecord::Tasks::DatabaseTasks.migrate
364
+
365
+ if ActiveRecord::Base.dump_schema_after_migration
366
+ ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, ActiveRecord::Base.schema_format)
367
+ end
368
+ rescue ActiveRecord::NoDatabaseError
369
+ config_name = db_config.name
370
+ ActiveRecord::Tasks::DatabaseTasks.create_current(db_config.env_name, config_name)
371
+
372
+ if File.exist?(ActiveRecord::Tasks::DatabaseTasks.dump_filename(config_name))
373
+ ActiveRecord::Tasks::DatabaseTasks.load_schema(
374
+ db_config,
375
+ ActiveRecord::Base.schema_format,
376
+ nil
377
+ )
378
+ else
379
+ ActiveRecord::Tasks::DatabaseTasks.migrate
380
+ end
381
+
382
+ seed = true
383
+ end
384
+
385
+ ActiveRecord::Base.establish_connection
386
+ ActiveRecord::Tasks::DatabaseTasks.load_seed if seed
387
+ end
190
388
 
191
389
  desc "Loads the seed data from db/seeds.rb"
192
- task :seed do
390
+ task seed: :load_config do
193
391
  db_namespace["abort_if_pending_migrations"].invoke
194
392
  ActiveRecord::Tasks::DatabaseTasks.load_seed
195
393
  end
196
394
 
395
+ namespace :seed do
396
+ desc "Truncates tables of each database for current environment and loads the seeds"
397
+ task replant: [:load_config, :truncate_all, :seed]
398
+ end
399
+
197
400
  namespace :fixtures do
198
- 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."
401
+ 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 (e.g. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
199
402
  task load: :load_config do
200
403
  require "active_record/fixtures"
201
404
 
@@ -210,14 +413,15 @@ db_namespace = namespace :db do
210
413
  fixture_files = if ENV["FIXTURES"]
211
414
  ENV["FIXTURES"].split(",")
212
415
  else
213
- # The use of String#[] here is to support namespaced fixtures.
214
- Dir["#{fixtures_dir}/**/*.yml"].map { |f| f[(fixtures_dir.size + 1)..-5] }
416
+ files = Dir[File.join(fixtures_dir, "**/*.{yml}")]
417
+ files.reject! { |f| f.start_with?(File.join(fixtures_dir, "files")) }
418
+ files.map! { |f| f[fixtures_dir.to_s.size..-5].delete_prefix("/") }
215
419
  end
216
420
 
217
421
  ActiveRecord::FixtureSet.create_fixtures(fixtures_dir, fixture_files)
218
422
  end
219
423
 
220
- # desc "Search for a fixture given a LABEL or ID. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
424
+ # desc "Search for a fixture given a LABEL or ID. Specify an alternative path (e.g. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
221
425
  task identify: :load_config do
222
426
  require "active_record/fixtures"
223
427
 
@@ -229,7 +433,7 @@ db_namespace = namespace :db do
229
433
  base_dir = ActiveRecord::Tasks::DatabaseTasks.fixtures_path
230
434
 
231
435
  Dir["#{base_dir}/**/*.yml"].each do |file|
232
- if data = YAML.load(ERB.new(IO.read(file)).result)
436
+ if data = ActiveSupport::ConfigurationFile.parse(file)
233
437
  data.each_key do |key|
234
438
  key_id = ActiveRecord::FixtureSet.identify(key)
235
439
 
@@ -243,100 +447,173 @@ db_namespace = namespace :db do
243
447
  end
244
448
 
245
449
  namespace :schema do
246
- desc "Creates a db/schema.rb file that is portable against any DB supported by Active Record"
450
+ desc "Creates a database schema file (either db/schema.rb or db/structure.sql, depending on `config.active_record.schema_format`)"
247
451
  task dump: :load_config do
248
- require "active_record/schema_dumper"
249
- filename = ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema.rb")
250
- File.open(filename, "w:utf-8") do |file|
251
- ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
452
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
453
+ ActiveRecord::Base.establish_connection(db_config)
454
+ ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config)
252
455
  end
456
+
253
457
  db_namespace["schema:dump"].reenable
254
458
  end
255
459
 
256
- desc "Loads a schema.rb file into the database"
460
+ desc "Loads a database schema file (either db/schema.rb or db/structure.sql, depending on `config.active_record.schema_format`) into the database"
257
461
  task load: [:load_config, :check_protected_environments] do
258
- ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV["SCHEMA"])
462
+ ActiveRecord::Tasks::DatabaseTasks.load_schema_current(ActiveRecord::Base.schema_format, ENV["SCHEMA"])
259
463
  end
260
464
 
261
465
  task load_if_ruby: ["db:create", :environment] do
466
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
467
+ Using `bin/rails db:schema:load_if_ruby` is deprecated and will be removed in Rails 7.0.
468
+ Configure the format using `config.active_record.schema_format = :ruby` to use `schema.rb` and run `bin/rails db:schema:load` instead.
469
+ MSG
262
470
  db_namespace["schema:load"].invoke if ActiveRecord::Base.schema_format == :ruby
263
471
  end
264
472
 
473
+ namespace :dump do
474
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
475
+ desc "Creates a database schema file (either db/schema.rb or db/structure.sql, depending on `config.active_record.schema_format`) for #{name} database"
476
+ task name => :load_config do
477
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: name)
478
+ ActiveRecord::Base.establish_connection(db_config)
479
+ ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config)
480
+ db_namespace["schema:dump:#{name}"].reenable
481
+ end
482
+ end
483
+ end
484
+
485
+ namespace :load do
486
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
487
+ desc "Loads a database schema file (either db/schema.rb or db/structure.sql, depending on `config.active_record.schema_format`) into the #{name} database"
488
+ task name => :load_config do
489
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: name)
490
+ ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, ActiveRecord::Base.schema_format, ENV["SCHEMA"])
491
+ end
492
+ end
493
+ end
494
+
265
495
  namespace :cache do
266
496
  desc "Creates a db/schema_cache.yml file."
267
497
  task dump: :load_config do
268
- conn = ActiveRecord::Base.connection
269
- filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.yml")
270
- ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(conn, filename)
498
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
499
+ ActiveRecord::Base.establish_connection(db_config)
500
+ filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(
501
+ db_config.name,
502
+ schema_cache_path: db_config.schema_cache_path,
503
+ )
504
+ ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(
505
+ ActiveRecord::Base.connection,
506
+ filename,
507
+ )
508
+ end
271
509
  end
272
510
 
273
511
  desc "Clears a db/schema_cache.yml file."
274
512
  task clear: :load_config do
275
- filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.yml")
276
- rm_f filename, verbose: false
513
+ ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
514
+ filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(
515
+ db_config.name,
516
+ schema_cache_path: db_config.schema_cache_path,
517
+ )
518
+ ActiveRecord::Tasks::DatabaseTasks.clear_schema_cache(
519
+ filename,
520
+ )
521
+ end
277
522
  end
278
523
  end
279
-
280
524
  end
281
525
 
282
526
  namespace :structure do
283
527
  desc "Dumps the database structure to db/structure.sql. Specify another file with SCHEMA=db/my_structure.sql"
284
528
  task dump: :load_config do
285
- filename = ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "structure.sql")
286
- current_config = ActiveRecord::Tasks::DatabaseTasks.current_config
287
- ActiveRecord::Tasks::DatabaseTasks.structure_dump(current_config, filename)
288
-
289
- if ActiveRecord::SchemaMigration.table_exists?
290
- File.open(filename, "a") do |f|
291
- f.puts ActiveRecord::Base.connection.dump_schema_information
292
- f.print "\n"
293
- end
294
- end
529
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
530
+ Using `bin/rails db:structure:dump` is deprecated and will be removed in Rails 7.0.
531
+ Configure the format using `config.active_record.schema_format = :sql` to use `structure.sql` and run `bin/rails db:schema:dump` instead.
532
+ MSG
533
+
534
+ db_namespace["schema:dump"].invoke
295
535
  db_namespace["structure:dump"].reenable
296
536
  end
297
537
 
298
538
  desc "Recreates the databases from the structure.sql file"
299
539
  task load: [:load_config, :check_protected_environments] do
300
- ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:sql, ENV["SCHEMA"])
540
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
541
+ Using `bin/rails db:structure:load` is deprecated and will be removed in Rails 7.0.
542
+ Configure the format using `config.active_record.schema_format = :sql` to use `structure.sql` and run `bin/rails db:schema:load` instead.
543
+ MSG
544
+ db_namespace["schema:load"].invoke
301
545
  end
302
546
 
303
547
  task load_if_sql: ["db:create", :environment] do
304
- db_namespace["structure:load"].invoke if ActiveRecord::Base.schema_format == :sql
548
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
549
+ Using `bin/rails db:structure:load_if_sql` is deprecated and will be removed in Rails 7.0.
550
+ Configure the format using `config.active_record.schema_format = :sql` to use `structure.sql` and run `bin/rails db:schema:load` instead.
551
+ MSG
552
+ db_namespace["schema:load"].invoke if ActiveRecord::Base.schema_format == :sql
553
+ end
554
+
555
+ namespace :dump do
556
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
557
+ desc "Dumps the #{name} database structure to db/structure.sql. Specify another file with SCHEMA=db/my_structure.sql"
558
+ task name => :load_config do
559
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
560
+ Using `bin/rails db:structure:dump:#{name}` is deprecated and will be removed in Rails 7.0.
561
+ Configure the format using `config.active_record.schema_format = :sql` to use `structure.sql` and run `bin/rails db:schema:dump:#{name}` instead.
562
+ MSG
563
+ db_namespace["schema:dump:#{name}"].invoke
564
+ db_namespace["structure:dump:#{name}"].reenable
565
+ end
566
+ end
567
+ end
568
+
569
+ namespace :load do
570
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
571
+ desc "Recreates the #{name} database from the structure.sql file"
572
+ task name => :load_config do
573
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
574
+ Using `bin/rails db:structure:load:#{name}` is deprecated and will be removed in Rails 7.0.
575
+ Configure the format using `config.active_record.schema_format = :sql` to use `structure.sql` and run `bin/rails db:schema:load:#{name}` instead.
576
+ MSG
577
+ db_namespace["schema:load:#{name}"].invoke
578
+ end
579
+ end
305
580
  end
306
581
  end
307
582
 
308
583
  namespace :test do
309
584
  # desc "Recreate the test database from the current schema"
310
585
  task load: %w(db:test:purge) do
311
- case ActiveRecord::Base.schema_format
312
- when :ruby
313
- db_namespace["test:load_schema"].invoke
314
- when :sql
315
- db_namespace["test:load_structure"].invoke
316
- end
586
+ db_namespace["test:load_schema"].invoke
317
587
  end
318
588
 
319
- # desc "Recreate the test database from an existent schema.rb file"
589
+ # desc "Recreate the test database from an existent schema file (schema.rb or structure.sql, depending on `config.active_record.schema_format`)"
320
590
  task load_schema: %w(db:test:purge) do
321
- begin
322
- should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
323
- ActiveRecord::Schema.verbose = false
324
- ActiveRecord::Tasks::DatabaseTasks.load_schema ActiveRecord::Base.configurations["test"], :ruby, ENV["SCHEMA"], "test"
325
- ensure
326
- if should_reconnect
327
- ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ActiveRecord::Tasks::DatabaseTasks.env])
328
- end
591
+ should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
592
+ ActiveRecord::Schema.verbose = false
593
+ ActiveRecord::Base.configurations.configs_for(env_name: "test").each do |db_config|
594
+ filename = ActiveRecord::Tasks::DatabaseTasks.dump_filename(db_config.name)
595
+ ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, ActiveRecord::Base.schema_format, filename)
596
+ end
597
+ ensure
598
+ if should_reconnect
599
+ ActiveRecord::Base.establish_connection(ActiveRecord::Tasks::DatabaseTasks.env.to_sym)
329
600
  end
330
601
  end
331
602
 
332
603
  # desc "Recreate the test database from an existent structure.sql file"
333
604
  task load_structure: %w(db:test:purge) do
334
- ActiveRecord::Tasks::DatabaseTasks.load_schema ActiveRecord::Base.configurations["test"], :sql, ENV["SCHEMA"], "test"
605
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
606
+ Using `bin/rails db:test:load_structure` is deprecated and will be removed in Rails 7.0.
607
+ Configure the format using `config.active_record.schema_format = :sql` to use `structure.sql` and run `bin/rails db:test:load_schema` instead.
608
+ MSG
609
+ db_namespace["test:load_schema"].invoke
335
610
  end
336
611
 
337
612
  # desc "Empty the test database"
338
613
  task purge: %w(load_config check_protected_environments) do
339
- ActiveRecord::Tasks::DatabaseTasks.purge ActiveRecord::Base.configurations["test"]
614
+ ActiveRecord::Base.configurations.configs_for(env_name: "test").each do |db_config|
615
+ ActiveRecord::Tasks::DatabaseTasks.purge(db_config)
616
+ end
340
617
  end
341
618
 
342
619
  # desc 'Load the test schema'
@@ -345,6 +622,56 @@ db_namespace = namespace :db do
345
622
  db_namespace["test:load"].invoke
346
623
  end
347
624
  end
625
+
626
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
627
+ # desc "Recreate the #{name} test database"
628
+ namespace :load do
629
+ task name => "db:test:purge:#{name}" do
630
+ db_namespace["test:load_schema:#{name}"].invoke
631
+ end
632
+ end
633
+
634
+ # desc "Recreate the #{name} test database from an existent schema.rb file"
635
+ namespace :load_schema do
636
+ task name => "db:test:purge:#{name}" do
637
+ should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
638
+ ActiveRecord::Schema.verbose = false
639
+ filename = ActiveRecord::Tasks::DatabaseTasks.dump_filename(name)
640
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: "test", name: name)
641
+ ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, ActiveRecord::Base.schema_format, filename)
642
+ ensure
643
+ if should_reconnect
644
+ ActiveRecord::Base.establish_connection(ActiveRecord::Tasks::DatabaseTasks.env.to_sym)
645
+ end
646
+ end
647
+ end
648
+
649
+ # desc "Recreate the #{name} test database from an existent structure.sql file"
650
+ namespace :load_structure do
651
+ task name => "db:test:purge:#{name}" do
652
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
653
+ Using `bin/rails db:test:load_structure:#{name}` is deprecated and will be removed in Rails 7.0.
654
+ Configure the format using `config.active_record.schema_format = :sql` to use `structure.sql` and run `bin/rails db:test:load_structure:#{name}` instead.
655
+ MSG
656
+ db_namespace["test:load_schema:#{name}"].invoke
657
+ end
658
+ end
659
+
660
+ # desc "Empty the #{name} test database"
661
+ namespace :purge do
662
+ task name => %w(load_config check_protected_environments) do
663
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: "test", name: name)
664
+ ActiveRecord::Tasks::DatabaseTasks.purge(db_config)
665
+ end
666
+ end
667
+
668
+ # desc 'Load the #{name} database test schema'
669
+ namespace :prepare do
670
+ task name => :load_config do
671
+ db_namespace["test:load:#{name}"].invoke
672
+ end
673
+ end
674
+ end
348
675
  end
349
676
  end
350
677
 
@@ -360,6 +687,10 @@ namespace :railties do
360
687
  if railtie.respond_to?(:paths) && (path = railtie.paths["db/migrate"].first)
361
688
  railties[railtie.railtie_name] = path
362
689
  end
690
+
691
+ unless ENV["MIGRATIONS_PATH"].blank?
692
+ railties[railtie.railtie_name] = railtie.root + ENV["MIGRATIONS_PATH"]
693
+ end
363
694
  end
364
695
 
365
696
  on_skip = Proc.new do |name, migration|