What is the difference between db: test: clone, db: test: clone_structure, db: test: load and db: test: prepare?

You must admit that a newbie to rails and databases, an official explanation at rubyonrails.org, makes all four of these tasks the same. Quote:

rake db:test:clone Recreate the test database from the current environment's database schema rake db:test:clone_structure Recreate the test database from the development structure rake db:test:load Recreate the test database from the current schema.rb rake db:test:prepare Check for pending migrations and load the test schema 

I don’t even know the difference between structure and scheme. And what's the difference between loading the current environment schema and just loading schema.rb?

How similar (or different) are these tasks?

+65
database ruby-on-rails unit-testing
Oct 07 2018-11-22T00:
source share
2 answers

Very good question. If I were at a dead end, so I dived into the rails source and picked up database.rake . Now this is more clear:

db:test:clone is just a combination of db:schema:dump and db:test:load :

 task :clone => %w(db:schema:dump db:test:load) 

db:test:clone_structure uses the file {rails_env} _structure.sql:

 task :clone_structure => [ 'db:structure:dump', 'db:test:purge' ] do # skipped some code, here what happens for MySQL: ActiveRecord::Base.establish_connection(:test) # ... IO.readlines("#{Rails.root}/db/#{Rails.env}_structure.sql").join.split("\n\n").each do |table| ActiveRecord::Base.connection.execute(table) end end 

db:test:load same as db:schema:load , but calls it in the test database:

 task :load => 'db:test:purge' do ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test']) # ... db_namespace['schema:load'].invoke end 

db:test:prepare warns you if any migrations are expected, and if not, either runs db:test:clone_structure (using the file {rails_env} _structure.sql), or db:test:load (using the schema.rb file) , depending on (this is a bit confusing to me, maybe someone can expand on it):

 task :prepare => 'db:abort_if_pending_migrations' do # ... db_namespace[{ :sql => 'test:clone_structure', :ruby => 'test:load' }[ActiveRecord::Base.schema_format]].invoke end 

Hope this clears up! Again, going through the database.rake file is simple and will clear up any other issues that you may have. This link refers to the line that is the start of the namespace: test.

+61
Oct 07 '11 at 23:18
source share
— -

This is actually not quite the same. Any of these tasks containing the word “circuit” acts in the file ... /db/schema.rb. schema.rb is actually the state of your schema after applying all the migrations. It can be performed to restore your circuit, and not to perform all db migrations (which can take a long time if you have many migrations).

Any of the tasks with the word "structure" acts in the file {Rails.env} _structure.sql. This file is used when your schema contains constructs that cannot be expressed in the schema.rb file. For example, if you use functions specific to a specific DBMS. Under the covers, the rails create this file using any scheme dump utility that is suitable for your RDBMS. To restore the schema, it reads the file and executes the SQL statements again using the special tool corresponding to RDBMS.

Rails knows whether to follow the path of schema.rb or the path of struct.sql based on whether you are installed //

config.active_record.schema_format =: sql

in your ... / config / application.rb

+20
Apr 6 2018-12-12T00:
source share



All Articles