diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 544cd2ff2e..0a610b317d 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -204,6 +204,57 @@ module ActiveRecord singleton_class.attr_accessor :action_on_strict_loading_violation self.action_on_strict_loading_violation = :raise + ## + # :singleton-method: + # Specifies the format to use when dumping the database schema with Rails' + # Rakefile. If :sql, the schema is dumped as (potentially database- + # specific) SQL statements. If :ruby, the schema is dumped as an + # ActiveRecord::Schema file which can be loaded into any database that + # supports migrations. Use :ruby if you want to have different database + # adapters for, e.g., your development and test environments. + singleton_class.attr_accessor :schema_format + self.schema_format = :ruby + + ## + # :singleton-method: + # Specifies if an error should be raised if the query has an order being + # ignored when doing batch queries. Useful in applications where the + # scope being ignored is error-worthy, rather than a warning. + singleton_class.attr_accessor :error_on_ignored_order + self.error_on_ignored_order = false + + ## + # :singleton-method: + # Specify whether or not to use timestamps for migration versions + singleton_class.attr_accessor :timestamped_migrations + self.timestamped_migrations = true + + ## + # :singleton-method: + # Specify whether schema dump should happen at the end of the + # bin/rails db:migrate command. This is true by default, which is useful for the + # development environment. This should ideally be false in the production + # environment where dumping schema is rarely needed. + singleton_class.attr_accessor :dump_schema_after_migration + self.dump_schema_after_migration = true + + ## + # :singleton-method: + # Specifies which database schemas to dump when calling db:schema:dump. + # If the value is :schema_search_path (the default), any schemas listed in + # schema_search_path are dumped. Use :all to dump all schemas regardless + # of schema_search_path, or a string of comma separated schemas for a + # custom list. + singleton_class.attr_accessor :dump_schemas + self.dump_schemas = :schema_search_path + + ## + # :singleton-method: + # Show a warning when Rails couldn't parse your database.yml + # for multiple databases. + singleton_class.attr_accessor :suppress_multiple_database_warning + self.suppress_multiple_database_warning = false + def self.eager_load! super ActiveRecord::Locking.eager_load! diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index ab878f4726..3626ebc8d3 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -70,51 +70,6 @@ module ActiveRecord @@configurations end - ## - # :singleton-method: - # Specifies the format to use when dumping the database schema with Rails' - # Rakefile. If :sql, the schema is dumped as (potentially database- - # specific) SQL statements. If :ruby, the schema is dumped as an - # ActiveRecord::Schema file which can be loaded into any database that - # supports migrations. Use :ruby if you want to have different database - # adapters for, e.g., your development and test environments. - mattr_accessor :schema_format, instance_writer: false, default: :ruby - - ## - # :singleton-method: - # Specifies if an error should be raised if the query has an order being - # ignored when doing batch queries. Useful in applications where the - # scope being ignored is error-worthy, rather than a warning. - mattr_accessor :error_on_ignored_order, instance_writer: false, default: false - - ## - # :singleton-method: - # Specify whether or not to use timestamps for migration versions - mattr_accessor :timestamped_migrations, instance_writer: false, default: true - - ## - # :singleton-method: - # Specify whether schema dump should happen at the end of the - # bin/rails db:migrate command. This is true by default, which is useful for the - # development environment. This should ideally be false in the production - # environment where dumping schema is rarely needed. - mattr_accessor :dump_schema_after_migration, instance_writer: false, default: true - - ## - # :singleton-method: - # Specifies which database schemas to dump when calling db:schema:dump. - # If the value is :schema_search_path (the default), any schemas listed in - # schema_search_path are dumped. Use :all to dump all schemas regardless - # of schema_search_path, or a string of comma separated schemas for a - # custom list. - mattr_accessor :dump_schemas, instance_writer: false, default: :schema_search_path - - ## - # :singleton-method: - # Show a warning when Rails couldn't parse your database.yml - # for multiple databases. - mattr_accessor :suppress_multiple_database_warning, instance_writer: false, default: false - ## # :singleton-method: # Force enumeration of all columns in SELECT statements. diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 8c328d8b99..bf26aaba5f 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -136,7 +136,7 @@ module ActiveRecord action "Run pending migrations" do ActiveRecord::Tasks::DatabaseTasks.migrate - if ActiveRecord::Base.dump_schema_after_migration + if ActiveRecord.dump_schema_after_migration ActiveRecord::Tasks::DatabaseTasks.dump_schema( ActiveRecord::Base.connection_db_config ) @@ -632,7 +632,7 @@ module ActiveRecord all_configs = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env) needs_update = !all_configs.all? do |db_config| - Tasks::DatabaseTasks.schema_up_to_date?(db_config, ActiveRecord::Base.schema_format) + Tasks::DatabaseTasks.schema_up_to_date?(db_config, ActiveRecord.schema_format) end if needs_update @@ -994,7 +994,7 @@ module ActiveRecord # Determines the version number of the next migration. def next_migration_number(number) - if ActiveRecord::Base.timestamped_migrations + if ActiveRecord.timestamped_migrations [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % number].max else SchemaMigration.normalize_migration_number(number) diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index a6c3b3a7c2..f3d169d3e0 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -96,9 +96,9 @@ db_namespace = namespace :db do ActiveRecord::Base.establish_connection(original_db_config) end - # IMPORTANT: This task won't dump the schema if ActiveRecord::Base.dump_schema_after_migration is set to false + # IMPORTANT: This task won't dump the schema if ActiveRecord.dump_schema_after_migration is set to false task :_dump do - if ActiveRecord::Base.dump_schema_after_migration + if ActiveRecord.dump_schema_after_migration db_namespace["schema:dump"].invoke end # Allow this task to be called as many times as required. An example is the @@ -108,9 +108,9 @@ db_namespace = namespace :db do namespace :_dump do ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name| - # IMPORTANT: This task won't dump the schema if ActiveRecord::Base.dump_schema_after_migration is set to false + # IMPORTANT: This task won't dump the schema if ActiveRecord.dump_schema_after_migration is set to false task name do - if ActiveRecord::Base.dump_schema_after_migration + if ActiveRecord.dump_schema_after_migration db_namespace["schema:dump:#{name}"].invoke end # Allow this task to be called as many times as required. An example is the @@ -430,7 +430,7 @@ db_namespace = namespace :db do desc "Loads a database schema file (either db/schema.rb or db/structure.sql, depending on `config.active_record.schema_format`) into the database" task load: [:load_config, :check_protected_environments] do - ActiveRecord::Tasks::DatabaseTasks.load_schema_current(ActiveRecord::Base.schema_format, ENV["SCHEMA"]) + ActiveRecord::Tasks::DatabaseTasks.load_schema_current(ActiveRecord.schema_format, ENV["SCHEMA"]) end task load_if_ruby: ["db:create", :environment] do @@ -438,7 +438,7 @@ db_namespace = namespace :db do Using `bin/rails db:schema:load_if_ruby` is deprecated and will be removed in Rails 7.0. Configure the format using `config.active_record.schema_format = :ruby` to use `schema.rb` and run `bin/rails db:schema:load` instead. MSG - db_namespace["schema:load"].invoke if ActiveRecord::Base.schema_format == :ruby + db_namespace["schema:load"].invoke if ActiveRecord.schema_format == :ruby end namespace :dump do @@ -458,7 +458,7 @@ db_namespace = namespace :db do 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" task name => :load_config do db_config = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: name) - ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, ActiveRecord::Base.schema_format, ENV["SCHEMA"]) + ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, ActiveRecord.schema_format, ENV["SCHEMA"]) end end end @@ -520,7 +520,7 @@ db_namespace = namespace :db do Using `bin/rails db:structure:load_if_sql` is deprecated and will be removed in Rails 7.0. Configure the format using `config.active_record.schema_format = :sql` to use `structure.sql` and run `bin/rails db:schema:load` instead. MSG - db_namespace["schema:load"].invoke if ActiveRecord::Base.schema_format == :sql + db_namespace["schema:load"].invoke if ActiveRecord.schema_format == :sql end namespace :dump do @@ -577,7 +577,7 @@ db_namespace = namespace :db do ActiveRecord::Schema.verbose = false ActiveRecord::Base.configurations.configs_for(env_name: "test").each do |db_config| filename = ActiveRecord::Tasks::DatabaseTasks.dump_filename(db_config.name) - ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, ActiveRecord::Base.schema_format, filename) + ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, ActiveRecord.schema_format, filename) end ensure if should_reconnect @@ -623,7 +623,7 @@ db_namespace = namespace :db do ActiveRecord::Schema.verbose = false filename = ActiveRecord::Tasks::DatabaseTasks.dump_filename(name) db_config = ActiveRecord::Base.configurations.configs_for(env_name: "test", name: name) - ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, ActiveRecord::Base.schema_format, filename) + ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, ActiveRecord.schema_format, filename) ensure if should_reconnect ActiveRecord::Base.establish_connection(ActiveRecord::Tasks::DatabaseTasks.env.to_sym) diff --git a/activerecord/lib/active_record/relation/batches.rb b/activerecord/lib/active_record/relation/batches.rb index 1917dcc164..bce3862a7f 100644 --- a/activerecord/lib/active_record/relation/batches.rb +++ b/activerecord/lib/active_record/relation/batches.rb @@ -284,7 +284,7 @@ module ActiveRecord end def act_on_ignored_order(error_on_ignore) - raise_error = (error_on_ignore.nil? ? klass.error_on_ignored_order : error_on_ignore) + raise_error = (error_on_ignore.nil? ? ActiveRecord.error_on_ignored_order : error_on_ignore) if raise_error raise ArgumentError.new(ORDER_IGNORE_MESSAGE) diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index ecd0c5f0af..d726edba7a 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -13,8 +13,8 @@ module ActiveRecord ## # :singleton-method: # A list of tables which should not be dumped to the schema. - # Acceptable values are strings as well as regexp if ActiveRecord::Base.schema_format == :ruby. - # Only strings are accepted if ActiveRecord::Base.schema_format == :sql. + # Acceptable values are strings as well as regexp if ActiveRecord.schema_format == :ruby. + # Only strings are accepted if ActiveRecord.schema_format == :sql. cattr_accessor :ignore_tables, default: [] ## diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index 4a6e9acadc..9164a21e5b 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -161,7 +161,7 @@ module ActiveRecord begin Rails.application.config.load_database_yaml rescue - unless ActiveRecord::Base.suppress_multiple_database_warning + unless ActiveRecord.suppress_multiple_database_warning $stderr.puts "Rails couldn't infer whether you are using multiple databases from your database.yml and can't generate the tasks for the non-primary databases. If you'd like to use this feature, please simplify your ERB." end @@ -210,8 +210,8 @@ module ActiveRecord # Skipped when no database migrate - if ActiveRecord::Base.dump_schema_after_migration - dump_schema(db_config, ActiveRecord::Base.schema_format) + if ActiveRecord.dump_schema_after_migration + dump_schema(db_config, ActiveRecord.schema_format) end rescue ActiveRecord::NoDatabaseError config_name = db_config.name @@ -220,7 +220,7 @@ module ActiveRecord if File.exist?(dump_filename(config_name)) load_schema( db_config, - ActiveRecord::Base.schema_format, + ActiveRecord.schema_format, nil ) else @@ -358,7 +358,7 @@ module ActiveRecord database_adapter_for(db_config, *arguments).structure_load(filename, flags) end - def load_schema(db_config, format = ActiveRecord::Base.schema_format, file = nil) # :nodoc: + def load_schema(db_config, format = ActiveRecord.schema_format, file = nil) # :nodoc: file ||= dump_filename(db_config.name, format) verbose_was, Migration.verbose = Migration.verbose, verbose? && ENV["VERBOSE"] @@ -380,7 +380,7 @@ module ActiveRecord Migration.verbose = verbose_was end - def schema_up_to_date?(configuration, format = ActiveRecord::Base.schema_format, file = nil, environment = nil, name = nil) + def schema_up_to_date?(configuration, format = ActiveRecord.schema_format, file = nil, environment = nil, name = nil) db_config = resolve_configuration(configuration) if environment || name @@ -401,7 +401,7 @@ module ActiveRecord ActiveRecord::InternalMetadata[:schema_sha1] == schema_sha1(file) end - def reconstruct_from_schema(db_config, format = ActiveRecord::Base.schema_format, file = nil) # :nodoc: + def reconstruct_from_schema(db_config, format = ActiveRecord.schema_format, file = nil) # :nodoc: file ||= dump_filename(db_config.name, format) check_schema_file(file) @@ -419,7 +419,7 @@ module ActiveRecord load_schema(db_config, format, file) end - def dump_schema(db_config, format = ActiveRecord::Base.schema_format) # :nodoc: + def dump_schema(db_config, format = ActiveRecord.schema_format) # :nodoc: require "active_record/schema_dumper" filename = dump_filename(db_config.name, format) connection = ActiveRecord::Base.connection @@ -441,12 +441,12 @@ module ActiveRecord end end - def schema_file(format = ActiveRecord::Base.schema_format) + def schema_file(format = ActiveRecord.schema_format) File.join(db_dir, schema_file_type(format)) end deprecate :schema_file - def schema_file_type(format = ActiveRecord::Base.schema_format) + def schema_file_type(format = ActiveRecord.schema_format) case format when :ruby "schema.rb" @@ -455,7 +455,7 @@ module ActiveRecord end end - def dump_filename(db_config_name, format = ActiveRecord::Base.schema_format) + def dump_filename(db_config_name, format = ActiveRecord.schema_format) filename = if ActiveRecord::Base.configurations.primary?(db_config_name) schema_file_type(format) else @@ -475,7 +475,7 @@ module ActiveRecord schema_cache_path || ENV["SCHEMA_CACHE"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename) end - def load_schema_current(format = ActiveRecord::Base.schema_format, file = nil, environment = env) + def load_schema_current(format = ActiveRecord.schema_format, file = nil, environment = env) each_current_configuration(environment) do |db_config| load_schema(db_config, format, file) end diff --git a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb index c4789091c6..0f36385540 100644 --- a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb @@ -50,13 +50,13 @@ module ActiveRecord set_psql_env search_path = \ - case ActiveRecord::Base.dump_schemas + case ActiveRecord.dump_schemas when :schema_search_path configuration_hash[:schema_search_path] when :all nil when String - ActiveRecord::Base.dump_schemas + ActiveRecord.dump_schemas end args = ["--schema-only", "--no-privileges", "--no-owner", "--file", filename] diff --git a/activerecord/lib/active_record/test_databases.rb b/activerecord/lib/active_record/test_databases.rb index ab4732bb80..0c9bb69184 100644 --- a/activerecord/lib/active_record/test_databases.rb +++ b/activerecord/lib/active_record/test_databases.rb @@ -14,7 +14,7 @@ module ActiveRecord ActiveRecord::Base.configurations.configs_for(env_name: env_name).each do |db_config| db_config._database = "#{db_config.database}-#{i}" - ActiveRecord::Tasks::DatabaseTasks.reconstruct_from_schema(db_config, ActiveRecord::Base.schema_format, nil) + ActiveRecord::Tasks::DatabaseTasks.reconstruct_from_schema(db_config, ActiveRecord.schema_format, nil) end ensure ActiveRecord::Base.establish_connection diff --git a/activerecord/test/cases/batches_test.rb b/activerecord/test/cases/batches_test.rb index 8df0d2f16c..5eabe3e84c 100644 --- a/activerecord/test/cases/batches_test.rb +++ b/activerecord/test/cases/batches_test.rb @@ -207,26 +207,26 @@ class EachTest < ActiveRecord::TestCase def test_find_in_batches_should_not_error_if_config_overridden # Set the config option which will be overridden - prev = ActiveRecord::Base.error_on_ignored_order - ActiveRecord::Base.error_on_ignored_order = true + prev = ActiveRecord.error_on_ignored_order + ActiveRecord.error_on_ignored_order = true assert_nothing_raised do PostWithDefaultScope.find_in_batches(error_on_ignore: false) { } end ensure # Set back to default - ActiveRecord::Base.error_on_ignored_order = prev + ActiveRecord.error_on_ignored_order = prev end def test_find_in_batches_should_error_on_config_specified_to_error # Set the config option - prev = ActiveRecord::Base.error_on_ignored_order - ActiveRecord::Base.error_on_ignored_order = true + prev = ActiveRecord.error_on_ignored_order + ActiveRecord.error_on_ignored_order = true assert_raise(ArgumentError) do PostWithDefaultScope.find_in_batches() { } end ensure # Set back to default - ActiveRecord::Base.error_on_ignored_order = prev + ActiveRecord.error_on_ignored_order = prev end def test_find_in_batches_should_not_error_by_default diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 63482d2402..338cd647e2 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -1396,13 +1396,13 @@ class CopyMigrationsTest < ActiveRecord::TestCase end def clear - ActiveRecord::Base.timestamped_migrations = true + ActiveRecord.timestamped_migrations = true to_delete = Dir[@migrations_path + "/*.rb"] - @existing_migrations File.delete(*to_delete) end def test_copying_migrations_without_timestamps - ActiveRecord::Base.timestamped_migrations = false + ActiveRecord.timestamped_migrations = false @migrations_path = MIGRATIONS_ROOT + "/valid" @existing_migrations = Dir[@migrations_path + "/*.rb"] @@ -1423,7 +1423,7 @@ class CopyMigrationsTest < ActiveRecord::TestCase end def test_copying_migrations_without_timestamps_from_2_sources - ActiveRecord::Base.timestamped_migrations = false + ActiveRecord.timestamped_migrations = false @migrations_path = MIGRATIONS_ROOT + "/valid" @existing_migrations = Dir[@migrations_path + "/*.rb"] @@ -1507,7 +1507,7 @@ class CopyMigrationsTest < ActiveRecord::TestCase end def test_copying_migrations_preserving_magic_comments - ActiveRecord::Base.timestamped_migrations = false + ActiveRecord.timestamped_migrations = false @migrations_path = MIGRATIONS_ROOT + "/valid" @existing_migrations = Dir[@migrations_path + "/*.rb"] diff --git a/activerecord/test/cases/tasks/postgresql_rake_test.rb b/activerecord/test/cases/tasks/postgresql_rake_test.rb index 2c5f2a08da..4833afa3c5 100644 --- a/activerecord/test/cases/tasks/postgresql_rake_test.rb +++ b/activerecord/test/cases/tasks/postgresql_rake_test.rb @@ -482,11 +482,11 @@ if current_adapter?(:PostgreSQLAdapter) private def with_dump_schemas(value, &block) - old_dump_schemas = ActiveRecord::Base.dump_schemas - ActiveRecord::Base.dump_schemas = value + old_dump_schemas = ActiveRecord.dump_schemas + ActiveRecord.dump_schemas = value yield ensure - ActiveRecord::Base.dump_schemas = old_dump_schemas + ActiveRecord.dump_schemas = old_dump_schemas end def with_structure_dump_flags(flags) diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 54145f9fb7..6889a965df 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -1660,13 +1660,13 @@ module ApplicationTests app "production" - assert_not ActiveRecord::Base.dump_schema_after_migration + assert_not ActiveRecord.dump_schema_after_migration end test "config.active_record.dump_schema_after_migration is true by default in development" do app "development" - assert ActiveRecord::Base.dump_schema_after_migration + assert ActiveRecord.dump_schema_after_migration end test "config.active_record.verbose_query_logs is false by default in development" do @@ -1677,7 +1677,7 @@ module ApplicationTests test "config.active_record.suppress_multiple_database_warning is false by default in development" do app "development" - assert_not ActiveRecord::Base.suppress_multiple_database_warning + assert_not ActiveRecord.suppress_multiple_database_warning end test "config.annotations wrapping SourceAnnotationExtractor::Annotation class" do diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index e9d02ce3d8..bc3df7d8ef 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -274,14 +274,14 @@ class ModelGeneratorTest < Rails::Generators::TestCase end def test_migration_without_timestamps - ActiveRecord::Base.timestamped_migrations = false + ActiveRecord.timestamped_migrations = false run_generator ["account"] assert_file "db/migrate/001_create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration\[[0-9.]+\]/ run_generator ["project"] assert_file "db/migrate/002_create_projects.rb", /class CreateProjects < ActiveRecord::Migration\[[0-9.]+\]/ ensure - ActiveRecord::Base.timestamped_migrations = true + ActiveRecord.timestamped_migrations = true end def test_migration_with_configured_path diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 23aac0e924..42b956b7c8 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -203,7 +203,7 @@ module RailtiesTest load "rails/tasks/engine.rake" RUBY - add_to_config "ActiveRecord::Base.timestamped_migrations = false" + add_to_config "ActiveRecord.timestamped_migrations = false" boot_rails