activerecord 6.0.1 → 6.1.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (270) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1363 -647
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +4 -4
  5. data/lib/active_record/aggregations.rb +5 -6
  6. data/lib/active_record/association_relation.rb +26 -15
  7. data/lib/active_record/associations/alias_tracker.rb +19 -16
  8. data/lib/active_record/associations/association.rb +55 -37
  9. data/lib/active_record/associations/association_scope.rb +19 -15
  10. data/lib/active_record/associations/belongs_to_association.rb +23 -10
  11. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -3
  12. data/lib/active_record/associations/builder/association.rb +32 -5
  13. data/lib/active_record/associations/builder/belongs_to.rb +10 -7
  14. data/lib/active_record/associations/builder/collection_association.rb +5 -4
  15. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +0 -3
  16. data/lib/active_record/associations/builder/has_many.rb +6 -2
  17. data/lib/active_record/associations/builder/has_one.rb +11 -14
  18. data/lib/active_record/associations/builder/singular_association.rb +1 -1
  19. data/lib/active_record/associations/collection_association.rb +38 -13
  20. data/lib/active_record/associations/collection_proxy.rb +14 -7
  21. data/lib/active_record/associations/foreign_association.rb +13 -0
  22. data/lib/active_record/associations/has_many_association.rb +24 -3
  23. data/lib/active_record/associations/has_many_through_association.rb +10 -4
  24. data/lib/active_record/associations/has_one_association.rb +15 -1
  25. data/lib/active_record/associations/join_dependency/join_association.rb +39 -16
  26. data/lib/active_record/associations/join_dependency/join_part.rb +3 -3
  27. data/lib/active_record/associations/join_dependency.rb +73 -42
  28. data/lib/active_record/associations/preloader/association.rb +49 -25
  29. data/lib/active_record/associations/preloader/through_association.rb +2 -2
  30. data/lib/active_record/associations/preloader.rb +12 -7
  31. data/lib/active_record/associations/singular_association.rb +1 -1
  32. data/lib/active_record/associations/through_association.rb +1 -1
  33. data/lib/active_record/associations.rb +119 -12
  34. data/lib/active_record/attribute_assignment.rb +10 -9
  35. data/lib/active_record/attribute_methods/before_type_cast.rb +13 -10
  36. data/lib/active_record/attribute_methods/dirty.rb +3 -13
  37. data/lib/active_record/attribute_methods/primary_key.rb +6 -4
  38. data/lib/active_record/attribute_methods/query.rb +3 -6
  39. data/lib/active_record/attribute_methods/read.rb +8 -12
  40. data/lib/active_record/attribute_methods/serialization.rb +11 -6
  41. data/lib/active_record/attribute_methods/time_zone_conversion.rb +12 -15
  42. data/lib/active_record/attribute_methods/write.rb +12 -21
  43. data/lib/active_record/attribute_methods.rb +64 -54
  44. data/lib/active_record/attributes.rb +33 -9
  45. data/lib/active_record/autosave_association.rb +56 -41
  46. data/lib/active_record/base.rb +2 -14
  47. data/lib/active_record/callbacks.rb +153 -24
  48. data/lib/active_record/coders/yaml_column.rb +24 -3
  49. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +190 -136
  50. data/lib/active_record/connection_adapters/abstract/database_limits.rb +2 -44
  51. data/lib/active_record/connection_adapters/abstract/database_statements.rb +83 -38
  52. data/lib/active_record/connection_adapters/abstract/query_cache.rb +3 -9
  53. data/lib/active_record/connection_adapters/abstract/quoting.rb +44 -35
  54. data/lib/active_record/connection_adapters/abstract/savepoints.rb +3 -3
  55. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +152 -116
  56. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +145 -52
  57. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +3 -3
  58. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +267 -105
  59. data/lib/active_record/connection_adapters/abstract/transaction.rb +94 -36
  60. data/lib/active_record/connection_adapters/abstract_adapter.rb +63 -77
  61. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +136 -111
  62. data/lib/active_record/connection_adapters/column.rb +15 -1
  63. data/lib/active_record/connection_adapters/deduplicable.rb +29 -0
  64. data/lib/active_record/connection_adapters/legacy_pool_manager.rb +35 -0
  65. data/lib/active_record/connection_adapters/mysql/column.rb +1 -1
  66. data/lib/active_record/connection_adapters/mysql/database_statements.rb +30 -36
  67. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +1 -2
  68. data/lib/active_record/connection_adapters/mysql/quoting.rb +18 -3
  69. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +32 -7
  70. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +8 -0
  71. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +5 -2
  72. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +20 -13
  73. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +10 -1
  74. data/lib/active_record/connection_adapters/mysql2_adapter.rb +31 -13
  75. data/lib/active_record/connection_adapters/pool_config.rb +73 -0
  76. data/lib/active_record/connection_adapters/pool_manager.rb +47 -0
  77. data/lib/active_record/connection_adapters/postgresql/column.rb +24 -1
  78. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +21 -56
  79. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +0 -1
  80. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +3 -5
  81. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +2 -2
  82. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +10 -2
  83. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +0 -1
  84. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +0 -1
  85. data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +49 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -3
  87. data/lib/active_record/connection_adapters/postgresql/oid/macaddr.rb +25 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +2 -2
  89. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +1 -1
  90. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -3
  91. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +24 -6
  92. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +1 -1
  93. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +11 -2
  94. data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
  95. data/lib/active_record/connection_adapters/postgresql/quoting.rb +30 -4
  96. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +2 -2
  97. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +7 -3
  98. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +1 -1
  99. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +0 -1
  100. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +72 -54
  101. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +8 -0
  102. data/lib/active_record/connection_adapters/postgresql/utils.rb +0 -1
  103. data/lib/active_record/connection_adapters/postgresql_adapter.rb +80 -66
  104. data/lib/active_record/connection_adapters/schema_cache.rb +130 -15
  105. data/lib/active_record/connection_adapters/sql_type_metadata.rb +8 -0
  106. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +38 -12
  107. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +1 -2
  108. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +5 -1
  109. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +38 -5
  110. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +57 -57
  111. data/lib/active_record/connection_adapters/statement_pool.rb +0 -1
  112. data/lib/active_record/connection_adapters.rb +52 -0
  113. data/lib/active_record/connection_handling.rb +218 -87
  114. data/lib/active_record/core.rb +276 -68
  115. data/lib/active_record/counter_cache.rb +4 -1
  116. data/lib/active_record/database_configurations/connection_url_resolver.rb +99 -0
  117. data/lib/active_record/database_configurations/database_config.rb +52 -9
  118. data/lib/active_record/database_configurations/hash_config.rb +54 -8
  119. data/lib/active_record/database_configurations/url_config.rb +15 -41
  120. data/lib/active_record/database_configurations.rb +125 -85
  121. data/lib/active_record/delegated_type.rb +209 -0
  122. data/lib/active_record/destroy_association_async_job.rb +36 -0
  123. data/lib/active_record/dynamic_matchers.rb +2 -3
  124. data/lib/active_record/enum.rb +80 -38
  125. data/lib/active_record/errors.rb +47 -12
  126. data/lib/active_record/explain.rb +9 -5
  127. data/lib/active_record/explain_subscriber.rb +1 -1
  128. data/lib/active_record/fixture_set/file.rb +10 -17
  129. data/lib/active_record/fixture_set/model_metadata.rb +1 -2
  130. data/lib/active_record/fixture_set/render_context.rb +1 -1
  131. data/lib/active_record/fixture_set/table_row.rb +2 -3
  132. data/lib/active_record/fixture_set/table_rows.rb +0 -1
  133. data/lib/active_record/fixtures.rb +58 -12
  134. data/lib/active_record/gem_version.rb +3 -3
  135. data/lib/active_record/inheritance.rb +40 -21
  136. data/lib/active_record/insert_all.rb +42 -9
  137. data/lib/active_record/integration.rb +3 -5
  138. data/lib/active_record/internal_metadata.rb +18 -7
  139. data/lib/active_record/legacy_yaml_adapter.rb +7 -3
  140. data/lib/active_record/locking/optimistic.rb +33 -18
  141. data/lib/active_record/locking/pessimistic.rb +6 -2
  142. data/lib/active_record/log_subscriber.rb +28 -9
  143. data/lib/active_record/middleware/database_selector/resolver/session.rb +3 -0
  144. data/lib/active_record/middleware/database_selector/resolver.rb +6 -2
  145. data/lib/active_record/middleware/database_selector.rb +4 -2
  146. data/lib/active_record/migration/command_recorder.rb +53 -45
  147. data/lib/active_record/migration/compatibility.rb +75 -21
  148. data/lib/active_record/migration/join_table.rb +0 -1
  149. data/lib/active_record/migration.rb +115 -85
  150. data/lib/active_record/model_schema.rb +117 -15
  151. data/lib/active_record/nested_attributes.rb +2 -5
  152. data/lib/active_record/no_touching.rb +1 -1
  153. data/lib/active_record/null_relation.rb +0 -1
  154. data/lib/active_record/persistence.rb +50 -46
  155. data/lib/active_record/query_cache.rb +15 -5
  156. data/lib/active_record/querying.rb +12 -7
  157. data/lib/active_record/railtie.rb +65 -45
  158. data/lib/active_record/railties/console_sandbox.rb +2 -4
  159. data/lib/active_record/railties/databases.rake +280 -99
  160. data/lib/active_record/readonly_attributes.rb +4 -0
  161. data/lib/active_record/reflection.rb +77 -63
  162. data/lib/active_record/relation/batches/batch_enumerator.rb +25 -9
  163. data/lib/active_record/relation/batches.rb +38 -32
  164. data/lib/active_record/relation/calculations.rb +106 -45
  165. data/lib/active_record/relation/delegation.rb +9 -7
  166. data/lib/active_record/relation/finder_methods.rb +45 -16
  167. data/lib/active_record/relation/from_clause.rb +5 -1
  168. data/lib/active_record/relation/merger.rb +27 -26
  169. data/lib/active_record/relation/predicate_builder/array_handler.rb +8 -9
  170. data/lib/active_record/relation/predicate_builder/association_query_value.rb +4 -5
  171. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +10 -6
  172. data/lib/active_record/relation/predicate_builder/relation_handler.rb +1 -1
  173. data/lib/active_record/relation/predicate_builder.rb +59 -40
  174. data/lib/active_record/relation/query_methods.rb +341 -188
  175. data/lib/active_record/relation/record_fetch_warning.rb +3 -3
  176. data/lib/active_record/relation/spawn_methods.rb +8 -8
  177. data/lib/active_record/relation/where_clause.rb +111 -62
  178. data/lib/active_record/relation.rb +116 -83
  179. data/lib/active_record/result.rb +41 -34
  180. data/lib/active_record/runtime_registry.rb +2 -2
  181. data/lib/active_record/sanitization.rb +6 -17
  182. data/lib/active_record/schema_dumper.rb +34 -4
  183. data/lib/active_record/schema_migration.rb +2 -8
  184. data/lib/active_record/scoping/default.rb +1 -4
  185. data/lib/active_record/scoping/named.rb +7 -18
  186. data/lib/active_record/scoping.rb +0 -1
  187. data/lib/active_record/secure_token.rb +16 -8
  188. data/lib/active_record/serialization.rb +5 -3
  189. data/lib/active_record/signed_id.rb +116 -0
  190. data/lib/active_record/statement_cache.rb +20 -4
  191. data/lib/active_record/store.rb +9 -4
  192. data/lib/active_record/suppressor.rb +2 -2
  193. data/lib/active_record/table_metadata.rb +42 -36
  194. data/lib/active_record/tasks/database_tasks.rb +140 -113
  195. data/lib/active_record/tasks/mysql_database_tasks.rb +34 -36
  196. data/lib/active_record/tasks/postgresql_database_tasks.rb +24 -27
  197. data/lib/active_record/tasks/sqlite_database_tasks.rb +13 -10
  198. data/lib/active_record/test_databases.rb +5 -4
  199. data/lib/active_record/test_fixtures.rb +87 -20
  200. data/lib/active_record/timestamp.rb +4 -7
  201. data/lib/active_record/touch_later.rb +20 -21
  202. data/lib/active_record/transactions.rb +25 -72
  203. data/lib/active_record/type/adapter_specific_registry.rb +2 -5
  204. data/lib/active_record/type/hash_lookup_type_map.rb +0 -1
  205. data/lib/active_record/type/serialized.rb +6 -3
  206. data/lib/active_record/type/time.rb +10 -0
  207. data/lib/active_record/type/type_map.rb +0 -1
  208. data/lib/active_record/type/unsigned_integer.rb +0 -1
  209. data/lib/active_record/type.rb +8 -2
  210. data/lib/active_record/type_caster/connection.rb +0 -1
  211. data/lib/active_record/type_caster/map.rb +8 -5
  212. data/lib/active_record/validations/associated.rb +1 -2
  213. data/lib/active_record/validations/numericality.rb +35 -0
  214. data/lib/active_record/validations/uniqueness.rb +24 -4
  215. data/lib/active_record/validations.rb +3 -3
  216. data/lib/active_record.rb +7 -13
  217. data/lib/arel/attributes/attribute.rb +4 -0
  218. data/lib/arel/collectors/bind.rb +5 -0
  219. data/lib/arel/collectors/composite.rb +8 -0
  220. data/lib/arel/collectors/sql_string.rb +7 -0
  221. data/lib/arel/collectors/substitute_binds.rb +7 -0
  222. data/lib/arel/nodes/binary.rb +82 -8
  223. data/lib/arel/nodes/bind_param.rb +8 -0
  224. data/lib/arel/nodes/casted.rb +21 -9
  225. data/lib/arel/nodes/equality.rb +6 -9
  226. data/lib/arel/nodes/grouping.rb +3 -0
  227. data/lib/arel/nodes/homogeneous_in.rb +76 -0
  228. data/lib/arel/nodes/in.rb +8 -1
  229. data/lib/arel/nodes/infix_operation.rb +13 -1
  230. data/lib/arel/nodes/join_source.rb +1 -1
  231. data/lib/arel/nodes/node.rb +7 -6
  232. data/lib/arel/nodes/ordering.rb +27 -0
  233. data/lib/arel/nodes/sql_literal.rb +3 -0
  234. data/lib/arel/nodes/table_alias.rb +7 -3
  235. data/lib/arel/nodes/unary.rb +0 -1
  236. data/lib/arel/nodes.rb +3 -1
  237. data/lib/arel/predications.rb +17 -24
  238. data/lib/arel/select_manager.rb +1 -2
  239. data/lib/arel/table.rb +13 -5
  240. data/lib/arel/visitors/dot.rb +14 -3
  241. data/lib/arel/visitors/mysql.rb +11 -1
  242. data/lib/arel/visitors/postgresql.rb +15 -5
  243. data/lib/arel/visitors/sqlite.rb +0 -1
  244. data/lib/arel/visitors/to_sql.rb +89 -79
  245. data/lib/arel/visitors/visitor.rb +0 -1
  246. data/lib/arel/visitors.rb +0 -7
  247. data/lib/arel.rb +5 -9
  248. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +0 -1
  249. data/lib/rails/generators/active_record/migration/migration_generator.rb +1 -0
  250. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +2 -0
  251. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +4 -4
  252. data/lib/rails/generators/active_record/migration.rb +6 -2
  253. data/lib/rails/generators/active_record/model/model_generator.rb +38 -2
  254. data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +7 -0
  255. metadata +30 -29
  256. data/lib/active_record/attribute_decorators.rb +0 -90
  257. data/lib/active_record/connection_adapters/connection_specification.rb +0 -297
  258. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +0 -29
  259. data/lib/active_record/define_callbacks.rb +0 -22
  260. data/lib/active_record/railties/collection_cache_association_loading.rb +0 -34
  261. data/lib/active_record/relation/predicate_builder/base_handler.rb +0 -18
  262. data/lib/active_record/relation/where_clause_factory.rb +0 -33
  263. data/lib/arel/attributes.rb +0 -22
  264. data/lib/arel/visitors/depth_first.rb +0 -204
  265. data/lib/arel/visitors/ibm_db.rb +0 -34
  266. data/lib/arel/visitors/informix.rb +0 -62
  267. data/lib/arel/visitors/mssql.rb +0 -157
  268. data/lib/arel/visitors/oracle.rb +0 -159
  269. data/lib/arel/visitors/oracle12.rb +0 -66
  270. data/lib/arel/visitors/where_sql.rb +0 -23
@@ -1,12 +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"
4
6
 
5
7
  databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
6
8
 
7
9
  db_namespace = namespace :db do
8
10
  desc "Set the environment value for the database"
9
11
  task "environment:set" => :load_config do
12
+ raise ActiveRecord::EnvironmentStorageError unless ActiveRecord::InternalMetadata.enabled?
10
13
  ActiveRecord::InternalMetadata.create_table
11
14
  ActiveRecord::InternalMetadata[:environment] = ActiveRecord::Base.connection.migration_context.current_environment
12
15
  end
@@ -16,7 +19,10 @@ db_namespace = namespace :db do
16
19
  end
17
20
 
18
21
  task load_config: :environment do
19
- 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
+
20
26
  ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
21
27
  end
22
28
 
@@ -25,16 +31,16 @@ db_namespace = namespace :db do
25
31
  ActiveRecord::Tasks::DatabaseTasks.create_all
26
32
  end
27
33
 
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
+ 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)
33
39
  end
34
40
  end
35
41
  end
36
42
 
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."
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."
38
44
  task create: [:load_config] do
39
45
  ActiveRecord::Tasks::DatabaseTasks.create_current
40
46
  end
@@ -44,16 +50,16 @@ db_namespace = namespace :db do
44
50
  ActiveRecord::Tasks::DatabaseTasks.drop_all
45
51
  end
46
52
 
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)
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)
52
58
  end
53
59
  end
54
60
  end
55
61
 
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."
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."
57
63
  task drop: [:load_config, :check_protected_environments] do
58
64
  db_namespace["drop:_unsafe"].invoke
59
65
  end
@@ -73,47 +79,65 @@ db_namespace = namespace :db do
73
79
  ActiveRecord::Tasks::DatabaseTasks.truncate_all
74
80
  end
75
81
 
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."
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."
77
83
  task purge: [:load_config, :check_protected_environments] do
78
84
  ActiveRecord::Tasks::DatabaseTasks.purge_current
79
85
  end
80
86
 
81
87
  desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
82
88
  task migrate: :load_config do
89
+ original_db_config = ActiveRecord::Base.connection_db_config
83
90
  ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
84
- ActiveRecord::Base.establish_connection(db_config.config)
91
+ ActiveRecord::Base.establish_connection(db_config)
85
92
  ActiveRecord::Tasks::DatabaseTasks.migrate
86
93
  end
87
94
  db_namespace["_dump"].invoke
95
+ ensure
96
+ ActiveRecord::Base.establish_connection(original_db_config)
88
97
  end
89
98
 
90
99
  # IMPORTANT: This task won't dump the schema if ActiveRecord::Base.dump_schema_after_migration is set to false
91
100
  task :_dump do
92
101
  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
102
+ db_namespace["schema:dump"].invoke
99
103
  end
100
104
  # Allow this task to be called as many times as required. An example is the
101
105
  # migrate:redo task, which calls other two internally that depend on this one.
102
106
  db_namespace["_dump"].reenable
103
107
  end
104
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
+
105
123
  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)
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)
111
130
  ActiveRecord::Tasks::DatabaseTasks.migrate
131
+ db_namespace["_dump:#{name}"].invoke
132
+ ensure
133
+ ActiveRecord::Base.establish_connection(original_db_config)
112
134
  end
113
135
  end
114
136
 
115
- # desc 'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
137
+ desc "Rolls back the database one migration and re-migrates up (options: STEP=x, VERSION=x)."
116
138
  task redo: :load_config do
139
+ ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:migrate:redo")
140
+
117
141
  raise "Empty VERSION provided" if ENV["VERSION"] && ENV["VERSION"].empty?
118
142
 
119
143
  if ENV["VERSION"]
@@ -125,10 +149,27 @@ db_namespace = namespace :db do
125
149
  end
126
150
  end
127
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
+
128
169
  # desc 'Resets your database using your migrations for the current environment'
129
170
  task reset: ["db:drop", "db:create", "db:migrate"]
130
171
 
131
- # desc 'Runs the "up" for a given migration VERSION.'
172
+ desc 'Runs the "up" for a given migration VERSION.'
132
173
  task up: :load_config do
133
174
  ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:migrate:up")
134
175
 
@@ -144,13 +185,13 @@ db_namespace = namespace :db do
144
185
  end
145
186
 
146
187
  namespace :up do
147
- ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
148
- task spec_name => :load_config do
188
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
189
+ task name => :load_config do
149
190
  raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
150
191
 
151
- db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
192
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
152
193
 
153
- ActiveRecord::Base.establish_connection(db_config.config)
194
+ ActiveRecord::Base.establish_connection(db_config)
154
195
  ActiveRecord::Tasks::DatabaseTasks.check_target_version
155
196
  ActiveRecord::Base.connection.migration_context.run(
156
197
  :up,
@@ -162,7 +203,7 @@ db_namespace = namespace :db do
162
203
  end
163
204
  end
164
205
 
165
- # desc 'Runs the "down" for a given migration VERSION.'
206
+ desc 'Runs the "down" for a given migration VERSION.'
166
207
  task down: :load_config do
167
208
  ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:migrate:down")
168
209
 
@@ -178,13 +219,13 @@ db_namespace = namespace :db do
178
219
  end
179
220
 
180
221
  namespace :down do
181
- ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |spec_name|
182
- task spec_name => :load_config do
222
+ ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
223
+ task name => :load_config do
183
224
  raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
184
225
 
185
- db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name)
226
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
186
227
 
187
- ActiveRecord::Base.establish_connection(db_config.config)
228
+ ActiveRecord::Base.establish_connection(db_config)
188
229
  ActiveRecord::Tasks::DatabaseTasks.check_target_version
189
230
  ActiveRecord::Base.connection.migration_context.run(
190
231
  :down,
@@ -199,27 +240,46 @@ db_namespace = namespace :db do
199
240
  desc "Display status of migrations"
200
241
  task status: :load_config do
201
242
  ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
202
- ActiveRecord::Base.establish_connection(db_config.config)
243
+ ActiveRecord::Base.establish_connection(db_config)
203
244
  ActiveRecord::Tasks::DatabaseTasks.migrate_status
204
245
  end
205
246
  end
206
247
 
207
248
  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)
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)
213
254
  ActiveRecord::Tasks::DatabaseTasks.migrate_status
214
255
  end
215
256
  end
216
257
  end
217
258
  end
218
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)
266
+
267
+ ActiveRecord::Base.establish_connection(db_config)
268
+ ActiveRecord::Base.connection.migration_context.rollback(step)
269
+
270
+ db_namespace["_dump"].invoke
271
+ end
272
+ end
273
+ end
274
+
219
275
  desc "Rolls the schema back to the previous version (specify steps w/ STEP=n)."
220
276
  task rollback: :load_config do
277
+ ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:rollback")
278
+
221
279
  step = ENV["STEP"] ? ENV["STEP"].to_i : 1
280
+
222
281
  ActiveRecord::Base.connection.migration_context.rollback(step)
282
+
223
283
  db_namespace["_dump"].invoke
224
284
  end
225
285
 
@@ -230,7 +290,7 @@ db_namespace = namespace :db do
230
290
  db_namespace["_dump"].invoke
231
291
  end
232
292
 
233
- # 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."
234
294
  task reset: [ "db:drop", "db:setup" ]
235
295
 
236
296
  # desc "Retrieves the charset for the current environment's database"
@@ -253,7 +313,7 @@ db_namespace = namespace :db do
253
313
  # desc "Raises an error if there are pending migrations"
254
314
  task abort_if_pending_migrations: :load_config do
255
315
  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)
316
+ ActiveRecord::Base.establish_connection(db_config)
257
317
 
258
318
  ActiveRecord::Base.connection.migration_context.open.pending_migrations
259
319
  end
@@ -263,18 +323,18 @@ db_namespace = namespace :db do
263
323
  pending_migrations.each do |pending_migration|
264
324
  puts " %4d %s" % [pending_migration.version, pending_migration.name]
265
325
  end
266
- 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.}
267
327
  end
268
328
  ensure
269
329
  ActiveRecord::Base.establish_connection(ActiveRecord::Tasks::DatabaseTasks.env.to_sym)
270
330
  end
271
331
 
272
332
  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)
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)
278
338
 
279
339
  pending_migrations = ActiveRecord::Base.connection.migration_context.open.pending_migrations
280
340
 
@@ -283,37 +343,41 @@ db_namespace = namespace :db do
283
343
  pending_migrations.each do |pending_migration|
284
344
  puts " %4d %s" % [pending_migration.version, pending_migration.name]
285
345
  end
286
- abort %{Run `rails db:migrate:#{spec_name}` to update your database then try again.}
346
+ abort %{Run `bin/rails db:migrate:#{name}` to update your database then try again.}
287
347
  end
288
348
  end
289
349
  end
290
350
  end
291
351
 
292
352
  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]
353
+ task setup: ["db:create", :environment, "db:schema:load", :seed]
294
354
 
295
355
  desc "Runs setup if database does not exist, or runs migrations if it does"
296
356
  task prepare: :load_config do
297
357
  seed = false
298
358
 
299
359
  ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
300
- ActiveRecord::Base.establish_connection(db_config.config)
360
+ ActiveRecord::Base.establish_connection(db_config)
301
361
 
302
362
  # Skipped when no database
303
363
  ActiveRecord::Tasks::DatabaseTasks.migrate
364
+
304
365
  if ActiveRecord::Base.dump_schema_after_migration
305
- ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config.config, ActiveRecord::Base.schema_format, db_config.spec_name)
366
+ ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, ActiveRecord::Base.schema_format)
306
367
  end
307
-
308
368
  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
- )
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
317
381
 
318
382
  seed = true
319
383
  end
@@ -334,7 +398,7 @@ db_namespace = namespace :db do
334
398
  end
335
399
 
336
400
  namespace :fixtures do
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."
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."
338
402
  task load: :load_config do
339
403
  require "active_record/fixtures"
340
404
 
@@ -349,14 +413,15 @@ db_namespace = namespace :db do
349
413
  fixture_files = if ENV["FIXTURES"]
350
414
  ENV["FIXTURES"].split(",")
351
415
  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] }
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("/") }
354
419
  end
355
420
 
356
421
  ActiveRecord::FixtureSet.create_fixtures(fixtures_dir, fixture_files)
357
422
  end
358
423
 
359
- # 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."
360
425
  task identify: :load_config do
361
426
  require "active_record/fixtures"
362
427
 
@@ -368,7 +433,7 @@ db_namespace = namespace :db do
368
433
  base_dir = ActiveRecord::Tasks::DatabaseTasks.fixtures_path
369
434
 
370
435
  Dir["#{base_dir}/**/*.yml"].each do |file|
371
- if data = YAML.load(ERB.new(IO.read(file)).result)
436
+ if data = ActiveSupport::ConfigurationFile.parse(file)
372
437
  data.each_key do |key|
373
438
  key_id = ActiveRecord::FixtureSet.identify(key)
374
439
 
@@ -382,31 +447,60 @@ db_namespace = namespace :db do
382
447
  end
383
448
 
384
449
  namespace :schema do
385
- 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`)"
386
451
  task dump: :load_config do
387
452
  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)
453
+ ActiveRecord::Base.establish_connection(db_config)
454
+ ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config)
390
455
  end
391
456
 
392
457
  db_namespace["schema:dump"].reenable
393
458
  end
394
459
 
395
- 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"
396
461
  task load: [:load_config, :check_protected_environments] do
397
- ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV["SCHEMA"])
462
+ ActiveRecord::Tasks::DatabaseTasks.load_schema_current(ActiveRecord::Base.schema_format, ENV["SCHEMA"])
398
463
  end
399
464
 
400
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
401
470
  db_namespace["schema:load"].invoke if ActiveRecord::Base.schema_format == :ruby
402
471
  end
403
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
+
404
495
  namespace :cache do
405
496
  desc "Creates a db/schema_cache.yml file."
406
497
  task dump: :load_config do
407
498
  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)
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
+ )
410
504
  ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(
411
505
  ActiveRecord::Base.connection,
412
506
  filename,
@@ -417,8 +511,13 @@ db_namespace = namespace :db do
417
511
  desc "Clears a db/schema_cache.yml file."
418
512
  task clear: :load_config do
419
513
  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
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
+ )
422
521
  end
423
522
  end
424
523
  end
@@ -427,61 +526,93 @@ db_namespace = namespace :db do
427
526
  namespace :structure do
428
527
  desc "Dumps the database structure to db/structure.sql. Specify another file with SCHEMA=db/my_structure.sql"
429
528
  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)
433
- 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
434
533
 
534
+ db_namespace["schema:dump"].invoke
435
535
  db_namespace["structure:dump"].reenable
436
536
  end
437
537
 
438
538
  desc "Recreates the databases from the structure.sql file"
439
539
  task load: [:load_config, :check_protected_environments] do
440
- 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
441
545
  end
442
546
 
443
547
  task load_if_sql: ["db:create", :environment] do
444
- 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
445
580
  end
446
581
  end
447
582
 
448
583
  namespace :test do
449
584
  # desc "Recreate the test database from the current schema"
450
585
  task load: %w(db:test:purge) do
451
- case ActiveRecord::Base.schema_format
452
- when :ruby
453
- db_namespace["test:load_schema"].invoke
454
- when :sql
455
- db_namespace["test:load_structure"].invoke
456
- end
586
+ db_namespace["test:load_schema"].invoke
457
587
  end
458
588
 
459
- # 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`)"
460
590
  task load_schema: %w(db:test:purge) do
461
591
  should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
462
592
  ActiveRecord::Schema.verbose = false
463
593
  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")
594
+ filename = ActiveRecord::Tasks::DatabaseTasks.dump_filename(db_config.name)
595
+ ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, ActiveRecord::Base.schema_format, filename)
466
596
  end
467
597
  ensure
468
598
  if should_reconnect
469
- ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations.default_hash(ActiveRecord::Tasks::DatabaseTasks.env))
599
+ ActiveRecord::Base.establish_connection(ActiveRecord::Tasks::DatabaseTasks.env.to_sym)
470
600
  end
471
601
  end
472
602
 
473
603
  # desc "Recreate the test database from an existent structure.sql file"
474
604
  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
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
479
610
  end
480
611
 
481
612
  # desc "Empty the test database"
482
613
  task purge: %w(load_config check_protected_environments) do
483
614
  ActiveRecord::Base.configurations.configs_for(env_name: "test").each do |db_config|
484
- ActiveRecord::Tasks::DatabaseTasks.purge(db_config.config)
615
+ ActiveRecord::Tasks::DatabaseTasks.purge(db_config)
485
616
  end
486
617
  end
487
618
 
@@ -491,6 +622,56 @@ db_namespace = namespace :db do
491
622
  db_namespace["test:load"].invoke
492
623
  end
493
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
494
675
  end
495
676
  end
496
677