-
Notifications
You must be signed in to change notification settings - Fork 24
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
rake db:test:prepare ってどこでどういうふうに定義されているんだろう #965
Comments
rake コマンドのヘルプを見ると、 $ rake -h
rake [-f rakefile] {options} targets...
Options are ...
(中略)
-W, --where [PATTERN] Describe the tasks (matching optional PATTERN), then exit. というオプションがある。これを使うと、そのタスクが定義されているファイルと行がわかる。 この $ rake -W db:test:prepare
rake db:test:prepare /usr/local/rbenv/versions/2.0.0-p598/lib/ruby/gems/2.0.0/gems/activerecord-4.0.4/lib/active_record/railties/databases.rake:368:in `block (2 levels) in <top (required)>' と表示されるので、ここをエディタで開いてみよう 👀 |
db_namespace = namespace :db do
(中略)
namespace :test do
(中略)
# desc 'Check for pending migrations and load the test schema'
task :prepare => [:environment, :load_config] do
unless ActiveRecord::Base.configurations.blank?
db_namespace['test:load'].invoke
end
end
end
end |
深い… |
タスクの中核は unless ActiveRecord::Base.configurations.blank?
db_namespace['test:load'].invoke
end となっている。
# Returns fully resolved configurations hash
def self.configurations
@@configurations
end で、設定される値としては、直上に ##
# Contains the database configuration - as is typically stored in config/database.yml -
# as a Hash.
#
# For example, the following database.yml...
#
# development:
# adapter: sqlite3
# database: db/development.sqlite3
#
# production:
# adapter: sqlite3
# database: db/production.sqlite3
#
# ...would result in ActiveRecord::Base.configurations to look like this:
#
# {
# 'development' => {
# 'adapter' => 'sqlite3',
# 'database' => 'db/development.sqlite3'
# },
# 'production' => {
# 'adapter' => 'sqlite3',
# 'database' => 'db/production.sqlite3'
# }
# }
def self.configurations=(config)
@@configurations = ActiveRecord::ConnectionHandling::MergeAndResolveDefaultUrlConfig.new(config).resolve
end と定義してある。 |
DB の情報がハッシュになってるのね。 |
つまり、DB の情報がひとつもなければ # /usr/local/rbenv/versions/2.0.0-p598/lib/ruby/gems/2.0.0/gems/activerecord-4.0.4/lib/active_record/railties/databases.rake:313
namespace :test do
# desc "Recreate the test database from the current schema"
task :load => 'db:test:purge' do
case ActiveRecord::Base.schema_format
when :ruby
db_namespace["test:load_schema"].invoke
when :sql
db_namespace["test:load_structure"].invoke
end
end となっている。 http://railsguides.jp/configuring.html
というもの。( structure.sql というファイルに見覚えがある人がいるはず) |
# /usr/local/rbenv/versions/2.0.0-p598/lib/ruby/gems/2.0.0/gems/activerecord-4.0.4/lib/active_record/railties/databases.rake:323
# desc "Recreate the test database from an existent schema.rb file"
task :load_schema => '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
ensure
if should_reconnect
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ActiveRecord::Tasks::DatabaseTasks.env])
end
end
end おお!!それらしい処理!! |
なんかきっといろいろやってるんだけど |
(だいぶ雑になってまいりました 🍙 ) |
# /usr/local/rbenv/versions/2.0.0-p598/lib/ruby/gems/2.0.0/gems/activerecord-4.0.4/lib/active_record/railties/databases.rake:250
desc 'Load a schema.rb file into the database'
task :load => [:environment, :load_config] do
file = ENV['SCHEMA'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'schema.rb')
if File.exist?(file)
load(file)
else
abort %{#{file} doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter #{Rails.root}/config/application.rb to limit the frameworks that will be loaded.}
end
end |
おおおーー schema.rb を読み込んでいる!! |
ちなみに task に渡されている # /usr/local/rbenv/versions/2.0.0-p598/lib/ruby/gems/2.0.0/gems/activerecord-4.0.4/lib/active_record/railties/databases.rake:4
task :load_config do
ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {}
ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
end で、 |
Rails チュートリアル 6.2.1 で出てきた
rake db:test:prepare
という rake タスクは、どこで定義されていて、どういう内容になってるんだろう?チュートリアルの記述を見るに、なんとなく、development 環境の DB の状態を test 環境にもコピーする、みたいな動きなんだろうとは推測できるけど。(ほぼ @highwide さんの台詞ママ)
The text was updated successfully, but these errors were encountered: