2013/04/01

Rakeコマンド

rakeコマンドはJavaでいうところのAntのようなビルドツールです。Antと同様にタスクという単位で処理を実行し、タスクの依存関係を定義することもできます。
rakeコマンドは、コマンド実行時にカレントディレクトリにあるRakefileを読み込んで処理を行います。(Antがbuild.xmlを読み込んで処理を行うのと同様です。)大きく違うのはデフォルトのタスクが存在するのと、独自にタスクを定義する場合は、Rakefileに直接書くのではなくlib/tasks以下に.rakeという拡張子を付けたファイルを配置し、そこにタスクを定義する点の2点です。

タスクの確認方法

デフォルトの状態でたくさんのタスクが定義されているので、どんなタスクが定義されているか確認する手段を理解しておくのは重要です。
rakeコマンドは以下のオプションを指定することにより定義済みのタスクを確認することができます。

 > rake --tasks (または -T)
 rake about              # List versions of all Rails frameworks and the env...
 rake assets:clean       # Remove compiled assets
 rake assets:precompile  # Compile all the assets named in config.assets.pre...
 rake db:create          # Create the database from DATABASE_URL or config/d...
 rake db:drop            # Drops the database using DATABASE_URL or the curr...
 rake db:fixtures:load   # Load fixtures into the current environment's data...
 rake db:migrate         # Migrate the database (options: VERSION=x, VERBOSE...
 rake db:migrate:status  # Display status of migrations
 rake db:rollback        # Rolls the schema back to the previous version (sp...
 rake db:schema:dump     # Create a db/schema.rb file that can be portably u...
 rake db:schema:load     # Load a schema.rb file into the database
 rake db:seed            # Load the seed data from db/seeds.rb
 rake db:setup           # Create the database, load the schema, and initial...
 rake db:structure:dump  # Dump the database structure to db/structure.sql. ...
 rake db:version         # Retrieves the current schema version number
 rake doc:app            # Generate docs for the app -- also available doc:r...
 rake log:clear          # Truncates all *.log files in log/ to zero bytes
 rake middleware         # Prints out your Rack middleware stack
 rake notes              # Enumerate all annotations (use notes:optimize, :f...
 rake notes:custom       # Enumerate a custom annotation, specify with ANNOT...
 rake rails:template     # Applies the template supplied by LOCATION=(/path/...
 rake rails:update       # Update configs and some other initially generated...
 rake routes             # Print out all defined routes in match order, with...
 rake secret             # Generate a cryptographically secure secret key (t...
 rake stats              # Report code statistics (KLOCs, etc) from the appl...
 rake test               # Runs test:units, test:functionals, test:integrati...
 rake test:recent        # Run tests for {:recent=>"test:prepare"} / Test re...
 rake test:single        # Run tests for {:single=>"test:prepare"}
 rake test:uncommitted   # Run tests for {:uncommitted=>"test:prepare"} / Te...
 rake time:zones:all     # Displays all time zones, also available: time:zon...
 rake tmp:clear          # Clear session, cache, and socket files from tmp/ ...
 rake tmp:create         # Creates tmp directories for sessions, cache, sock...

データベース関連のタスク

タスクの一覧を見ると分かりますが、データベース関連のタスクにはdb:というプレフィックスが定義されています。これらのタスクを利用してデータベース関連の操作を行います。

マイグレーション

マイグレーションを実行するには db:migrateタスクを実行します。
 > rake db:migrate

マイグレーションの状態確認

 > rake db:migrate:status

ロールバック

データベースの状態を1世代前にロールバックするには db:rollback タスクを実行します。
 > rake db:rollback
指定した世代分ロールバックするには STEP オプションを指定します。
以下は5世代前を指定する例です。
 > rake db:rollback STEP=5

ルーティング関連のタスク

Railsではルーティングをconfig/routes.rbに定義します。ファイルを参照することにより定義したルーティング情報を確認することはできますが、直観的に理解するにはrakeコマンドを利用した方が賢明です。
 > rake routes

0 件のコメント:

コメントを投稿