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