Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync parallel test DBs to schema using SHA #36873

Merged
merged 1 commit into from Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions activerecord/lib/active_record/tasks/database_tasks.rb
Expand Up @@ -347,6 +347,24 @@ def schema_up_to_date?(configuration, format = ActiveRecord::Base.schema_format,
ActiveRecord::InternalMetadata[:schema_sha1] == schema_sha1(file)
end

def reconstruct_from_schema(configuration, format = ActiveRecord::Base.schema_format, file = nil, environment = env, spec_name = "primary") # :nodoc:
file ||= dump_filename(spec_name, format)

check_schema_file(file)

ActiveRecord::Base.establish_connection(configuration)

if schema_up_to_date?(configuration, format, file, environment, spec_name)
truncate_tables(configuration)
else
purge(configuration)
load_schema(configuration, format, file, environment, spec_name)
end
rescue ActiveRecord::NoDatabaseError
create(configuration)
load_schema(configuration, format, file, environment, spec_name)
end

def dump_schema(configuration, format = ActiveRecord::Base.schema_format, spec_name = "primary") # :nodoc:
require "active_record/schema_dumper"
filename = dump_filename(spec_name, format)
Expand Down
17 changes: 1 addition & 16 deletions activerecord/lib/active_record/test_databases.rb
Expand Up @@ -8,31 +8,16 @@ module TestDatabases # :nodoc:
create_and_load_schema(i, env_name: Rails.env)
end

ActiveSupport::Testing::Parallelization.run_cleanup_hook do
drop(env_name: Rails.env)
end

def self.create_and_load_schema(i, env_name:)
old, ENV["VERBOSE"] = ENV["VERBOSE"], "false"

ActiveRecord::Base.configurations.configs_for(env_name: env_name).each do |db_config|
db_config.config["database"] += "-#{i}"
ActiveRecord::Tasks::DatabaseTasks.create(db_config.config)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where'd this go? Nothing in reset_to_schema sounds obviously likely to do the create if needed (and from its name, I don't think I'd expect it to anyway)

Copy link
Member Author

@jhawthorn jhawthorn Aug 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

purge does (re)create the database. I was thinking reset in the "rake db:reset" sense, which recreates the database, but reading it again I agree that might be confusing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly about avoiding 'reset' -- that parallel makes sense too.

.. but where does the create actually happen now?

Copy link
Member Author

@jhawthorn jhawthorn Aug 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack! You're right. This does not work when there is no DB 😳 I thought purge would create. I'll fix this and improve the naming.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with reconstruct_from_schema, which I like (I didn't want recreate because create doesn't imply loading the schema, but reconstruct does sound like it loads the DB's structure).

I've made sure this now also creates DBs.

ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config.config, ActiveRecord::Base.schema_format, nil, env_name, db_config.spec_name)
ActiveRecord::Tasks::DatabaseTasks.reconstruct_from_schema(db_config.config, ActiveRecord::Base.schema_format, nil, env_name, db_config.spec_name)
end
ensure
ActiveRecord::Base.establish_connection(Rails.env.to_sym)
ENV["VERBOSE"] = old
end

def self.drop(env_name:)
old, ENV["VERBOSE"] = ENV["VERBOSE"], "false"

ActiveRecord::Base.configurations.configs_for(env_name: env_name).each do |db_config|
ActiveRecord::Tasks::DatabaseTasks.drop(db_config.config)
end
ensure
ENV["VERBOSE"] = old
end
end
end