2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2012-08-30 21:50:30 -04:00
|
|
|
require "isolation/abstract_unit"
|
2019-03-04 17:57:38 -05:00
|
|
|
require "env_helpers"
|
2012-08-30 21:50:30 -04:00
|
|
|
|
|
|
|
module ApplicationTests
|
|
|
|
module RakeTests
|
|
|
|
class RakeDbsTest < ActiveSupport::TestCase
|
2019-03-04 17:57:38 -05:00
|
|
|
include ActiveSupport::Testing::Isolation, EnvHelpers
|
2012-08-30 21:50:30 -04:00
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
FileUtils.rm_rf("#{app_path}/config/environments")
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
|
|
|
def database_url_db_name
|
2014-04-02 08:36:16 -04:00
|
|
|
"db/database_url_db.sqlite3"
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_database_url
|
2016-08-06 13:16:09 -04:00
|
|
|
ENV["DATABASE_URL"] = "sqlite3:#{database_url_db_name}"
|
2013-02-20 08:44:06 -05:00
|
|
|
# ensure it's using the DATABASE_URL
|
|
|
|
FileUtils.rm_rf("#{app_path}/config/database.yml")
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
2018-09-22 00:46:44 -04:00
|
|
|
def db_create_and_drop(expected_database, environment_loaded: true)
|
2012-08-30 21:50:30 -04:00
|
|
|
Dir.chdir(app_path) do
|
2017-09-03 12:55:26 -04:00
|
|
|
output = rails("db:create")
|
2016-04-18 18:32:10 -04:00
|
|
|
assert_match(/Created database/, output)
|
2014-03-20 07:27:22 -04:00
|
|
|
assert File.exist?(expected_database)
|
2018-09-22 00:46:44 -04:00
|
|
|
yield if block_given?
|
2019-12-17 22:34:01 -05:00
|
|
|
assert_equal expected_database, ActiveRecord::Base.connection_db_config.database if environment_loaded
|
2017-09-03 12:55:26 -04:00
|
|
|
output = rails("db:drop")
|
2016-04-18 18:32:10 -04:00
|
|
|
assert_match(/Dropped database/, output)
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not File.exist?(expected_database)
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-26 14:25:11 -04:00
|
|
|
def db_create_with_warning(expected_database)
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
output = rails("db:create")
|
|
|
|
assert_match(/Rails couldn't infer whether you are using multiple databases/, output)
|
|
|
|
assert_match(/Created database/, output)
|
|
|
|
assert File.exist?(expected_database)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-01 08:29:37 -04:00
|
|
|
test "db:create and db:drop without database URL" do
|
2012-08-30 21:50:30 -04:00
|
|
|
require "#{app_path}/config/environment"
|
2019-09-12 13:10:57 -04:00
|
|
|
db_create_and_drop ActiveRecord::Base.configurations[Rails.env][:database]
|
2014-03-20 07:19:33 -04:00
|
|
|
end
|
2012-08-30 21:50:30 -04:00
|
|
|
|
2019-04-01 08:29:37 -04:00
|
|
|
test "db:create and db:drop with database URL" do
|
2012-08-30 21:50:30 -04:00
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
set_database_url
|
2014-03-20 07:27:22 -04:00
|
|
|
db_create_and_drop database_url_db_name
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
2019-10-03 08:40:49 -04:00
|
|
|
test "db:create and db:drop with database URL don't use YAML DBs" do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
set_database_url
|
|
|
|
|
|
|
|
File.write("#{app_path}/config/database.yml", <<~YAML)
|
|
|
|
test:
|
|
|
|
adapter: sqlite3
|
|
|
|
database: db/test.sqlite3
|
|
|
|
|
|
|
|
development:
|
|
|
|
adapter: sqlite3
|
|
|
|
database: db/development.sqlite3
|
|
|
|
YAML
|
|
|
|
|
|
|
|
with_rails_env "development" do
|
|
|
|
db_create_and_drop database_url_db_name do
|
|
|
|
assert_not File.exist?("#{app_path}/db/test.sqlite3")
|
|
|
|
assert_not File.exist?("#{app_path}/db/development.sqlite3")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-13 23:54:58 -05:00
|
|
|
test "db:create and db:drop respect environment setting" do
|
|
|
|
app_file "config/database.yml", <<-YAML
|
2019-03-11 17:49:27 -04:00
|
|
|
<% 1 %>
|
2017-11-13 23:54:58 -05:00
|
|
|
development:
|
2019-03-05 18:45:06 -05:00
|
|
|
database: <%= Rails.application.config.database %>
|
2017-11-13 23:54:58 -05:00
|
|
|
adapter: sqlite3
|
|
|
|
YAML
|
2019-03-12 08:34:41 -04:00
|
|
|
|
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
|
|
|
Rails.application.configure do
|
|
|
|
config.database = "db/development.sqlite3"
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
db_create_and_drop("db/development.sqlite3", environment_loaded: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "db:create and db:drop don't raise errors when loading YAML with multiline ERB" do
|
|
|
|
app_file "config/database.yml", <<-YAML
|
|
|
|
development:
|
|
|
|
database: <%=
|
|
|
|
Rails.application.config.database
|
|
|
|
%>
|
|
|
|
adapter: sqlite3
|
|
|
|
YAML
|
|
|
|
|
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
|
|
|
Rails.application.configure do
|
|
|
|
config.database = "db/development.sqlite3"
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
db_create_and_drop("db/development.sqlite3", environment_loaded: false)
|
|
|
|
end
|
|
|
|
|
2019-06-26 14:25:11 -04:00
|
|
|
test "db:create and db:drop show warning but doesn't raise errors when loading YAML with alias ERB" do
|
|
|
|
app_file "config/database.yml", <<-YAML
|
|
|
|
sqlite: &sqlite
|
|
|
|
adapter: sqlite3
|
|
|
|
database: db/development.sqlite3
|
|
|
|
|
|
|
|
development:
|
|
|
|
<<: *<%= ENV["DB"] || "sqlite" %>
|
|
|
|
YAML
|
|
|
|
|
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
|
|
|
Rails.application.configure do
|
|
|
|
config.database = "db/development.sqlite3"
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
db_create_with_warning("db/development.sqlite3")
|
|
|
|
end
|
|
|
|
|
2019-03-12 08:34:41 -04:00
|
|
|
test "db:create and db:drop don't raise errors when loading YAML containing conditional statements in ERB" do
|
|
|
|
app_file "config/database.yml", <<-YAML
|
|
|
|
development:
|
|
|
|
<% if Rails.application.config.database %>
|
|
|
|
database: <%= Rails.application.config.database %>
|
|
|
|
<% else %>
|
|
|
|
database: db/default.sqlite3
|
|
|
|
<% end %>
|
|
|
|
adapter: sqlite3
|
|
|
|
YAML
|
|
|
|
|
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
|
|
|
Rails.application.configure do
|
|
|
|
config.database = "db/development.sqlite3"
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
db_create_and_drop("db/development.sqlite3", environment_loaded: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "db:create and db:drop don't raise errors when loading YAML containing multiple ERB statements on the same line" do
|
|
|
|
app_file "config/database.yml", <<-YAML
|
|
|
|
development:
|
|
|
|
database: <% if Rails.application.config.database %><%= Rails.application.config.database %><% else %>db/default.sqlite3<% end %>
|
|
|
|
adapter: sqlite3
|
|
|
|
YAML
|
2017-11-13 23:54:58 -05:00
|
|
|
|
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
|
|
|
Rails.application.configure do
|
2019-03-05 18:45:06 -05:00
|
|
|
config.database = "db/development.sqlite3"
|
2017-11-13 23:54:58 -05:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2019-03-05 18:45:06 -05:00
|
|
|
db_create_and_drop("db/development.sqlite3", environment_loaded: false)
|
2017-11-13 23:54:58 -05:00
|
|
|
end
|
|
|
|
|
2019-05-10 16:25:35 -04:00
|
|
|
test "db:create and db:drop dont raise errors when loading YAML with FIXME ERB" do
|
|
|
|
app_file "config/database.yml", <<-YAML
|
|
|
|
development:
|
|
|
|
<%= Rails.application.config.database ? 'database: db/development.sqlite3' : 'database: db/development.sqlite3' %>
|
|
|
|
adapter: sqlite3
|
|
|
|
YAML
|
|
|
|
|
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
|
|
|
Rails.application.configure do
|
|
|
|
config.database = "db/development.sqlite3"
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
db_create_and_drop("db/development.sqlite3", environment_loaded: false)
|
|
|
|
end
|
|
|
|
|
2019-05-15 12:55:59 -04:00
|
|
|
test "db:create and db:drop don't raise errors when loading YAML which contains a key's value as an ERB statement" do
|
|
|
|
app_file "config/database.yml", <<-YAML
|
|
|
|
development:
|
|
|
|
database: <%= Rails.application.config.database ? 'db/development.sqlite3' : 'db/development.sqlite3' %>
|
|
|
|
custom_option: <%= ENV['CUSTOM_OPTION'] %>
|
|
|
|
adapter: sqlite3
|
|
|
|
YAML
|
|
|
|
|
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
|
|
|
Rails.application.configure do
|
|
|
|
config.database = "db/development.sqlite3"
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
db_create_and_drop("db/development.sqlite3", environment_loaded: false)
|
|
|
|
end
|
|
|
|
|
2015-04-26 23:17:05 -04:00
|
|
|
def with_database_existing
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
set_database_url
|
2017-09-03 12:55:26 -04:00
|
|
|
rails "db:create"
|
2015-04-26 23:17:05 -04:00
|
|
|
yield
|
2017-09-03 12:55:26 -04:00
|
|
|
rails "db:drop"
|
2015-04-26 23:17:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:create failure because database exists" do
|
2015-04-26 23:17:05 -04:00
|
|
|
with_database_existing do
|
2017-09-03 12:55:26 -04:00
|
|
|
output = rails("db:create")
|
2015-12-03 21:00:10 -05:00
|
|
|
assert_match(/already exists/, output)
|
2015-04-26 23:17:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-26 23:20:08 -04:00
|
|
|
def with_bad_permissions
|
|
|
|
Dir.chdir(app_path) do
|
2019-02-05 09:50:06 -05:00
|
|
|
skip "Can't avoid permissions as root" if Process.uid.zero?
|
|
|
|
|
2015-04-26 23:20:08 -04:00
|
|
|
set_database_url
|
|
|
|
FileUtils.chmod("-w", "db")
|
|
|
|
yield
|
|
|
|
FileUtils.chmod("+w", "db")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:create failure because bad permissions" do
|
2015-04-26 23:20:08 -04:00
|
|
|
with_bad_permissions do
|
2017-09-03 12:55:26 -04:00
|
|
|
output = rails("db:create", allow_failure: true)
|
2018-08-28 02:20:18 -04:00
|
|
|
assert_match("Couldn't create '#{database_url_db_name}' database. Please check your configuration.", output)
|
2015-04-26 23:20:08 -04:00
|
|
|
assert_equal 1, $?.exitstatus
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-01 04:16:06 -05:00
|
|
|
test "db:create works when schema cache exists and database does not exist" do
|
|
|
|
use_postgresql
|
|
|
|
|
|
|
|
begin
|
|
|
|
rails %w(db:create db:migrate db:schema:cache:dump)
|
|
|
|
|
|
|
|
rails "db:drop"
|
|
|
|
rails "db:create"
|
|
|
|
assert_equal 0, $?.exitstatus
|
|
|
|
ensure
|
|
|
|
rails "db:drop" rescue nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:drop failure because database does not exist" do
|
2017-09-14 04:43:56 -04:00
|
|
|
output = rails("db:drop:_unsafe", "--trace")
|
|
|
|
assert_match(/does not exist/, output)
|
2015-04-26 23:17:05 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:drop failure because bad permissions" do
|
2015-04-26 23:21:54 -04:00
|
|
|
with_database_existing do
|
|
|
|
with_bad_permissions do
|
2017-09-03 12:55:26 -04:00
|
|
|
output = rails("db:drop", allow_failure: true)
|
2015-12-03 21:00:10 -05:00
|
|
|
assert_match(/Couldn't drop/, output)
|
2015-04-26 23:21:54 -04:00
|
|
|
assert_equal 1, $?.exitstatus
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-08 15:42:28 -05:00
|
|
|
test "db:truncate_all truncates all non-internal tables" do
|
2019-03-04 17:57:38 -05:00
|
|
|
Dir.chdir(app_path) do
|
|
|
|
rails "generate", "model", "book", "title:string"
|
|
|
|
rails "db:migrate"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
Book.create!(title: "Remote")
|
|
|
|
assert_equal 1, Book.count
|
|
|
|
schema_migrations = ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.schema_migrations_table_name}\"")
|
|
|
|
internal_metadata = ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.internal_metadata_table_name}\"")
|
|
|
|
|
|
|
|
rails "db:truncate_all"
|
|
|
|
|
|
|
|
assert_equal(
|
|
|
|
schema_migrations,
|
|
|
|
ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.schema_migrations_table_name}\"")
|
|
|
|
)
|
|
|
|
assert_equal(
|
|
|
|
internal_metadata,
|
|
|
|
ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.internal_metadata_table_name}\"")
|
|
|
|
)
|
|
|
|
assert_equal 0, Book.count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "db:truncate_all does not truncate any tables when environment is protected" do
|
|
|
|
with_rails_env "production" do
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
rails "generate", "model", "book", "title:string"
|
|
|
|
rails "db:migrate"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
Book.create!(title: "Remote")
|
|
|
|
assert_equal 1, Book.count
|
|
|
|
schema_migrations = ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.schema_migrations_table_name}\"")
|
|
|
|
internal_metadata = ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.internal_metadata_table_name}\"")
|
|
|
|
books = ActiveRecord::Base.connection.execute("SELECT * from \"books\"")
|
|
|
|
|
|
|
|
output = rails("db:truncate_all", allow_failure: true)
|
|
|
|
assert_match(/ActiveRecord::ProtectedEnvironmentError/, output)
|
|
|
|
|
|
|
|
assert_equal(
|
|
|
|
schema_migrations,
|
|
|
|
ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.schema_migrations_table_name}\"")
|
|
|
|
)
|
|
|
|
assert_equal(
|
|
|
|
internal_metadata,
|
|
|
|
ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.internal_metadata_table_name}\"")
|
|
|
|
)
|
|
|
|
assert_equal 1, Book.count
|
|
|
|
assert_equal(books, ActiveRecord::Base.connection.execute("SELECT * from \"books\""))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-20 07:27:22 -04:00
|
|
|
def db_migrate_and_status(expected_database)
|
2017-09-14 04:43:56 -04:00
|
|
|
rails "generate", "model", "book", "title:string"
|
|
|
|
rails "db:migrate"
|
|
|
|
output = rails("db:migrate:status")
|
|
|
|
assert_match(%r{database:\s+\S*#{Regexp.escape(expected_database)}}, output)
|
|
|
|
assert_match(/up\s+\d{14}\s+Create books/, output)
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:migrate and db:migrate:status without database_url" do
|
2012-08-30 21:50:30 -04:00
|
|
|
require "#{app_path}/config/environment"
|
2019-09-12 13:10:57 -04:00
|
|
|
db_migrate_and_status ActiveRecord::Base.configurations[Rails.env][:database]
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:migrate and db:migrate:status with database_url" do
|
2012-08-30 21:50:30 -04:00
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
set_database_url
|
2014-03-20 07:27:22 -04:00
|
|
|
db_migrate_and_status database_url_db_name
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def db_schema_dump
|
|
|
|
Dir.chdir(app_path) do
|
2017-09-03 12:55:26 -04:00
|
|
|
rails "generate", "model", "book", "title:string"
|
|
|
|
rails "db:migrate", "db:schema:dump"
|
2012-08-30 21:50:30 -04:00
|
|
|
schema_dump = File.read("db/schema.rb")
|
|
|
|
assert_match(/create_table \"books\"/, schema_dump)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:schema:dump without database_url" do
|
2012-08-30 21:50:30 -04:00
|
|
|
db_schema_dump
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:schema:dump with database_url" do
|
2012-08-30 21:50:30 -04:00
|
|
|
set_database_url
|
|
|
|
db_schema_dump
|
|
|
|
end
|
|
|
|
|
2014-03-20 07:27:22 -04:00
|
|
|
def db_fixtures_load(expected_database)
|
2012-08-30 21:50:30 -04:00
|
|
|
Dir.chdir(app_path) do
|
2017-09-03 12:55:26 -04:00
|
|
|
rails "generate", "model", "book", "title:string"
|
2019-02-18 14:06:28 -05:00
|
|
|
reload
|
2017-09-03 12:55:26 -04:00
|
|
|
rails "db:migrate", "db:fixtures:load"
|
2019-02-18 14:06:28 -05:00
|
|
|
|
2019-12-17 22:34:01 -05:00
|
|
|
assert_match expected_database, ActiveRecord::Base.connection_db_config.database
|
2012-08-30 21:50:30 -04:00
|
|
|
assert_equal 2, Book.count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:fixtures:load without database_url" do
|
2012-08-30 21:50:30 -04:00
|
|
|
require "#{app_path}/config/environment"
|
2019-09-12 13:10:57 -04:00
|
|
|
db_fixtures_load ActiveRecord::Base.configurations[Rails.env][:database]
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:fixtures:load with database_url" do
|
2012-08-30 21:50:30 -04:00
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
set_database_url
|
2014-03-20 07:27:22 -04:00
|
|
|
db_fixtures_load database_url_db_name
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:fixtures:load with namespaced fixture" do
|
2014-10-11 22:08:50 -04:00
|
|
|
require "#{app_path}/config/environment"
|
2017-09-14 04:43:56 -04:00
|
|
|
|
|
|
|
rails "generate", "model", "admin::book", "title:string"
|
2019-02-18 14:06:28 -05:00
|
|
|
reload
|
2017-09-14 04:43:56 -04:00
|
|
|
rails "db:migrate", "db:fixtures:load"
|
2019-02-18 14:06:28 -05:00
|
|
|
|
2017-09-14 04:43:56 -04:00
|
|
|
assert_equal 2, Admin::Book.count
|
2014-10-11 22:08:50 -04:00
|
|
|
end
|
|
|
|
|
2014-03-20 07:27:22 -04:00
|
|
|
def db_structure_dump_and_load(expected_database)
|
2012-08-30 21:50:30 -04:00
|
|
|
Dir.chdir(app_path) do
|
2017-09-03 12:55:26 -04:00
|
|
|
rails "generate", "model", "book", "title:string"
|
|
|
|
rails "db:migrate", "db:structure:dump"
|
2012-08-30 21:50:30 -04:00
|
|
|
structure_dump = File.read("db/structure.sql")
|
2017-01-18 21:40:59 -05:00
|
|
|
assert_match(/CREATE TABLE (?:IF NOT EXISTS )?\"books\"/, structure_dump)
|
2017-09-03 12:55:26 -04:00
|
|
|
rails "environment", "db:drop", "db:structure:load"
|
2019-12-17 22:34:01 -05:00
|
|
|
assert_match expected_database, ActiveRecord::Base.connection_db_config.database
|
2012-08-30 21:50:30 -04:00
|
|
|
require "#{app_path}/app/models/book"
|
2017-12-14 03:30:54 -05:00
|
|
|
# if structure is not loaded correctly, exception would be raised
|
2014-03-14 19:57:36 -04:00
|
|
|
assert_equal 0, Book.count
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:structure:dump and db:structure:load without database_url" do
|
2012-08-30 21:50:30 -04:00
|
|
|
require "#{app_path}/config/environment"
|
2019-09-12 13:10:57 -04:00
|
|
|
db_structure_dump_and_load ActiveRecord::Base.configurations[Rails.env][:database]
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:structure:dump and db:structure:load with database_url" do
|
2012-08-30 21:50:30 -04:00
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
set_database_url
|
2014-03-20 07:27:22 -04:00
|
|
|
db_structure_dump_and_load database_url_db_name
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
|
2017-09-12 03:08:16 -04:00
|
|
|
test "db:structure:dump and db:structure:load set ar_internal_metadata" do
|
|
|
|
require "#{app_path}/config/environment"
|
2019-09-12 13:10:57 -04:00
|
|
|
db_structure_dump_and_load ActiveRecord::Base.configurations[Rails.env][:database]
|
2017-09-12 03:08:16 -04:00
|
|
|
|
2019-12-25 01:07:59 -05:00
|
|
|
assert_equal "test", rails("runner", "-e", "test", "puts ActiveRecord::InternalMetadata[:environment]").strip
|
|
|
|
assert_equal "development", rails("runner", "puts ActiveRecord::InternalMetadata[:environment]").strip
|
2017-09-12 03:08:16 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:structure:dump does not dump schema information when no migrations are used" do
|
2017-09-03 12:55:26 -04:00
|
|
|
# create table without migrations
|
|
|
|
rails "runner", "ActiveRecord::Base.connection.create_table(:posts) {|t| t.string :title }"
|
2014-03-20 13:40:51 -04:00
|
|
|
|
2017-09-03 12:55:26 -04:00
|
|
|
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)
|
2014-03-20 13:40:51 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:schema:load and db:structure:load do not purge the existing database" do
|
2017-09-14 04:43:56 -04:00
|
|
|
rails "runner", "ActiveRecord::Base.connection.create_table(:posts) {|t| t.string :title }"
|
2014-12-16 03:58:34 -05:00
|
|
|
|
2017-09-14 04:43:56 -04:00
|
|
|
app_file "db/schema.rb", <<-RUBY
|
|
|
|
ActiveRecord::Schema.define(version: 20140423102712) do
|
|
|
|
create_table(:comments) {}
|
|
|
|
end
|
|
|
|
RUBY
|
2014-12-16 03:58:34 -05:00
|
|
|
|
2017-09-14 04:43:56 -04:00
|
|
|
list_tables = lambda { rails("runner", "p ActiveRecord::Base.connection.tables").strip }
|
2014-12-16 03:58:34 -05:00
|
|
|
|
2019-12-25 01:07:59 -05:00
|
|
|
assert_equal '["posts"]', list_tables[]
|
2017-09-14 04:43:56 -04:00
|
|
|
rails "db:schema:load"
|
2019-12-25 01:07:59 -05:00
|
|
|
assert_equal '["posts", "comments", "schema_migrations", "ar_internal_metadata"]', list_tables[]
|
2014-12-16 03:58:34 -05:00
|
|
|
|
2017-09-14 04:43:56 -04:00
|
|
|
app_file "db/structure.sql", <<-SQL
|
|
|
|
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
|
|
|
|
SQL
|
2014-12-16 03:58:34 -05:00
|
|
|
|
2017-09-14 04:43:56 -04:00
|
|
|
rails "db:structure:load"
|
2019-12-25 01:07:59 -05:00
|
|
|
assert_equal '["posts", "comments", "schema_migrations", "ar_internal_metadata", "users"]', list_tables[]
|
2014-12-16 03:58:34 -05:00
|
|
|
end
|
|
|
|
|
2015-04-27 04:45:22 -04:00
|
|
|
test "db:schema:load with inflections" do
|
2017-09-14 04:43:56 -04:00
|
|
|
app_file "config/initializers/inflection.rb", <<-RUBY
|
|
|
|
ActiveSupport::Inflector.inflections do |inflect|
|
|
|
|
inflect.irregular 'goose', 'geese'
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
app_file "config/initializers/primary_key_table_name.rb", <<-RUBY
|
|
|
|
ActiveRecord::Base.primary_key_prefix_type = :table_name
|
|
|
|
RUBY
|
|
|
|
app_file "db/schema.rb", <<-RUBY
|
|
|
|
ActiveRecord::Schema.define(version: 20140423102712) do
|
|
|
|
create_table("goose".pluralize) do |t|
|
|
|
|
t.string :name
|
2015-04-27 04:45:22 -04:00
|
|
|
end
|
2017-09-14 04:43:56 -04:00
|
|
|
end
|
|
|
|
RUBY
|
2015-04-27 04:45:22 -04:00
|
|
|
|
2017-09-14 04:43:56 -04:00
|
|
|
rails "db:schema:load"
|
2015-04-27 04:45:22 -04:00
|
|
|
|
2017-09-14 04:43:56 -04:00
|
|
|
tables = rails("runner", "p ActiveRecord::Base.connection.tables").strip
|
|
|
|
assert_match(/"geese"/, tables)
|
2015-04-27 04:45:22 -04:00
|
|
|
|
2017-09-14 04:43:56 -04:00
|
|
|
columns = rails("runner", "p ActiveRecord::Base.connection.columns('geese').map(&:name)").strip
|
2019-12-25 01:07:59 -05:00
|
|
|
assert_equal columns, '["gooseid", "name"]'
|
2015-04-27 04:45:22 -04:00
|
|
|
end
|
|
|
|
|
2017-07-23 06:09:15 -04:00
|
|
|
test "db:schema:load fails if schema.rb doesn't exist yet" do
|
2017-09-03 12:55:26 -04:00
|
|
|
stderr_output = capture(:stderr) { rails("db:schema:load", stderr: true, allow_failure: true) }
|
2019-01-22 03:53:47 -05:00
|
|
|
assert_match(/Run `bin\/rails db:migrate` to create it/, stderr_output)
|
2017-07-23 06:09:15 -04:00
|
|
|
end
|
|
|
|
|
2012-08-30 21:50:30 -04:00
|
|
|
def db_test_load_structure
|
|
|
|
Dir.chdir(app_path) do
|
2017-09-03 12:55:26 -04:00
|
|
|
rails "generate", "model", "book", "title:string"
|
|
|
|
rails "db:migrate", "db:structure:dump", "db:test:load_structure"
|
2012-08-30 21:50:30 -04:00
|
|
|
ActiveRecord::Base.configurations = Rails.application.config.database_configuration
|
2013-12-25 04:54:25 -05:00
|
|
|
ActiveRecord::Base.establish_connection :test
|
2012-08-30 21:50:30 -04:00
|
|
|
require "#{app_path}/app/models/book"
|
2017-12-14 03:30:54 -05:00
|
|
|
# if structure is not loaded correctly, exception would be raised
|
2014-03-14 19:57:36 -04:00
|
|
|
assert_equal 0, Book.count
|
2019-09-12 13:10:57 -04:00
|
|
|
assert_match ActiveRecord::Base.configurations["test"][:database],
|
2019-12-17 22:34:01 -05:00
|
|
|
ActiveRecord::Base.connection_db_config.database
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:test:load_structure without database_url" do
|
2012-08-30 21:50:30 -04:00
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
db_test_load_structure
|
|
|
|
end
|
2013-12-29 06:05:04 -05:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "db:setup loads schema and seeds database" do
|
2018-12-20 12:44:01 -05:00
|
|
|
@old_rails_env = ENV["RAILS_ENV"]
|
|
|
|
@old_rack_env = ENV["RACK_ENV"]
|
|
|
|
ENV.delete "RAILS_ENV"
|
|
|
|
ENV.delete "RACK_ENV"
|
2014-09-03 11:31:38 -04:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
app_file "db/schema.rb", <<-RUBY
|
2014-09-03 11:31:38 -04:00
|
|
|
ActiveRecord::Schema.define(version: "1") do
|
|
|
|
create_table :users do |t|
|
|
|
|
t.string :name
|
|
|
|
end
|
|
|
|
end
|
2019-11-23 19:54:47 -05:00
|
|
|
RUBY
|
2014-09-03 11:31:38 -04:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
app_file "db/seeds.rb", <<-RUBY
|
2019-12-17 22:34:01 -05:00
|
|
|
puts ActiveRecord::Base.connection_db_config.database
|
2018-12-20 12:44:01 -05:00
|
|
|
RUBY
|
2014-09-03 11:31:38 -04:00
|
|
|
|
2018-12-20 12:44:01 -05:00
|
|
|
database_path = rails("db:setup")
|
|
|
|
assert_equal "development.sqlite3", File.basename(database_path.strip)
|
|
|
|
ensure
|
|
|
|
ENV["RAILS_ENV"] = @old_rails_env
|
|
|
|
ENV["RACK_ENV"] = @old_rack_env
|
2014-09-03 11:31:38 -04:00
|
|
|
end
|
2017-09-11 18:06:07 -04:00
|
|
|
|
|
|
|
test "db:setup sets ar_internal_metadata" do
|
|
|
|
app_file "db/schema.rb", ""
|
|
|
|
rails "db:setup"
|
|
|
|
|
|
|
|
test_environment = lambda { rails("runner", "-e", "test", "puts ActiveRecord::InternalMetadata[:environment]").strip }
|
|
|
|
development_environment = lambda { rails("runner", "puts ActiveRecord::InternalMetadata[:environment]").strip }
|
|
|
|
|
2019-12-25 01:07:59 -05:00
|
|
|
assert_equal "test", test_environment.call
|
|
|
|
assert_equal "development", development_environment.call
|
2017-09-11 18:06:07 -04:00
|
|
|
|
|
|
|
app_file "db/structure.sql", ""
|
|
|
|
app_file "config/initializers/enable_sql_schema_format.rb", <<-RUBY
|
|
|
|
Rails.application.config.active_record.schema_format = :sql
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
rails "db:setup"
|
|
|
|
|
2019-12-25 01:07:59 -05:00
|
|
|
assert_equal "test", test_environment.call
|
|
|
|
assert_equal "development", development_environment.call
|
2017-09-11 18:06:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "db:test:prepare sets test ar_internal_metadata" do
|
|
|
|
app_file "db/schema.rb", ""
|
|
|
|
rails "db:test:prepare"
|
|
|
|
|
|
|
|
test_environment = lambda { rails("runner", "-e", "test", "puts ActiveRecord::InternalMetadata[:environment]").strip }
|
|
|
|
|
2019-12-25 01:07:59 -05:00
|
|
|
assert_equal "test", test_environment.call
|
2017-09-11 18:06:07 -04:00
|
|
|
|
|
|
|
app_file "db/structure.sql", ""
|
|
|
|
app_file "config/initializers/enable_sql_schema_format.rb", <<-RUBY
|
|
|
|
Rails.application.config.active_record.schema_format = :sql
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
rails "db:test:prepare"
|
|
|
|
|
2019-12-25 01:07:59 -05:00
|
|
|
assert_equal "test", test_environment.call
|
2017-09-11 18:06:07 -04:00
|
|
|
end
|
2019-03-04 17:57:38 -05:00
|
|
|
|
2019-03-08 15:42:28 -05:00
|
|
|
test "db:seed:replant truncates all non-internal tables and loads the seeds" do
|
2019-03-04 17:57:38 -05:00
|
|
|
Dir.chdir(app_path) do
|
|
|
|
rails "generate", "model", "book", "title:string"
|
|
|
|
rails "db:migrate"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
Book.create!(title: "Remote")
|
|
|
|
assert_equal 1, Book.count
|
|
|
|
schema_migrations = ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.schema_migrations_table_name}\"")
|
|
|
|
internal_metadata = ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.internal_metadata_table_name}\"")
|
|
|
|
|
|
|
|
app_file "db/seeds.rb", <<-RUBY
|
|
|
|
Book.create!(title: "Rework")
|
|
|
|
Book.create!(title: "Ruby Under a Microscope")
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
rails "db:seed:replant"
|
|
|
|
|
|
|
|
assert_equal(
|
|
|
|
schema_migrations,
|
|
|
|
ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.schema_migrations_table_name}\"")
|
|
|
|
)
|
|
|
|
assert_equal(
|
|
|
|
internal_metadata,
|
|
|
|
ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.internal_metadata_table_name}\"")
|
|
|
|
)
|
|
|
|
assert_equal 2, Book.count
|
|
|
|
assert_not_predicate Book.where(title: "Remote"), :exists?
|
|
|
|
assert_predicate Book.where(title: "Rework"), :exists?
|
|
|
|
assert_predicate Book.where(title: "Ruby Under a Microscope"), :exists?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "db:seed:replant does not truncate any tables and does not load the seeds when environment is protected" do
|
|
|
|
with_rails_env "production" do
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
rails "generate", "model", "book", "title:string"
|
|
|
|
rails "db:migrate"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
Book.create!(title: "Remote")
|
|
|
|
assert_equal 1, Book.count
|
|
|
|
schema_migrations = ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.schema_migrations_table_name}\"")
|
|
|
|
internal_metadata = ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.internal_metadata_table_name}\"")
|
|
|
|
books = ActiveRecord::Base.connection.execute("SELECT * from \"books\"")
|
|
|
|
|
|
|
|
app_file "db/seeds.rb", <<-RUBY
|
|
|
|
Book.create!(title: "Rework")
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
output = rails("db:seed:replant", allow_failure: true)
|
|
|
|
assert_match(/ActiveRecord::ProtectedEnvironmentError/, output)
|
|
|
|
|
|
|
|
assert_equal(
|
|
|
|
schema_migrations,
|
|
|
|
ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.schema_migrations_table_name}\"")
|
|
|
|
)
|
|
|
|
assert_equal(
|
|
|
|
internal_metadata,
|
|
|
|
ActiveRecord::Base.connection.execute("SELECT * from \"#{ActiveRecord::Base.internal_metadata_table_name}\"")
|
|
|
|
)
|
|
|
|
assert_equal 1, Book.count
|
|
|
|
assert_equal(books, ActiveRecord::Base.connection.execute("SELECT * from \"books\""))
|
|
|
|
assert_not_predicate Book.where(title: "Rework"), :exists?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-04-02 10:11:09 -04:00
|
|
|
|
|
|
|
test "db:prepare setup the database" do
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
rails "generate", "model", "book", "title:string"
|
|
|
|
output = rails("db:prepare")
|
2019-04-02 12:50:28 -04:00
|
|
|
assert_match(/CreateBooks: migrated/, output)
|
2019-04-02 10:11:09 -04:00
|
|
|
|
2019-04-02 12:50:28 -04:00
|
|
|
output = rails("db:drop")
|
|
|
|
assert_match(/Dropped database/, output)
|
|
|
|
|
|
|
|
rails "generate", "model", "recipe", "title:string"
|
|
|
|
output = rails("db:prepare")
|
2019-04-02 11:45:37 -04:00
|
|
|
assert_match(/CreateBooks: migrated/, output)
|
2019-04-02 12:50:28 -04:00
|
|
|
assert_match(/CreateRecipes: migrated/, output)
|
2019-04-02 10:11:09 -04:00
|
|
|
end
|
|
|
|
end
|
2019-07-24 02:50:23 -04:00
|
|
|
|
|
|
|
test "db:prepare does not touch schema when dumping is disabled" do
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
rails "generate", "model", "book", "title:string"
|
|
|
|
rails "db:create", "db:migrate"
|
|
|
|
|
|
|
|
app_file "db/schema.rb", "Not touched"
|
|
|
|
app_file "config/initializers/disable_dumping_schema.rb", <<-RUBY
|
|
|
|
Rails.application.config.active_record.dump_schema_after_migration = false
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
rails "db:prepare"
|
|
|
|
|
|
|
|
assert_equal("Not touched", File.read("db/schema.rb").strip)
|
|
|
|
end
|
|
|
|
end
|
2012-08-30 21:50:30 -04:00
|
|
|
end
|
|
|
|
end
|
Run rake tasks all at once in rake dbs test for a speed up
Before:
Finished tests in 56.245787s, 0.2133 tests/s, 0.0000 assertions/s.
12 tests, 0 assertions, 0 failures, 0 errors, 0 skips
After:
Finished tests in 42.401416s, 0.2830 tests/s, 0.0000 assertions/s.
12 tests, 0 assertions, 0 failures, 0 errors, 0 skips
2012-12-06 18:41:00 -05:00
|
|
|
end
|