Skip to content

Commit

Permalink
Remove deprecated rake tasks:
Browse files Browse the repository at this point in the history
    * `db:schema:load_if_ruby`
    * `db:structure:dump`
    * `db:structure:load`
    * `db:structure:load_if_sql`
    * `db:structure:dump:#{name}`
    * `db:structure:load:#{name}`
    * `db:test:load_structure`
    * `db:test:load_structure:#{name}`
  • Loading branch information
rafaelfranca committed Nov 19, 2021
1 parent 7b9d4d2 commit cf269d5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 216 deletions.
13 changes: 13 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
* Remove deprecated rake tasks:

* `db:schema:load_if_ruby`
* `db:structure:dump`
* `db:structure:load`
* `db:structure:load_if_sql`
* `db:structure:dump:#{name}`
* `db:structure:load:#{name}`
* `db:test:load_structure`
* `db:test:load_structure:#{name}`

*Rafael Mendonça França*

* Remove deprecated `DatabaseConfig#config` method.

*Rafael Mendonça França*
Expand Down
85 changes: 0 additions & 85 deletions activerecord/lib/active_record/railties/databases.rake
Original file line number Diff line number Diff line change
Expand Up @@ -469,14 +469,6 @@ db_namespace = namespace :db do
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(ActiveRecord.schema_format, ENV["SCHEMA"])
end

task load_if_ruby: ["db:create", :environment] do
ActiveSupport::Deprecation.warn(<<-MSG.squish)
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.schema_format == :ruby
end

namespace :dump do
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
desc "Creates a database schema file (either db/schema.rb or db/structure.sql, depending on `config.active_record.schema_format`) for #{name} database"
Expand Down Expand Up @@ -533,63 +525,6 @@ db_namespace = namespace :db do
end
end

namespace :structure do
desc "Dumps the database structure to db/structure.sql. Specify another file with SCHEMA=db/my_structure.sql"
task dump: :load_config do
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Using `bin/rails db:structure:dump` 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:dump` instead.
MSG

db_namespace["schema:dump"].invoke
db_namespace["structure:dump"].reenable
end

desc "Recreates the databases from the structure.sql file"
task load: [:load_config, :check_protected_environments] do
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Using `bin/rails db:structure:load` 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
end

task load_if_sql: ["db:create", :environment] do
ActiveSupport::Deprecation.warn(<<-MSG.squish)
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.schema_format == :sql
end

namespace :dump do
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
desc "Dumps the #{name} database structure to db/structure.sql. Specify another file with SCHEMA=db/my_structure.sql"
task name => :load_config do
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Using `bin/rails db:structure:dump:#{name}` 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:dump:#{name}` instead.
MSG
db_namespace["schema:dump:#{name}"].invoke
db_namespace["structure:dump:#{name}"].reenable
end
end
end

namespace :load do
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
desc "Recreates the #{name} database from the structure.sql file"
task name => :load_config do
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Using `bin/rails db:structure:load:#{name}` 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:#{name}` instead.
MSG
db_namespace["schema:load:#{name}"].invoke
end
end
end
end

namespace :encryption do
desc "Generate a set of keys for configuring Active Record encryption in a given environment"
task :init do
Expand Down Expand Up @@ -623,15 +558,6 @@ db_namespace = namespace :db do
end
end

# desc "Recreate the test database from an existent structure.sql file"
task load_structure: %w(db:test:purge) do
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Using `bin/rails db:test:load_structure` 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:test:load_schema` instead.
MSG
db_namespace["test:load_schema"].invoke
end

# desc "Empty the test database"
task purge: %w(load_config check_protected_environments) do
ActiveRecord::Base.configurations.configs_for(env_name: "test").each do |db_config|
Expand Down Expand Up @@ -668,17 +594,6 @@ db_namespace = namespace :db do
end
end

# desc "Recreate the #{name} test database from an existent structure.sql file"
namespace :load_structure do
task name => "db:test:purge:#{name}" do
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Using `bin/rails db:test:load_structure:#{name}` 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:test:load_structure:#{name}` instead.
MSG
db_namespace["test:load_schema:#{name}"].invoke
end
end

# desc "Empty the #{name} test database"
namespace :purge do
task name => %w(load_config check_protected_environments) do
Expand Down
11 changes: 11 additions & 0 deletions guides/source/7_0_release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ Please refer to the [Changelog][active-record] for detailed changes.

* Remove deprecated `DatabaseConfig#config` method.

* Remove deprecated rake tasks:

* `db:schema:load_if_ruby`
* `db:structure:dump`
* `db:structure:load`
* `db:structure:load_if_sql`
* `db:structure:dump:#{name}`
* `db:structure:load:#{name}`
* `db:test:load_structure`
* `db:test:load_structure:#{name}`

### Deprecations

### Notable changes
Expand Down
85 changes: 1 addition & 84 deletions railties/test/application/rake/dbs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -471,63 +471,6 @@ def db_fixtures_load(expected_database)
assert_equal 2, Admin::Book.count
end

def db_structure_dump_and_load(expected_database)
Dir.chdir(app_path) do
rails "generate", "model", "book", "title:string"
rails "db:migrate", "db:structure:dump"
structure_dump = File.read("db/structure.sql")
assert_match(/CREATE TABLE (?:IF NOT EXISTS )?"books"/, structure_dump)
rails "environment", "db:drop", "db:structure:load"
assert_match expected_database, ActiveRecord::Base.connection_db_config.database
require "#{app_path}/app/models/book"
# if structure is not loaded correctly, exception would be raised
assert_equal 0, Book.count
end
end

["dump", "load"].each do |command|
test "db:structure:#{command} is deprecated" do
add_to_config("config.active_support.deprecation = :stderr")
stderr_output = capture(:stderr) { rails("db:structure:#{command}", stderr: true, allow_failure: true) }
assert_match(/DEPRECATION WARNING: Using `bin\/rails db:structure:#{command}` is deprecated and will be removed in Rails 7.0/, stderr_output)
end
end

test "db:structure:dump and db:structure:load without database_url" do
add_to_config "config.active_record.schema_format = :sql"
require "#{app_path}/config/environment"
db_config = ActiveRecord::Base.connection_db_config
db_structure_dump_and_load db_config.database
end

test "db:structure:dump and db:structure:load with database_url" do
add_to_config "config.active_record.schema_format = :sql"
require "#{app_path}/config/environment"
set_database_url
db_structure_dump_and_load database_url_db_name
end

test "db:structure:dump and db:structure:load set ar_internal_metadata" do
add_to_config "config.active_record.schema_format = :sql"
require "#{app_path}/config/environment"
db_config = ActiveRecord::Base.connection_db_config
db_structure_dump_and_load db_config.database

assert_equal "test", rails("runner", "-e", "test", "puts ActiveRecord::InternalMetadata[:environment]").strip
assert_equal "development", rails("runner", "puts ActiveRecord::InternalMetadata[:environment]").strip
end

test "db:structure:dump does not dump schema information when no migrations are used" do
add_to_config "config.active_record.schema_format = :sql"
# create table without migrations
rails "runner", "ActiveRecord::Base.connection.create_table(:posts) {|t| t.string :title }"

stderr_output = capture(:stderr) { rails("db:structure:dump", stderr: true, allow_failure: true) }
assert_empty stderr_output
structure_dump = File.read("#{app_path}/db/structure.sql")
assert_match(/CREATE TABLE (?:IF NOT EXISTS )?"posts"/, structure_dump)
end

test "db:schema:load and db:structure:load do not purge the existing database" do
rails "runner", "ActiveRecord::Base.connection.create_table(:posts) {|t| t.string :title }"

Expand All @@ -548,7 +491,7 @@ def db_structure_dump_and_load(expected_database)
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
SQL

rails "db:structure:load"
rails "db:schema:load"
assert_equal '["posts", "comments", "schema_migrations", "ar_internal_metadata", "users"]', list_tables[]
end

Expand Down Expand Up @@ -583,32 +526,6 @@ def db_structure_dump_and_load(expected_database)
assert_match(/Run `bin\/rails db:migrate` to create it/, stderr_output)
end

def db_test_load_structure
Dir.chdir(app_path) do
rails "generate", "model", "book", "title:string"
rails "db:migrate", "db:structure:dump", "db:test:load_structure"
ActiveRecord::Base.configurations = Rails.application.config.database_configuration
ActiveRecord::Base.establish_connection :test
require "#{app_path}/app/models/book"
# if structure is not loaded correctly, exception would be raised
assert_equal 0, Book.count
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "test", name: "primary")
assert_match db_config.database, ActiveRecord::Base.connection_db_config.database
end
end

test "db:test:load_structure without database_url" do
add_to_config "config.active_record.schema_format = :sql"
require "#{app_path}/config/environment"
db_test_load_structure
end

test "db:test:load_structure is deprecated" do
add_to_config("config.active_support.deprecation = :stderr")
stderr_output = capture(:stderr) { rails("db:test:load_structure", stderr: true, allow_failure: true) }
assert_match(/DEPRECATION WARNING: Using `bin\/rails db:test:load_structure` is deprecated and will be removed in Rails 7.0/, stderr_output)
end

test "db:setup loads schema and seeds database" do
@old_rails_env = ENV["RAILS_ENV"]
@old_rack_env = ENV["RACK_ENV"]
Expand Down
60 changes: 19 additions & 41 deletions railties/test/application/rake/multi_dbs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,34 +115,37 @@ def db_migrate_and_schema_dump_and_load(schema_format = "ruby")
end
end

def db_migrate_and_schema_dump_and_load_one_database(format, database)
def db_migrate_and_schema_dump_and_load_one_database(database, schema_format)
add_to_config "config.active_record.schema_format = :#{schema_format}"
require "#{app_path}/config/environment"

Dir.chdir(app_path) do
generate_models_for_animals
rails "db:migrate:#{database}", "db:#{format}:dump:#{database}"
rails "db:migrate:#{database}", "db:schema:dump:#{database}"

if format == "schema"
if schema_format == "ruby"
if database == "primary"
schema_dump = File.read("db/#{format}.rb")
assert_not(File.exist?("db/animals_#{format}.rb"))
schema_dump = File.read("db/schema.rb")
assert_not(File.exist?("db/animals_schema.rb"))
assert_match(/create_table "books"/, schema_dump)
else
assert_not(File.exist?("db/#{format}.rb"))
schema_dump_animals = File.read("db/animals_#{format}.rb")
assert_not(File.exist?("db/schema.rb"))
schema_dump_animals = File.read("db/animals_schema.rb")
assert_match(/create_table "dogs"/, schema_dump_animals)
end
else
if database == "primary"
schema_dump = File.read("db/#{format}.sql")
assert_not(File.exist?("db/animals_#{format}.sql"))
schema_dump = File.read("db/structure.sql")
assert_not(File.exist?("db/animals_structure.sql"))
assert_match(/CREATE TABLE (?:IF NOT EXISTS )?"books"/, schema_dump)
else
assert_not(File.exist?("db/#{format}.sql"))
schema_dump_animals = File.read("db/animals_#{format}.sql")
assert_not(File.exist?("db/structure.sql"))
schema_dump_animals = File.read("db/animals_structure.sql")
assert_match(/CREATE TABLE (?:IF NOT EXISTS )?"dogs"/, schema_dump_animals)
end
end

rails "db:#{format}:load:#{database}"
rails "db:schema:load:#{database}"

ar_tables = lambda { rails("runner", "p ActiveRecord::Base.connection.tables").strip }
animals_tables = lambda { rails("runner", "p AnimalsBase.connection.tables").strip }
Expand Down Expand Up @@ -584,44 +587,19 @@ def change
end

test "db:migrate:name and db:schema:dump:name and db:schema:load:name works for the primary database" do
require "#{app_path}/config/environment"
db_migrate_and_schema_dump_and_load_one_database("schema", "primary")
db_migrate_and_schema_dump_and_load_one_database("primary", "ruby")
end

test "db:migrate:name and db:schema:dump:name and db:schema:load:name works for the animals database" do
require "#{app_path}/config/environment"
db_migrate_and_schema_dump_and_load_one_database("schema", "animals")
end

["dump", "load"].each do |command|
test "db:structure:#{command}:NAME is deprecated" do
app_file "config/database.yml", <<-YAML
default: &default
adapter: sqlite3
development:
primary:
<<: *default
animals:
<<: *default
database: db/animals_development.sqlite3
YAML

add_to_config("config.active_support.deprecation = :stderr")
stderr_output = capture(:stderr) { rails("db:structure:#{command}:animals", stderr: true, allow_failure: true) }
assert_match(/DEPRECATION WARNING: Using `bin\/rails db:structure:#{command}:animals` is deprecated and will be removed in Rails 7.0/, stderr_output)
end
db_migrate_and_schema_dump_and_load_one_database("animals", "ruby")
end

test "db:migrate:name and db:structure:dump:name and db:structure:load:name works for the primary database" do
add_to_config "config.active_record.schema_format = :sql"
require "#{app_path}/config/environment"
db_migrate_and_schema_dump_and_load_one_database("structure", "primary")
db_migrate_and_schema_dump_and_load_one_database("primary", "sql")
end

test "db:migrate:name and db:structure:dump:name and db:structure:load:name works for the animals database" do
add_to_config "config.active_record.schema_format = :sql"
require "#{app_path}/config/environment"
db_migrate_and_schema_dump_and_load_one_database("structure", "animals")
db_migrate_and_schema_dump_and_load_one_database("animals", "sql")
end

test "db:test:prepare:name works for the primary database with a ruby schema" do
Expand Down
6 changes: 0 additions & 6 deletions railties/test/application/rake_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,6 @@ def test_db_test_prepare_when_using_sql_format
assert_match(/Execute db:test:load_schema/, output)
end

def test_rake_dump_structure_should_respect_db_structure_env_variable
# ensure we have a schema_migrations table to dump
rails "db:migrate", "db:structure:dump", "SCHEMA=db/my_structure.sql"
assert File.exist?(File.join(app_path, "db", "my_structure.sql"))
end

def test_rake_dump_structure_should_be_called_twice_when_migrate_redo
add_to_config "config.active_record.schema_format = :sql"

Expand Down

0 comments on commit cf269d5

Please sign in to comment.