Thursday, February 19, 2009

Rake: A basic intro of Rake and db migration using rake

Rake is a simple ruby build program with capabilities similar to make. Rake has the following features:

  • Rakefiles (rake‘s version of Makefiles) are completely defined in standard Ruby syntax. No XML files to edit. No quirky Makefile syntax to worry about (is that a tab or a space?)
  • Users can specify tasks with prerequisites.
  • Rake supports rule patterns to synthesize implicit tasks.
  • Flexible FileLists? that act like arrays but know about manipulating file names and paths.
  • A library of prepackaged tasks to make building rakefiles easier.

You can get the tasks lists by executing ruby --tasks a selective approach of previous would be ruby --tasks db which shows all the tasks associated with db

  • rake db:fixtures:load # Load fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y
  • rake db:migrate # Migrate the database through scripts in db/migrate. Target specific version with VERSION=x
  • rake db:remove_unknown # Remove all migrations in db/migrate that is missing from ENVMIGRATION_DIR? directory
  • rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
  • rake db:schema:load # Load a schema.rb file into the database
  • rake db:sessions:clear # Clear the sessions table
  • rake db:sessions:create # Creates a sessions table for use with CGI::Session::ActiveRecordStore?
  • rake db:status # Display schema status in parsable YAML format
  • rake db:structure:dump # Dump the database structure to a SQL file
  • rake db:test:clone # Recreate the test database from the current environment's database schema
  • rake db:test:clone_structure # Recreate the test databases from the development structure
  • rake db:test:prepare # Prepare the test database and load the schema
  • rake db:test:purge # Empty the test database
  • rake db:unmigrate # Remove a specific migration based on MIGRATION_FILE (no validation is done)

  • rake db:migrate : (Migrate the database through scripts in db/migrate. Target specific version with VERSION=x)
  • Generate script for your acction example: script/generate migration DummyMigration?
  • it creates a file something like 01211257749_dummy_migration.rb in the directory vim db/migrate/
  • and also makes an entry in history.txt in the same folder do vim db/migrate/history.txt to check that

  • Revert back your changes using the following syntax

rake db:unmigrate MIGRATION_FILE=db/migrate/01211257749_dummy_migration.rb
rake --tasks db

Structure Dump

  • Syntax: rake db:structure:dump * creates a file something like development_structure.sql on the db folder * you can change the default schema by modifying the database config file
  • cd config/
  • vim database.yml
  • rake db:structure:dump RAILS_ENV=production
  • creates a structure dump for vim production_structure.sql

1 comment:

  1. I believe when you wrote "You can get the tasks lists by executing ruby --tasks" you actually meant to say executing rake --tasks.

    For db tasks, try

    $ rake --tasks db