rakeのDB関連タスク

Rails本などではDBを作る際にmysqladminを使いましょうと書かれているが、
調べてみると以下のタスクでDBの作成を行ってくれる

rake db:create

ただしconfig/database.ymlの設定が完了している必要がある。

DBを初期化したい場合には

rake db:migrate:reset

を実行する

db:migrate:resetを行うと、db:drop, db:create, db:migrateが順に実行される。

これらは以下に定義されている

rails-2.3.2/lib/tasks/databases.rake

ちなみにdb:resetというのもあるがこれはdb:drop, db:create, db:schema:loadが実行される
ただ、db:schema:loadというのは、ざっと見ただけではよく分からなかった

     desc "Load a schema.rb file into the database"
     task :load => :environment do
       file = ENV['SCHEMA'] || "#{RAILS_ROOT}/db/schema.rb"
       load(file)
     end

db/schema.rbはdb:migrate実行時に生成され、中身はdb/migrate/以下のものを統合している模様。
という事はdb:migrate実行後にmigrateを変更していない、かつDBを初期化したいという場合にdb:schema:loadを使用するdb:resetを使うべき?

db/schema.rbのコメントにもこれ使った方が早いぜとある。

# This file is auto-generated from the current state of the database. Instead of editing this file, 
# please use the migrations feature of Active Record to incrementally modify your database, and
# then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your database schema. If you need
# to create the application database on another system, you should be using db:schema:load, not running
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ついでに大雑把な訳:
このファイルは現状のデータベースから自動生成されます。このファイルを編集する代わりにデータベースの差分変更を行うActive Recordのマイグレーション機能を使ってください。そうすればこのスキーマ定義は再生成されます。

注意:このschema.rb定義はあなたのデータベーススキーマの信頼できる情報源です。他のシステムへこのデータベースを作成する必要があれば、1からすべてのマイグレーションを実行するのではなくdb:schema:loadを使用しましょう。すべてのマイグレーションを実行することには欠陥があり、継続して行える方法ではありません(訳注:なんで??)。(あなたは多くのマイグレーションを作成しているでしょうし、実行するには遅く、そして問題を起こす可能性が高いです。)

このファイルをバージョン管理システムへチェックインすることを強くお勧めします。

終わり