activerecord 6.0.3 → 6.1.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

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