提交 ec54acd1 编写于 作者: Y Yves Senn

Merge pull request #16316 from rails/senny/purge_schema_maintenance

purge database before loading schema, take 2
* Deprecate `DatabaseTasks.load_schema` to act on the current connection.
Use `.load_schema_current` instead. In the future `load_schema` will
require the `configuration` to act on as an argument.
*Yves Senn*
* Fixed automatic maintaining test schema to properly handle sql structure
schema format.
Fixes #15394.
*Wojciech Wnętrzak*
* Fix type casting to Decimal from Float with large precision.
*Tomohiro Hashidate*
......
......@@ -399,7 +399,7 @@ def check_pending!(connection = Base.connection)
def load_schema_if_pending!
if ActiveRecord::Migrator.needs_migration?
ActiveRecord::Tasks::DatabaseTasks.load_schema
ActiveRecord::Tasks::DatabaseTasks.load_schema_current
check_pending!
end
end
......
......@@ -240,7 +240,7 @@ db_namespace = namespace :db do
desc 'Load a schema.rb file into the database'
task :load => [:environment, :load_config] do
ActiveRecord::Tasks::DatabaseTasks.load_schema(:ruby, ENV['SCHEMA'])
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV['SCHEMA'])
end
task :load_if_ruby => ['db:create', :environment] do
......@@ -286,7 +286,7 @@ db_namespace = namespace :db do
desc "Recreate the databases from the structure.sql file"
task :load => [:environment, :load_config] do
ActiveRecord::Tasks::DatabaseTasks.load_schema(:sql, ENV['DB_STRUCTURE'])
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:sql, ENV['DB_STRUCTURE'])
end
task :load_if_sql => ['db:create', :environment] do
......@@ -317,9 +317,8 @@ db_namespace = namespace :db do
task :load_schema => %w(db:test:deprecated db:test:purge) do
begin
should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
ActiveRecord::Schema.verbose = false
db_namespace["schema:load"].invoke
ActiveRecord::Tasks::DatabaseTasks.load_schema_for ActiveRecord::Base.configurations['test'], :ruby, ENV['SCHEMA']
ensure
if should_reconnect
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ActiveRecord::Tasks::DatabaseTasks.env])
......@@ -329,12 +328,7 @@ db_namespace = namespace :db do
# desc "Recreate the test database from an existent structure.sql file"
task :load_structure => %w(db:test:deprecated db:test:purge) do
begin
ActiveRecord::Tasks::DatabaseTasks.current_config(:config => ActiveRecord::Base.configurations['test'])
db_namespace["structure:load"].invoke
ensure
ActiveRecord::Tasks::DatabaseTasks.current_config(:config => nil)
end
ActiveRecord::Tasks::DatabaseTasks.load_schema_for ActiveRecord::Base.configurations['test'], :sql, ENV['SCHEMA']
end
# desc "Recreate the test database from a fresh schema"
......
......@@ -184,20 +184,39 @@ def structure_load(*arguments)
end
def load_schema(format = ActiveRecord::Base.schema_format, file = nil)
ActiveSupport::Deprecation.warn(<<-MESSAGE.strip_heredoc)
This method will act on a specific connection in the future.
To act on the current connection, use `load_schema_current` instead.
MESSAGE
load_schema_current(format, file)
end
# This method is the successor of +load_schema+. We should rename it
# after +load_schema+ went through a deprecation cycle. (Rails > 4.2)
def load_schema_for(configuration, format = ActiveRecord::Base.schema_format, file = nil) # :nodoc:
case format
when :ruby
file ||= File.join(db_dir, "schema.rb")
check_schema_file(file)
purge(configuration)
ActiveRecord::Base.establish_connection(configuration)
load(file)
when :sql
file ||= File.join(db_dir, "structure.sql")
check_schema_file(file)
structure_load(current_config, file)
purge(configuration)
structure_load(configuration, file)
else
raise ArgumentError, "unknown format #{format.inspect}"
end
end
def load_schema_current(format = ActiveRecord::Base.schema_format, file = nil, environment = env)
each_current_configuration(environment) { |configuration|
load_schema_for configuration, format, file
}
end
def check_schema_file(filename)
unless File.exist?(filename)
message = %{#{filename} doesn't exist yet. Run `rake db:migrate` to create it, then try again.}
......
......@@ -21,7 +21,11 @@ def drop
FileUtils.rm(file) if File.exist?(file)
end
alias :purge :drop
def purge
drop
create
end
def charset
connection.encoding
......
......@@ -67,7 +67,7 @@ def test_failure
assert_match %r{/app/test/unit/failing_test\.rb}, output
end
test "migrations" do
test "ruby schema migrations" do
output = script('generate model user name:string')
version = output.match(/(\d+)_create_users\.rb/)[1]
......@@ -104,6 +104,95 @@ class UserTest < ActiveSupport::TestCase
assert !result.include?("create_table(:users)")
end
test "sql structure migrations" do
output = script('generate model user name:string')
version = output.match(/(\d+)_create_users\.rb/)[1]
app_file 'test/models/user_test.rb', <<-RUBY
require 'test_helper'
class UserTest < ActiveSupport::TestCase
test "user" do
User.create! name: "Jon"
end
end
RUBY
app_file 'db/structure.sql', ''
app_file 'config/initializers/enable_sql_schema_format.rb', <<-RUBY
Rails.application.config.active_record.schema_format = :sql
RUBY
assert_unsuccessful_run "models/user_test.rb", "Migrations are pending"
app_file 'db/structure.sql', <<-SQL
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
INSERT INTO schema_migrations (version) VALUES ('#{version}');
SQL
app_file 'config/initializers/disable_maintain_test_schema.rb', <<-RUBY
Rails.application.config.active_record.maintain_test_schema = false
RUBY
assert_unsuccessful_run "models/user_test.rb", "Could not find table 'users'"
File.delete "#{app_path}/config/initializers/disable_maintain_test_schema.rb"
assert_successful_test_run('models/user_test.rb')
end
test "sql structure migrations when adding column to existing table" do
output_1 = script('generate model user name:string')
version_1 = output_1.match(/(\d+)_create_users\.rb/)[1]
app_file 'test/models/user_test.rb', <<-RUBY
require 'test_helper'
class UserTest < ActiveSupport::TestCase
test "user" do
User.create! name: "Jon"
end
end
RUBY
app_file 'config/initializers/enable_sql_schema_format.rb', <<-RUBY
Rails.application.config.active_record.schema_format = :sql
RUBY
app_file 'db/structure.sql', <<-SQL
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
INSERT INTO schema_migrations (version) VALUES ('#{version_1}');
SQL
assert_successful_test_run('models/user_test.rb')
output_2 = script('generate migration add_email_to_users')
version_2 = output_2.match(/(\d+)_add_email_to_users\.rb/)[1]
app_file 'test/models/user_test.rb', <<-RUBY
require 'test_helper'
class UserTest < ActiveSupport::TestCase
test "user" do
User.create! name: "Jon", email: "jon@doe.com"
end
end
RUBY
app_file 'db/structure.sql', <<-SQL
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "email" varchar(255));
INSERT INTO schema_migrations (version) VALUES ('#{version_1}');
INSERT INTO schema_migrations (version) VALUES ('#{version_2}');
SQL
assert_successful_test_run('models/user_test.rb')
end
private
def assert_unsuccessful_run(name, message)
result = run_test_file(name)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册