Add database specific setup and reset tasks

This commit is contained in:
Ryan Hall 2021-08-03 14:59:39 -07:00
parent 7f5ebc4b43
commit 0a296be9e1
4 changed files with 102 additions and 2 deletions

View File

@ -1,3 +1,7 @@
* Added support for multiple databases to `rails db:setup` and `rails db:reset`.
*Ryan Hall*
* Add `ActiveRecord::Relation#structurally_compatible?`.
Adds a query method by which a user can tell if the relation that they're

View File

@ -306,7 +306,16 @@ db_namespace = namespace :db do
db_namespace["_dump"].invoke
end
desc "Drops and recreates the database from db/schema.rb for the current environment and loads the seeds."
namespace :reset do
task all: ["db:drop", "db:setup"]
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
desc "Drops and recreates the #{name} database from its schema for the current environment and loads the seeds."
task name => ["db:drop:#{name}", "db:setup:#{name}"]
end
end
desc "Drops and recreates all databases from their schema for the current environment and loads the seeds."
task reset: [ "db:drop", "db:setup" ]
# desc "Retrieves the charset for the current environment's database"
@ -365,7 +374,16 @@ db_namespace = namespace :db do
end
end
desc "Creates the database, loads the schema, and initializes with the seed data (use db:reset to also drop the database first)"
namespace :setup do
task all: ["db:create", :environment, "db:schema:load", :seed]
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
desc "Creates the #{name} database, loads the schema, and initializes with the seed data (use db:reset:#{name} to also drop the database first)"
task name => ["db:create:#{name}", :environment, "db:schema:load:#{name}", "db:seed"]
end
end
desc "Creates all databases, loads all schemas, and initializes with the seed data (use db:reset to also drop all databases first)"
task setup: ["db:create", :environment, "db:schema:load", :seed]
desc "Runs setup if database does not exist, or runs migrations if it does"

View File

@ -180,6 +180,9 @@ rails db:migrate:primary # Migrate primary database for current
rails db:migrate:status # Display status of migrations
rails db:migrate:status:animals # Display status of migrations for animals database
rails db:migrate:status:primary # Display status of migrations for primary database
rails db:reset # Drops and recreates all databases from their schema for the current environment and loads the seeds
rails db:reset:animals # Drops and recreates the animals database from its schema for the current environment and loads the seeds
rails db:reset:primary # Drops and recreates the primary database from its schema for the current environment and loads the seeds
rails db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n)
rails db:rollback:animals # Rollback animals database for current environment (specify steps w/ STEP=n)
rails db:rollback:primary # Rollback primary database for current environment (specify steps w/ STEP=n)
@ -189,6 +192,9 @@ rails db:schema:dump:primary # Creates a db/schema.rb file that is p
rails db:schema:load # Loads a database schema file (either db/schema.rb or db/structure.sql ...
rails db:schema:load:animals # Loads a database schema file (either db/schema.rb or db/structure.sql ...
rails db:schema:load:primary # Loads a database schema file (either db/schema.rb or db/structure.sql ...
rails db:setup # Creates all databases, loads all schemas, and initializes with the seed data (use db:reset to also drop all databases first)
rails db:setup:animals # Creates the animals database, loads the schema, and initializes with the seed data (use db:reset:animals to also drop the database first)
rails db:setup:primary # Creates the primary database, loads the schema, and initializes with the seed data (use db:reset:primary to also drop the database first)
```
Running a command like `bin/rails db:create` will create both the primary and animals databases.

View File

@ -244,6 +244,54 @@ module ApplicationTests
end
end
def db_setup
Dir.chdir(app_path) do
rails "db:migrate"
rails "db:drop"
output = rails("db:setup")
assert_match(/Created database/, output)
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).each do |db_config|
assert_match_namespace(db_config.name, output)
assert File.exist?(db_config.database)
end
end
end
def db_setup_namespaced(namespace, expected_database)
Dir.chdir(app_path) do
rails "db:migrate"
rails "db:drop:#{namespace}"
output = rails("db:setup:#{namespace}")
assert_match(/Created database/, output)
assert_match_namespace(namespace, output)
assert File.exist?(expected_database)
end
end
def db_reset
Dir.chdir(app_path) do
rails "db:migrate"
output = rails("db:reset")
assert_match(/Dropped database/, output)
assert_match(/Created database/, output)
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).each do |db_config|
assert_match_namespace(db_config.name, output)
assert File.exist?(db_config.database)
end
end
end
def db_reset_namespaced(namespace, expected_database)
Dir.chdir(app_path) do
rails "db:migrate"
output = rails("db:reset:#{namespace}")
assert_match(/Dropped database/, output)
assert_match(/Created database/, output)
assert_match_namespace(namespace, output)
assert File.exist?(expected_database)
end
end
def db_up_and_down(version, namespace = nil)
Dir.chdir(app_path) do
generate_models_for_animals
@ -721,6 +769,30 @@ module ApplicationTests
assert_match(/You have 1 pending migration/, output)
end
test "db:setup works on all databases" do
require "#{app_path}/config/environment"
db_setup
end
test "db:setup:namespace works" do
require "#{app_path}/config/environment"
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).each do |db_config|
db_setup_namespaced db_config.name, db_config.database
end
end
test "db:reset works on all databases" do
require "#{app_path}/config/environment"
db_reset
end
test "db:reset:namespace works" do
require "#{app_path}/config/environment"
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).each do |db_config|
db_reset_namespaced db_config.name, db_config.database
end
end
test "db:prepare works on all databases" do
require "#{app_path}/config/environment"
db_prepare