In this second part of a four-part article series on creating an online bookstore application with Ruby on Rails, you'll learn how to run a migration script and unit tests, create the controller, and more. This article is excerpted from chapter two of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).
Rails uses the development environment by default, so our development database is now updated to match our migration file. If you want to do the migration to some database other than the development database, you can do so by specifying theRAILS_ENVenvironment variable before therakecommand:
$ RAILS_ENV=production rake db:migrate
So what really happened when our migration script was run? First of all, we got a new table calledauthors. But as this was the first migration, Rails also automatically created a table calledschema_info. The table has only one field,version, and there should be only one row in that table at any given moment. The value of the row tells the current migration version of the database schema. The migration scripts use this information to determine which migrations need to be run to get everything up-to-date.
Last, the migration created a file calleddb/schema.rb, which is in the same format as the migration files, and always describes the current state of the whole database schema. After the file was created, future migrations will automatically keep it up-to-date. Therefore, you should never edit it by hand.
Rake (http://rake.rubyforge.org/) is a build language and tool similar to make (www.gnu.org/software/make/) and ant (http://ant.apache.org/). It is written in Ruby and sports its own DSL for handling the maintenance of a Ruby application. Rails uses Rake extensively for many kinds of tasks. The following are some of the most popular Rake tasks used in Rails. For a complete list, run rake -T in the root of your Rails application directory.
rake: Running rake without any parameters will rebuild the test database according to the migrations, and run all unit, functional, and integration tests found in the test directory.
rake db:migrate: Updates the database of the current environment to the latest version. You can specify the target version by appending VERSION=x after the command.
rake db:sessions:create: Creates a table for storing user session data in the database. Rails automatically assigns a session cookie for each user and uses it to track the user. The session mechanism is very useful for tasks like user authentication, as youíll see in Chapter 8.
rake db:sessions:clear: Purges the sessions table. It is a good idea to schedule this command to run on regular intervals to keep the table size from growing rapidly. Every new visitor to the application will result in a new database row in the sessions table.
rake log:clear: Truncates the log files of your Rails applicationís log directory. Just like session data, the log files can get massive over time, so itís a good idea to clear them every once in a while.
rake rails:freeze:gems: Locks your Rails application to the latest version of Rails gems you have installed on your system. Without running this command (or rake rails:freeze:edge), your application will "float" on the latest gem version, which might lead to problems if there are changes in Rails code that break backward-compatibility.
rake rails:freeze:edge: Similar to rake rails:freeze:gems, with the distinction that it locks the Rails code to the latest (possibly unstable) code in the Rails Subversion source code repository. The Rails code is copied to the vendor/rails directory in your application tree.
rake rails:unfreeze: Breaks the connection between the application and Rails version that was created by either of the freeze tasks just described.
rake stats: Outputs useful statistics about your application, including lines of code and the code to test ratio.
Rake is a very powerful tool that you can use to automate many of the repetitive and tedious maintenance tasks in your application. If you want to know more about Rake, Martin Fowler has written an excellent tutorial called "Using the Rake Build Language" (http://martinfowler.com/ articles/rake.html).