activerecord 7.0.8.6 → 7.2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +631 -1939
- data/MIT-LICENSE +1 -1
- data/README.rdoc +29 -29
- data/examples/performance.rb +2 -2
- data/lib/active_record/aggregations.rb +16 -13
- data/lib/active_record/association_relation.rb +2 -2
- data/lib/active_record/associations/alias_tracker.rb +25 -19
- data/lib/active_record/associations/association.rb +35 -12
- data/lib/active_record/associations/association_scope.rb +16 -9
- data/lib/active_record/associations/belongs_to_association.rb +23 -8
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +3 -2
- data/lib/active_record/associations/builder/association.rb +3 -3
- data/lib/active_record/associations/builder/belongs_to.rb +22 -8
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -7
- data/lib/active_record/associations/builder/has_many.rb +3 -4
- data/lib/active_record/associations/builder/has_one.rb +3 -4
- data/lib/active_record/associations/builder/singular_association.rb +4 -0
- data/lib/active_record/associations/collection_association.rb +26 -14
- data/lib/active_record/associations/collection_proxy.rb +29 -11
- data/lib/active_record/associations/errors.rb +265 -0
- data/lib/active_record/associations/foreign_association.rb +10 -3
- data/lib/active_record/associations/has_many_association.rb +21 -14
- data/lib/active_record/associations/has_many_through_association.rb +17 -7
- data/lib/active_record/associations/has_one_association.rb +10 -3
- data/lib/active_record/associations/join_dependency/join_association.rb +30 -27
- data/lib/active_record/associations/join_dependency.rb +10 -10
- data/lib/active_record/associations/nested_error.rb +47 -0
- data/lib/active_record/associations/preloader/association.rb +33 -8
- data/lib/active_record/associations/preloader/branch.rb +7 -1
- data/lib/active_record/associations/preloader/through_association.rb +1 -3
- data/lib/active_record/associations/preloader.rb +13 -10
- data/lib/active_record/associations/singular_association.rb +7 -1
- data/lib/active_record/associations/through_association.rb +22 -11
- data/lib/active_record/associations.rb +354 -485
- data/lib/active_record/attribute_assignment.rb +0 -4
- data/lib/active_record/attribute_methods/before_type_cast.rb +17 -0
- data/lib/active_record/attribute_methods/composite_primary_key.rb +84 -0
- data/lib/active_record/attribute_methods/dirty.rb +53 -35
- data/lib/active_record/attribute_methods/primary_key.rb +45 -25
- data/lib/active_record/attribute_methods/query.rb +28 -16
- data/lib/active_record/attribute_methods/read.rb +8 -7
- data/lib/active_record/attribute_methods/serialization.rb +131 -32
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +11 -6
- data/lib/active_record/attribute_methods/write.rb +6 -6
- data/lib/active_record/attribute_methods.rb +148 -33
- data/lib/active_record/attributes.rb +64 -50
- data/lib/active_record/autosave_association.rb +69 -37
- data/lib/active_record/base.rb +9 -5
- data/lib/active_record/callbacks.rb +11 -25
- data/lib/active_record/coders/column_serializer.rb +61 -0
- data/lib/active_record/coders/json.rb +1 -1
- data/lib/active_record/coders/yaml_column.rb +70 -42
- data/lib/active_record/connection_adapters/abstract/connection_handler.rb +123 -131
- data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +2 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +4 -1
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +323 -88
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +160 -45
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +217 -63
- data/lib/active_record/connection_adapters/abstract/quoting.rb +72 -63
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +18 -4
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +137 -11
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +307 -129
- data/lib/active_record/connection_adapters/abstract/transaction.rb +367 -75
- data/lib/active_record/connection_adapters/abstract_adapter.rb +510 -111
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +278 -125
- data/lib/active_record/connection_adapters/column.rb +9 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +26 -139
- data/lib/active_record/connection_adapters/mysql/quoting.rb +53 -54
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +6 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +1 -1
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +25 -13
- data/lib/active_record/connection_adapters/mysql2/database_statements.rb +152 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +101 -68
- data/lib/active_record/connection_adapters/pool_config.rb +20 -10
- data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
- data/lib/active_record/connection_adapters/postgresql/column.rb +14 -3
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +100 -43
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +6 -0
- data/lib/active_record/connection_adapters/postgresql/oid/interval.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +11 -2
- data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +14 -4
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +65 -61
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +3 -9
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -6
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +151 -2
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +53 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +370 -63
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +367 -201
- data/lib/active_record/connection_adapters/schema_cache.rb +302 -79
- data/lib/active_record/connection_adapters/sqlite3/column.rb +62 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +60 -43
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +45 -46
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +22 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +14 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +16 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +50 -8
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +290 -110
- data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
- data/lib/active_record/connection_adapters/trilogy/database_statements.rb +99 -0
- data/lib/active_record/connection_adapters/trilogy_adapter.rb +229 -0
- data/lib/active_record/connection_adapters.rb +124 -1
- data/lib/active_record/connection_handling.rb +96 -104
- data/lib/active_record/core.rb +251 -176
- data/lib/active_record/counter_cache.rb +68 -34
- data/lib/active_record/database_configurations/connection_url_resolver.rb +8 -3
- data/lib/active_record/database_configurations/database_config.rb +26 -5
- data/lib/active_record/database_configurations/hash_config.rb +52 -34
- data/lib/active_record/database_configurations/url_config.rb +37 -12
- data/lib/active_record/database_configurations.rb +87 -34
- data/lib/active_record/delegated_type.rb +39 -10
- data/lib/active_record/deprecator.rb +7 -0
- data/lib/active_record/destroy_association_async_job.rb +3 -1
- data/lib/active_record/dynamic_matchers.rb +2 -2
- data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
- data/lib/active_record/encryption/cipher/aes256_gcm.rb +4 -1
- data/lib/active_record/encryption/config.rb +25 -1
- data/lib/active_record/encryption/configurable.rb +12 -19
- data/lib/active_record/encryption/context.rb +10 -3
- data/lib/active_record/encryption/contexts.rb +5 -1
- data/lib/active_record/encryption/derived_secret_key_provider.rb +8 -2
- data/lib/active_record/encryption/encryptable_record.rb +45 -21
- data/lib/active_record/encryption/encrypted_attribute_type.rb +47 -12
- data/lib/active_record/encryption/encryptor.rb +18 -3
- data/lib/active_record/encryption/extended_deterministic_queries.rb +66 -69
- data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +3 -3
- data/lib/active_record/encryption/key_generator.rb +12 -1
- data/lib/active_record/encryption/key_provider.rb +1 -1
- data/lib/active_record/encryption/message_pack_message_serializer.rb +76 -0
- data/lib/active_record/encryption/message_serializer.rb +6 -0
- data/lib/active_record/encryption/null_encryptor.rb +4 -0
- data/lib/active_record/encryption/properties.rb +3 -3
- data/lib/active_record/encryption/read_only_null_encryptor.rb +4 -0
- data/lib/active_record/encryption/scheme.rb +22 -21
- data/lib/active_record/encryption.rb +3 -0
- data/lib/active_record/enum.rb +129 -28
- data/lib/active_record/errors.rb +151 -31
- data/lib/active_record/explain.rb +21 -12
- data/lib/active_record/fixture_set/model_metadata.rb +14 -4
- data/lib/active_record/fixture_set/render_context.rb +2 -0
- data/lib/active_record/fixture_set/table_row.rb +29 -8
- data/lib/active_record/fixtures.rb +167 -97
- data/lib/active_record/future_result.rb +47 -8
- data/lib/active_record/gem_version.rb +4 -4
- data/lib/active_record/inheritance.rb +34 -18
- data/lib/active_record/insert_all.rb +72 -22
- data/lib/active_record/integration.rb +11 -8
- data/lib/active_record/internal_metadata.rb +124 -20
- data/lib/active_record/locking/optimistic.rb +8 -7
- data/lib/active_record/locking/pessimistic.rb +5 -2
- data/lib/active_record/log_subscriber.rb +18 -22
- data/lib/active_record/marshalling.rb +59 -0
- data/lib/active_record/message_pack.rb +124 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +4 -0
- data/lib/active_record/middleware/database_selector.rb +6 -8
- data/lib/active_record/middleware/shard_selector.rb +3 -1
- data/lib/active_record/migration/command_recorder.rb +106 -8
- data/lib/active_record/migration/compatibility.rb +147 -5
- data/lib/active_record/migration/default_strategy.rb +22 -0
- data/lib/active_record/migration/execution_strategy.rb +19 -0
- data/lib/active_record/migration/pending_migration_connection.rb +21 -0
- data/lib/active_record/migration.rb +234 -117
- data/lib/active_record/model_schema.rb +90 -102
- data/lib/active_record/nested_attributes.rb +48 -11
- data/lib/active_record/normalization.rb +163 -0
- data/lib/active_record/persistence.rb +168 -339
- data/lib/active_record/promise.rb +84 -0
- data/lib/active_record/query_cache.rb +18 -25
- data/lib/active_record/query_logs.rb +92 -52
- data/lib/active_record/query_logs_formatter.rb +41 -0
- data/lib/active_record/querying.rb +33 -8
- data/lib/active_record/railtie.rb +129 -85
- data/lib/active_record/railties/controller_runtime.rb +22 -7
- data/lib/active_record/railties/databases.rake +145 -154
- data/lib/active_record/railties/job_runtime.rb +23 -0
- data/lib/active_record/readonly_attributes.rb +32 -5
- data/lib/active_record/reflection.rb +267 -69
- data/lib/active_record/relation/batches/batch_enumerator.rb +20 -5
- data/lib/active_record/relation/batches.rb +198 -63
- data/lib/active_record/relation/calculations.rb +250 -93
- data/lib/active_record/relation/delegation.rb +30 -19
- data/lib/active_record/relation/finder_methods.rb +93 -18
- data/lib/active_record/relation/merger.rb +6 -6
- data/lib/active_record/relation/predicate_builder/array_handler.rb +2 -2
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +18 -3
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +10 -7
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
- data/lib/active_record/relation/predicate_builder.rb +28 -16
- data/lib/active_record/relation/query_attribute.rb +2 -1
- data/lib/active_record/relation/query_methods.rb +576 -107
- data/lib/active_record/relation/record_fetch_warning.rb +3 -0
- data/lib/active_record/relation/spawn_methods.rb +5 -4
- data/lib/active_record/relation/where_clause.rb +7 -19
- data/lib/active_record/relation.rb +580 -90
- data/lib/active_record/result.rb +49 -48
- data/lib/active_record/runtime_registry.rb +63 -1
- data/lib/active_record/sanitization.rb +70 -25
- data/lib/active_record/schema.rb +8 -7
- data/lib/active_record/schema_dumper.rb +63 -14
- data/lib/active_record/schema_migration.rb +75 -24
- data/lib/active_record/scoping/default.rb +15 -5
- data/lib/active_record/scoping/named.rb +3 -2
- data/lib/active_record/scoping.rb +2 -1
- data/lib/active_record/secure_password.rb +60 -0
- data/lib/active_record/secure_token.rb +21 -3
- data/lib/active_record/signed_id.rb +27 -6
- data/lib/active_record/statement_cache.rb +7 -7
- data/lib/active_record/store.rb +8 -8
- data/lib/active_record/suppressor.rb +3 -1
- data/lib/active_record/table_metadata.rb +1 -1
- data/lib/active_record/tasks/database_tasks.rb +190 -118
- data/lib/active_record/tasks/mysql_database_tasks.rb +15 -6
- data/lib/active_record/tasks/postgresql_database_tasks.rb +16 -13
- data/lib/active_record/tasks/sqlite_database_tasks.rb +16 -7
- data/lib/active_record/test_fixtures.rb +170 -155
- data/lib/active_record/testing/query_assertions.rb +121 -0
- data/lib/active_record/timestamp.rb +31 -17
- data/lib/active_record/token_for.rb +123 -0
- data/lib/active_record/touch_later.rb +12 -7
- data/lib/active_record/transaction.rb +132 -0
- data/lib/active_record/transactions.rb +106 -24
- data/lib/active_record/translation.rb +0 -2
- data/lib/active_record/type/adapter_specific_registry.rb +1 -8
- data/lib/active_record/type/internal/timezone.rb +7 -2
- data/lib/active_record/type/serialized.rb +1 -3
- data/lib/active_record/type/time.rb +4 -0
- data/lib/active_record/type_caster/connection.rb +4 -4
- data/lib/active_record/validations/absence.rb +1 -1
- data/lib/active_record/validations/associated.rb +9 -3
- data/lib/active_record/validations/numericality.rb +5 -4
- data/lib/active_record/validations/presence.rb +5 -28
- data/lib/active_record/validations/uniqueness.rb +61 -11
- data/lib/active_record/validations.rb +12 -5
- data/lib/active_record/version.rb +1 -1
- data/lib/active_record.rb +247 -33
- data/lib/arel/alias_predication.rb +1 -1
- data/lib/arel/collectors/bind.rb +2 -0
- data/lib/arel/collectors/composite.rb +7 -0
- data/lib/arel/collectors/sql_string.rb +1 -1
- data/lib/arel/collectors/substitute_binds.rb +1 -1
- data/lib/arel/errors.rb +10 -0
- data/lib/arel/factory_methods.rb +4 -0
- data/lib/arel/nodes/binary.rb +6 -7
- data/lib/arel/nodes/bound_sql_literal.rb +65 -0
- data/lib/arel/nodes/cte.rb +36 -0
- data/lib/arel/nodes/fragments.rb +35 -0
- data/lib/arel/nodes/homogeneous_in.rb +1 -9
- data/lib/arel/nodes/leading_join.rb +8 -0
- data/lib/arel/nodes/{and.rb → nary.rb} +5 -2
- data/lib/arel/nodes/node.rb +115 -5
- data/lib/arel/nodes/sql_literal.rb +13 -0
- data/lib/arel/nodes/table_alias.rb +4 -0
- data/lib/arel/nodes.rb +6 -2
- data/lib/arel/predications.rb +3 -1
- data/lib/arel/select_manager.rb +1 -1
- data/lib/arel/table.rb +9 -5
- data/lib/arel/tree_manager.rb +8 -3
- data/lib/arel/update_manager.rb +2 -1
- data/lib/arel/visitors/dot.rb +1 -0
- data/lib/arel/visitors/mysql.rb +17 -5
- data/lib/arel/visitors/postgresql.rb +1 -12
- data/lib/arel/visitors/sqlite.rb +25 -0
- data/lib/arel/visitors/to_sql.rb +112 -34
- data/lib/arel/visitors/visitor.rb +2 -2
- data/lib/arel.rb +21 -3
- data/lib/rails/generators/active_record/application_record/USAGE +8 -0
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +4 -1
- data/lib/rails/generators/active_record/migration.rb +3 -1
- data/lib/rails/generators/active_record/model/USAGE +113 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
- metadata +56 -14
- data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
- data/lib/active_record/null_relation.rb +0 -63
@@ -9,9 +9,10 @@ databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
|
|
9
9
|
db_namespace = namespace :db do
|
10
10
|
desc "Set the environment value for the database"
|
11
11
|
task "environment:set" => :load_config do
|
12
|
-
|
13
|
-
ActiveRecord::
|
14
|
-
|
12
|
+
pool = ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool
|
13
|
+
raise ActiveRecord::EnvironmentStorageError unless pool.internal_metadata.enabled?
|
14
|
+
|
15
|
+
pool.internal_metadata.create_table_and_set_flags(pool.migration_context.current_environment)
|
15
16
|
end
|
16
17
|
|
17
18
|
task check_protected_environments: :load_config do
|
@@ -40,7 +41,7 @@ db_namespace = namespace :db do
|
|
40
41
|
end
|
41
42
|
end
|
42
43
|
|
43
|
-
desc "
|
44
|
+
desc "Create 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."
|
44
45
|
task create: [:load_config] do
|
45
46
|
ActiveRecord::Tasks::DatabaseTasks.create_current
|
46
47
|
end
|
@@ -59,7 +60,7 @@ db_namespace = namespace :db do
|
|
59
60
|
end
|
60
61
|
end
|
61
62
|
|
62
|
-
desc "
|
63
|
+
desc "Drop 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."
|
63
64
|
task drop: [:load_config, :check_protected_environments] do
|
64
65
|
db_namespace["drop:_unsafe"].invoke
|
65
66
|
end
|
@@ -88,23 +89,21 @@ db_namespace = namespace :db do
|
|
88
89
|
task migrate: :load_config do
|
89
90
|
db_configs = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env)
|
90
91
|
|
91
|
-
if db_configs.size == 1
|
92
|
+
if db_configs.size == 1 && db_configs.first.primary?
|
92
93
|
ActiveRecord::Tasks::DatabaseTasks.migrate
|
93
94
|
else
|
94
|
-
|
95
|
-
mapped_versions = ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions(db_configs)
|
95
|
+
mapped_versions = ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions
|
96
96
|
|
97
97
|
mapped_versions.sort.each do |version, db_configs|
|
98
98
|
db_configs.each do |db_config|
|
99
|
-
ActiveRecord::
|
100
|
-
|
99
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection(db_config) do
|
100
|
+
ActiveRecord::Tasks::DatabaseTasks.migrate(version)
|
101
|
+
end
|
101
102
|
end
|
102
103
|
end
|
103
104
|
end
|
104
105
|
|
105
106
|
db_namespace["_dump"].invoke
|
106
|
-
ensure
|
107
|
-
ActiveRecord::Base.establish_connection(original_db_config) if original_db_config
|
108
107
|
end
|
109
108
|
|
110
109
|
# IMPORTANT: This task won't dump the schema if ActiveRecord.dump_schema_after_migration is set to false
|
@@ -121,10 +120,7 @@ db_namespace = namespace :db do
|
|
121
120
|
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
122
121
|
# IMPORTANT: This task won't dump the schema if ActiveRecord.dump_schema_after_migration is set to false
|
123
122
|
task name do
|
124
|
-
|
125
|
-
|
126
|
-
if ActiveRecord.dump_schema_after_migration && db_config.schema_dump
|
127
|
-
ActiveRecord::Base.establish_connection(db_config)
|
123
|
+
if ActiveRecord.dump_schema_after_migration
|
128
124
|
db_namespace["schema:dump:#{name}"].invoke
|
129
125
|
end
|
130
126
|
|
@@ -139,17 +135,15 @@ db_namespace = namespace :db do
|
|
139
135
|
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
140
136
|
desc "Migrate #{name} database for current environment"
|
141
137
|
task name => :load_config do
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
138
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: Rails.env, name: name) do
|
139
|
+
ActiveRecord::Tasks::DatabaseTasks.migrate
|
140
|
+
end
|
141
|
+
|
146
142
|
db_namespace["_dump:#{name}"].invoke
|
147
|
-
ensure
|
148
|
-
ActiveRecord::Base.establish_connection(original_db_config)
|
149
143
|
end
|
150
144
|
end
|
151
145
|
|
152
|
-
desc "
|
146
|
+
desc "Roll back the database one migration and re-migrate up (options: STEP=x, VERSION=x)."
|
153
147
|
task redo: :load_config do
|
154
148
|
ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:migrate:redo")
|
155
149
|
|
@@ -166,7 +160,7 @@ db_namespace = namespace :db do
|
|
166
160
|
|
167
161
|
namespace :redo do
|
168
162
|
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
169
|
-
desc "
|
163
|
+
desc "Roll back #{name} database one migration and re-migrate up (options: STEP=x, VERSION=x)."
|
170
164
|
task name => :load_config do
|
171
165
|
raise "Empty VERSION provided" if ENV["VERSION"] && ENV["VERSION"].empty?
|
172
166
|
|
@@ -184,7 +178,7 @@ db_namespace = namespace :db do
|
|
184
178
|
# desc 'Resets your database using your migrations for the current environment'
|
185
179
|
task reset: ["db:drop", "db:create", "db:migrate"]
|
186
180
|
|
187
|
-
desc '
|
181
|
+
desc 'Run the "up" for a given migration VERSION.'
|
188
182
|
task up: :load_config do
|
189
183
|
ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:migrate:up")
|
190
184
|
|
@@ -192,7 +186,7 @@ db_namespace = namespace :db do
|
|
192
186
|
|
193
187
|
ActiveRecord::Tasks::DatabaseTasks.check_target_version
|
194
188
|
|
195
|
-
ActiveRecord::
|
189
|
+
ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool.migration_context.run(
|
196
190
|
:up,
|
197
191
|
ActiveRecord::Tasks::DatabaseTasks.target_version
|
198
192
|
)
|
@@ -201,24 +195,21 @@ db_namespace = namespace :db do
|
|
201
195
|
|
202
196
|
namespace :up do
|
203
197
|
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
198
|
+
desc "Run the \"up\" on #{name} database for a given migration VERSION."
|
204
199
|
task name => :load_config do
|
205
200
|
raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
|
206
201
|
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
ActiveRecord::Base.connection.migration_context.run(
|
212
|
-
:up,
|
213
|
-
ActiveRecord::Tasks::DatabaseTasks.target_version
|
214
|
-
)
|
202
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: Rails.env, name: name) do |pool|
|
203
|
+
ActiveRecord::Tasks::DatabaseTasks.check_target_version
|
204
|
+
pool.migration_context.run(:up, ActiveRecord::Tasks::DatabaseTasks.target_version)
|
205
|
+
end
|
215
206
|
|
216
|
-
db_namespace["_dump"].invoke
|
207
|
+
db_namespace["_dump:#{name}"].invoke
|
217
208
|
end
|
218
209
|
end
|
219
210
|
end
|
220
211
|
|
221
|
-
desc '
|
212
|
+
desc 'Run the "down" for a given migration VERSION.'
|
222
213
|
task down: :load_config do
|
223
214
|
ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:migrate:down")
|
224
215
|
|
@@ -226,7 +217,7 @@ db_namespace = namespace :db do
|
|
226
217
|
|
227
218
|
ActiveRecord::Tasks::DatabaseTasks.check_target_version
|
228
219
|
|
229
|
-
ActiveRecord::
|
220
|
+
ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool.migration_context.run(
|
230
221
|
:down,
|
231
222
|
ActiveRecord::Tasks::DatabaseTasks.target_version
|
232
223
|
)
|
@@ -235,27 +226,23 @@ db_namespace = namespace :db do
|
|
235
226
|
|
236
227
|
namespace :down do
|
237
228
|
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
229
|
+
desc "Run the \"down\" on #{name} database for a given migration VERSION."
|
238
230
|
task name => :load_config do
|
239
231
|
raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
|
240
232
|
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
ActiveRecord::Base.connection.migration_context.run(
|
246
|
-
:down,
|
247
|
-
ActiveRecord::Tasks::DatabaseTasks.target_version
|
248
|
-
)
|
233
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: Rails.env, name: name) do |pool|
|
234
|
+
ActiveRecord::Tasks::DatabaseTasks.check_target_version
|
235
|
+
pool.migration_context.run(:down, ActiveRecord::Tasks::DatabaseTasks.target_version)
|
236
|
+
end
|
249
237
|
|
250
|
-
db_namespace["_dump"].invoke
|
238
|
+
db_namespace["_dump:#{name}"].invoke
|
251
239
|
end
|
252
240
|
end
|
253
241
|
end
|
254
242
|
|
255
243
|
desc "Display status of migrations"
|
256
244
|
task status: :load_config do
|
257
|
-
ActiveRecord::
|
258
|
-
ActiveRecord::Base.establish_connection(db_config)
|
245
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each do
|
259
246
|
ActiveRecord::Tasks::DatabaseTasks.migrate_status
|
260
247
|
end
|
261
248
|
end
|
@@ -264,9 +251,9 @@ db_namespace = namespace :db do
|
|
264
251
|
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
265
252
|
desc "Display status of migrations for #{name} database"
|
266
253
|
task name => :load_config do
|
267
|
-
|
268
|
-
|
269
|
-
|
254
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: Rails.env, name: name) do
|
255
|
+
ActiveRecord::Tasks::DatabaseTasks.migrate_status
|
256
|
+
end
|
270
257
|
end
|
271
258
|
end
|
272
259
|
end
|
@@ -277,24 +264,24 @@ db_namespace = namespace :db do
|
|
277
264
|
desc "Rollback #{name} database for current environment (specify steps w/ STEP=n)."
|
278
265
|
task name => :load_config do
|
279
266
|
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
|
280
|
-
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
|
281
267
|
|
282
|
-
ActiveRecord::
|
283
|
-
|
268
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: Rails.env, name: name) do |pool|
|
269
|
+
pool.migration_context.rollback(step)
|
270
|
+
end
|
284
271
|
|
285
|
-
db_namespace["_dump"].invoke
|
272
|
+
db_namespace["_dump:#{name}"].invoke
|
286
273
|
end
|
287
274
|
end
|
288
275
|
end
|
289
276
|
|
290
|
-
desc "
|
277
|
+
desc "Roll the schema back to the previous version (specify steps w/ STEP=n)."
|
291
278
|
task rollback: :load_config do
|
292
279
|
ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:rollback")
|
293
280
|
raise "VERSION is not supported - To rollback a specific version, use db:migrate:down" if ENV["VERSION"]
|
294
281
|
|
295
282
|
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
|
296
283
|
|
297
|
-
ActiveRecord::
|
284
|
+
ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool.migration_context.rollback(step)
|
298
285
|
|
299
286
|
db_namespace["_dump"].invoke
|
300
287
|
end
|
@@ -302,7 +289,9 @@ db_namespace = namespace :db do
|
|
302
289
|
# desc 'Pushes the schema to the next version (specify steps w/ STEP=n).'
|
303
290
|
task forward: :load_config do
|
304
291
|
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
|
305
|
-
|
292
|
+
|
293
|
+
ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool.migration_context.forward(step)
|
294
|
+
|
306
295
|
db_namespace["_dump"].invoke
|
307
296
|
end
|
308
297
|
|
@@ -310,65 +299,84 @@ db_namespace = namespace :db do
|
|
310
299
|
task all: ["db:drop", "db:setup"]
|
311
300
|
|
312
301
|
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
313
|
-
desc "
|
302
|
+
desc "Drop and recreate the #{name} database from its schema for the current environment and load the seeds."
|
314
303
|
task name => ["db:drop:#{name}", "db:setup:#{name}"]
|
315
304
|
end
|
316
305
|
end
|
317
306
|
|
318
|
-
desc "
|
307
|
+
desc "Drop and recreate all databases from their schema for the current environment and load the seeds."
|
319
308
|
task reset: [ "db:drop", "db:setup" ]
|
320
309
|
|
321
|
-
# desc "
|
310
|
+
# desc "Retrieve the charset for the current environment's database"
|
322
311
|
task charset: :load_config do
|
323
312
|
puts ActiveRecord::Tasks::DatabaseTasks.charset_current
|
324
313
|
end
|
325
314
|
|
326
|
-
# desc "
|
315
|
+
# desc "Retrieve the collation for the current environment's database"
|
327
316
|
task collation: :load_config do
|
328
317
|
puts ActiveRecord::Tasks::DatabaseTasks.collation_current
|
329
318
|
rescue NoMethodError
|
330
319
|
$stderr.puts "Sorry, your database adapter is not supported yet. Feel free to submit a patch."
|
331
320
|
end
|
332
321
|
|
333
|
-
desc "
|
322
|
+
desc "Retrieve the current schema version number"
|
334
323
|
task version: :load_config do
|
335
|
-
|
324
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: Rails.env) do |pool|
|
325
|
+
puts "\ndatabase: #{pool.db_config.database}\n"
|
326
|
+
puts "Current version: #{pool.migration_context.current_version}"
|
327
|
+
puts
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
namespace :version do
|
332
|
+
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
333
|
+
desc "Retrieve the current schema version number for #{name} database"
|
334
|
+
task name => :load_config do
|
335
|
+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
|
336
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection(db_config) do |connection|
|
337
|
+
puts "Current version: #{connection.schema_version}"
|
338
|
+
end
|
339
|
+
end
|
340
|
+
end
|
336
341
|
end
|
337
342
|
|
338
343
|
# desc "Raises an error if there are pending migrations"
|
339
344
|
task abort_if_pending_migrations: :load_config do
|
340
|
-
pending_migrations =
|
341
|
-
ActiveRecord::Base.establish_connection(db_config)
|
345
|
+
pending_migrations = []
|
342
346
|
|
343
|
-
|
347
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each do |pool|
|
348
|
+
pending_migrations << pool.migration_context.open.pending_migrations
|
344
349
|
end
|
345
350
|
|
351
|
+
pending_migrations = pending_migrations.flatten!
|
352
|
+
|
346
353
|
if pending_migrations.any?
|
347
354
|
puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
|
355
|
+
|
348
356
|
pending_migrations.each do |pending_migration|
|
349
357
|
puts " %4d %s" % [pending_migration.version, pending_migration.name]
|
350
358
|
end
|
359
|
+
|
351
360
|
abort %{Run `bin/rails db:migrate` to update your database then try again.}
|
352
361
|
end
|
353
|
-
ensure
|
354
|
-
ActiveRecord::Base.establish_connection(ActiveRecord::Tasks::DatabaseTasks.env.to_sym)
|
355
362
|
end
|
356
363
|
|
357
364
|
namespace :abort_if_pending_migrations do
|
358
365
|
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
359
|
-
# desc "
|
366
|
+
# desc "Raise an error if there are pending migrations for #{name} database"
|
360
367
|
task name => :load_config do
|
361
|
-
|
362
|
-
|
368
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: Rails.env, name: name) do |pool|
|
369
|
+
pending_migrations = pool.migration_context.open.pending_migrations
|
370
|
+
|
371
|
+
if pending_migrations.any?
|
372
|
+
puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
|
363
373
|
|
364
|
-
|
374
|
+
pending_migrations.each do |pending_migration|
|
375
|
+
puts " %4d %s" % [pending_migration.version, pending_migration.name]
|
376
|
+
end
|
365
377
|
|
366
|
-
|
367
|
-
puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
|
368
|
-
pending_migrations.each do |pending_migration|
|
369
|
-
puts " %4d %s" % [pending_migration.version, pending_migration.name]
|
378
|
+
abort %{Run `bin/rails db:migrate:#{name}` to update your database then try again.}
|
370
379
|
end
|
371
|
-
abort %{Run `bin/rails db:migrate:#{name}` to update your database then try again.}
|
372
380
|
end
|
373
381
|
end
|
374
382
|
end
|
@@ -378,32 +386,32 @@ db_namespace = namespace :db do
|
|
378
386
|
task all: ["db:create", :environment, "db:schema:load", :seed]
|
379
387
|
|
380
388
|
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
381
|
-
desc "
|
389
|
+
desc "Create the #{name} database, load the schema, and initialize with the seed data (use db:reset:#{name} to also drop the database first)"
|
382
390
|
task name => ["db:create:#{name}", :environment, "db:schema:load:#{name}", "db:seed"]
|
383
391
|
end
|
384
392
|
end
|
385
393
|
|
386
|
-
desc "
|
394
|
+
desc "Create all databases, load all schemas, and initialize with the seed data (use db:reset to also drop all databases first)"
|
387
395
|
task setup: ["db:create", :environment, "db:schema:load", :seed]
|
388
396
|
|
389
|
-
desc "
|
397
|
+
desc "Run setup if database does not exist, or run migrations if it does"
|
390
398
|
task prepare: :load_config do
|
391
399
|
ActiveRecord::Tasks::DatabaseTasks.prepare_all
|
392
400
|
end
|
393
401
|
|
394
|
-
desc "
|
402
|
+
desc "Load the seed data from db/seeds.rb"
|
395
403
|
task seed: :load_config do
|
396
404
|
db_namespace["abort_if_pending_migrations"].invoke
|
397
405
|
ActiveRecord::Tasks::DatabaseTasks.load_seed
|
398
406
|
end
|
399
407
|
|
400
408
|
namespace :seed do
|
401
|
-
desc "
|
409
|
+
desc "Truncate tables of each database for current environment and load the seeds"
|
402
410
|
task replant: [:load_config, :truncate_all, :seed]
|
403
411
|
end
|
404
412
|
|
405
413
|
namespace :fixtures do
|
406
|
-
desc "
|
414
|
+
desc "Load fixtures into the current environment's database. To load specific fixtures, use FIXTURES=x,y. To load from subdirectory in test/fixtures, use FIXTURES_DIR=z. To specify an alternative path (e.g. spec/fixtures), use FIXTURES_PATH=spec/fixtures."
|
407
415
|
task load: :load_config do
|
408
416
|
require "active_record/fixtures"
|
409
417
|
|
@@ -452,32 +460,32 @@ db_namespace = namespace :db do
|
|
452
460
|
end
|
453
461
|
|
454
462
|
namespace :schema do
|
455
|
-
desc "
|
463
|
+
desc "Create a database schema file (either db/schema.rb or db/structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`)"
|
456
464
|
task dump: :load_config do
|
457
|
-
ActiveRecord::
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, schema_format)
|
462
|
-
end
|
465
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each do |pool|
|
466
|
+
db_config = pool.db_config
|
467
|
+
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
|
468
|
+
ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, schema_format)
|
463
469
|
end
|
464
470
|
|
465
471
|
db_namespace["schema:dump"].reenable
|
466
472
|
end
|
467
473
|
|
468
|
-
desc "
|
474
|
+
desc "Load a database schema file (either db/schema.rb or db/structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`) into the database"
|
469
475
|
task load: [:load_config, :check_protected_environments] do
|
470
476
|
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(ActiveRecord.schema_format, ENV["SCHEMA"])
|
471
477
|
end
|
472
478
|
|
473
479
|
namespace :dump do
|
474
480
|
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
475
|
-
desc "
|
481
|
+
desc "Create a database schema file (either db/schema.rb or db/structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`) for #{name} database"
|
476
482
|
task name => :load_config do
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
483
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(name: name) do |pool|
|
484
|
+
db_config = pool.db_config
|
485
|
+
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
|
486
|
+
ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, schema_format)
|
487
|
+
end
|
488
|
+
|
481
489
|
db_namespace["schema:dump:#{name}"].reenable
|
482
490
|
end
|
483
491
|
end
|
@@ -485,41 +493,32 @@ db_namespace = namespace :db do
|
|
485
493
|
|
486
494
|
namespace :load do
|
487
495
|
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
488
|
-
desc "
|
489
|
-
task name =>
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
ActiveRecord::Base.establish_connection(original_db_config) if original_db_config
|
496
|
+
desc "Load a database schema file (either db/schema.rb or db/structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`) into the #{name} database"
|
497
|
+
task name => "db:test:purge:#{name}" do
|
498
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(name: name) do |pool|
|
499
|
+
db_config = pool.db_config
|
500
|
+
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
|
501
|
+
ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, schema_format)
|
502
|
+
end
|
496
503
|
end
|
497
504
|
end
|
498
505
|
end
|
499
506
|
|
500
507
|
namespace :cache do
|
501
|
-
desc "
|
508
|
+
desc "Create a db/schema_cache.yml file."
|
502
509
|
task dump: :load_config do
|
503
|
-
ActiveRecord::
|
504
|
-
|
505
|
-
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(
|
506
|
-
|
507
|
-
|
508
|
-
)
|
509
|
-
ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(
|
510
|
-
ActiveRecord::Base.connection,
|
511
|
-
filename,
|
512
|
-
)
|
510
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each do |pool|
|
511
|
+
db_config = pool.db_config
|
512
|
+
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config)
|
513
|
+
|
514
|
+
ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(pool, filename)
|
513
515
|
end
|
514
516
|
end
|
515
517
|
|
516
|
-
desc "
|
518
|
+
desc "Clear a db/schema_cache.yml file."
|
517
519
|
task clear: :load_config do
|
518
520
|
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
|
519
|
-
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(
|
520
|
-
db_config.name,
|
521
|
-
schema_cache_path: db_config.schema_cache_path,
|
522
|
-
)
|
521
|
+
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config)
|
523
522
|
ActiveRecord::Tasks::DatabaseTasks.clear_schema_cache(
|
524
523
|
filename,
|
525
524
|
)
|
@@ -543,23 +542,14 @@ db_namespace = namespace :db do
|
|
543
542
|
end
|
544
543
|
|
545
544
|
namespace :test do
|
546
|
-
# desc "Recreate the test database from the current schema"
|
547
|
-
task load: %w(db:test:purge) do
|
548
|
-
db_namespace["test:load_schema"].invoke
|
549
|
-
end
|
550
|
-
|
551
545
|
# desc "Recreate the test database from an existent schema file (schema.rb or structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`)"
|
552
546
|
task load_schema: %w(db:test:purge) do
|
553
|
-
|
554
|
-
|
555
|
-
|
547
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: "test") do |pool|
|
548
|
+
db_config = pool.db_config
|
549
|
+
ActiveRecord::Schema.verbose = false
|
556
550
|
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
|
557
551
|
ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, schema_format)
|
558
552
|
end
|
559
|
-
ensure
|
560
|
-
if should_reconnect
|
561
|
-
ActiveRecord::Base.establish_connection(ActiveRecord::Tasks::DatabaseTasks.env.to_sym)
|
562
|
-
end
|
563
553
|
end
|
564
554
|
|
565
555
|
# desc "Empty the test database"
|
@@ -572,29 +562,19 @@ db_namespace = namespace :db do
|
|
572
562
|
# desc 'Load the test schema'
|
573
563
|
task prepare: :load_config do
|
574
564
|
unless ActiveRecord::Base.configurations.blank?
|
575
|
-
db_namespace["test:
|
565
|
+
db_namespace["test:load_schema"].invoke
|
576
566
|
end
|
577
567
|
end
|
578
568
|
|
579
569
|
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
580
|
-
# desc "Recreate the #{name} test database"
|
581
|
-
namespace :load do
|
582
|
-
task name => "db:test:purge:#{name}" do
|
583
|
-
db_namespace["test:load_schema:#{name}"].invoke
|
584
|
-
end
|
585
|
-
end
|
586
|
-
|
587
570
|
# desc "Recreate the #{name} test database from an existent schema.rb file"
|
588
571
|
namespace :load_schema do
|
589
572
|
task name => "db:test:purge:#{name}" do
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
ensure
|
596
|
-
if should_reconnect
|
597
|
-
ActiveRecord::Base.establish_connection(ActiveRecord::Tasks::DatabaseTasks.env.to_sym)
|
573
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: "test", name: name) do |pool|
|
574
|
+
db_config = pool.db_config
|
575
|
+
ActiveRecord::Schema.verbose = false
|
576
|
+
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
|
577
|
+
ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, schema_format)
|
598
578
|
end
|
599
579
|
end
|
600
580
|
end
|
@@ -602,15 +582,17 @@ db_namespace = namespace :db do
|
|
602
582
|
# desc "Empty the #{name} test database"
|
603
583
|
namespace :purge do
|
604
584
|
task name => %w(load_config check_protected_environments) do
|
605
|
-
|
606
|
-
|
585
|
+
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(env: "test", name: name) do |pool|
|
586
|
+
db_config = pool.db_config
|
587
|
+
ActiveRecord::Tasks::DatabaseTasks.purge(db_config)
|
588
|
+
end
|
607
589
|
end
|
608
590
|
end
|
609
591
|
|
610
592
|
# desc 'Load the #{name} database test schema'
|
611
593
|
namespace :prepare do
|
612
594
|
task name => :load_config do
|
613
|
-
db_namespace["test:
|
595
|
+
db_namespace["test:load_schema:#{name}"].invoke
|
614
596
|
end
|
615
597
|
end
|
616
598
|
end
|
@@ -619,7 +601,7 @@ end
|
|
619
601
|
|
620
602
|
namespace :railties do
|
621
603
|
namespace :install do
|
622
|
-
# desc "
|
604
|
+
# desc "Copy missing migrations from Railties (e.g. engines). You can specify Railties to use with FROM=railtie1,railtie2 and database to copy to with DATABASE=database."
|
623
605
|
task migrations: :'db:load_config' do
|
624
606
|
to_load = ENV["FROM"].blank? ? :all : ENV["FROM"].split(",").map(&:strip)
|
625
607
|
railties = {}
|
@@ -643,7 +625,16 @@ namespace :railties do
|
|
643
625
|
puts "Copied migration #{migration.basename} from #{name}"
|
644
626
|
end
|
645
627
|
|
646
|
-
|
628
|
+
if ENV["DATABASE"].present? && ENV["DATABASE"] != "primary"
|
629
|
+
config = ActiveRecord::Base.configurations.configs_for(name: ENV["DATABASE"])
|
630
|
+
raise "Invalid DATABASE provided" if config.blank?
|
631
|
+
destination = config.migrations_paths
|
632
|
+
raise "#{ENV["DATABASE"]} does not have a custom migration path" if destination.blank?
|
633
|
+
else
|
634
|
+
destination = ActiveRecord::Tasks::DatabaseTasks.migrations_paths.first
|
635
|
+
end
|
636
|
+
|
637
|
+
ActiveRecord::Migration.copy(destination, railties,
|
647
638
|
on_skip: on_skip, on_copy: on_copy)
|
648
639
|
end
|
649
640
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record/runtime_registry"
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
module Railties # :nodoc:
|
7
|
+
module JobRuntime # :nodoc:
|
8
|
+
private
|
9
|
+
def instrument(operation, payload = {}, &block)
|
10
|
+
if operation == :perform && block
|
11
|
+
super(operation, payload) do
|
12
|
+
db_runtime_before_perform = ActiveRecord::RuntimeRegistry.sql_runtime
|
13
|
+
result = block.call
|
14
|
+
payload[:db_runtime] = ActiveRecord::RuntimeRegistry.sql_runtime - db_runtime_before_perform
|
15
|
+
result
|
16
|
+
end
|
17
|
+
else
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|