August 19, 2008

Update Rails/Rake/Gems

Filed under: gems, rails, rake — oomoo @ 9:09 am

Keep your Rails/Rake/Gems up-to-date by running these commands:

Update the RubyGems system itself (required by Rails 2.0).
gem update –system

Update Rails-specific gems and installs any new components (specifically Active Resource).
gem install rails

Update Rake, the build tool used by Rails.
gem update rake

Update the Ruby bindings for the SQLite3 database.
gem update sqlite3-ruby

For an existing project, also do this:

rake rails:update

…and edit the config/environment.rb file and set the correct GEM Version:

RAILS_GEM_VERSION = ‘2.1.0’ unless defined? RAILS_GEM_VERSION

You might want to make sure your migrations are up-to-date:

rake db:migrate
rake db:schema:dump


Check your GEM version:
gem –version


Downgrade/Rollback GEM
gem -v


List all the installed gems by typing:

gem list


View the documentation for all the installed gems by typing:

gem server

To access the documentation, point your browser to http://localhost:8808/

This command starts a web server on port 8808 by default. 

You’ll see a list of the gems you have installed with a link to their RDoc documentation.


Install SQLite3:
gem install sqlite3-ruby


Force Rails to use MySQL (instead of the default SQLite3):

rails -d mysql myapp


March 11, 2008

Migration-Related Rake Commands

Filed under: migration, rails, rake — oomoo @ 1:28 pm

Here are some of the Rake commands that are used when doing migrations (and some tips)…

rake db:migrate   Migrate the database through scripts in db/migrate. Target specific version with VERSION=x  

rake db:schema:dump  Dump the database structure into the “master migration script” db/schema.rb

rake db:structure:dump  Dump the database structure to a SQL file  

rake db:schema:load  Load a schema.rb file into the database

rake db:fixtures:load  Load fixtures into the current environment’s database. Load specific fixtures using FIXTURES=x,y  

rake db:sessions:clear  Clear the sessions table

rake db:sessions:create Creates a sessions table for use with CGI::Session::ActiveRecordStore  

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  

Next “Migration Number” that needs to be applied, is stored in table “schema_info” field named “version” 

Test the Migration Scrips…   “rake db:migrate –dry-run –trace” 

Apply the Migration Scripts…  “rake db:migrate”

    Did an error occur?  See the “troubleshooting” section below. 

Roll back a script…  “rake db:migrate VERSION=004”    (substitute the desired “version #” for the number “4”) (Use the number “0” to drop the tables) 

    If you manually “dropped” the table yourself, you need to add the table back (just add one field), so the rollback will work.
    OR, you could manually change the number stored in “schema_info.version” to a number prior to when the table existed. 
    It is a good idea to “roll back” EVERY script, just to make sure your “self.down” works properly (before the next \db\migrate\ script gets created). 

Apply the Migration Script…Again  “rake db:migrate”   (After it rolls back successfully, just “rake db:migrate” again to put your migration back into effect.) 

Regenerate the “schema.rb” file to include your new model…  “rake db:schema:dump” 

Create a free website or blog at WordPress.com.