The Hitchhiker’s Guide to Ruby On Rails Galaxy

Records of my voyage through RoR Galaxy

Posts Tagged ‘migration’

In Rails 2.0, use attribute pairs to create add and remove column migrations.

Posted by arjunghosh on February 14, 2008

We see a very useful feature in Rails 2.0 as defined in Changeset 7422, where now we can generate migration for adding and removing columns by directly passing attribute:type pairs to the migration generator.
Now for example, we need to add a column called ‘dept’ to students table where the model will be called ‘Student’.
Now previously we used something like:-

script/generate migration add_dept_to_student

Now in case of new better way of writing migration in Rails 2.0, we write it as follows:-

script/generate migration AddDeptToStudent dept:string

Here the migration name AddDeptToStudent holds the key. ‘Add’ specifies the we want to add column(s) and ‘Student’ separated by ‘To’ specifies the table.
Similarly, if we need to remove a column ‘dept’ :

script/generate migration RemoveDeptFromStudent dept:string

Similar to above, the migration name RemoveDeptFromStudent holds the key. ‘Remove’ specifies the we want to remove column(s) and ‘Student’ separated by ‘From’ specifies the table.

Advertisements

Posted in Uncategorized | Tagged: , , | 3 Comments »

Excellent rake task code snippet to completely rebuild & migrate your db

Posted by arjunghosh on January 4, 2008

The other day I came across a code snippet which completely automates the re-building and migrating of database. It also run all my rake task for db which I can include in it. All this is done by single line of code. I thought I will share it here.

1. First you need to create a rake file in your /lib/tasks folder of your Rails application. Say it is called

db_populate.rake

2. In this file, add all your customized rake tasks for your db like adding initial seed data to db,etc.

3. Then add the following code just before the end of the rake task :

desc ‘rebuild the database from scratch’

task :rebuild=> :environment do
p “Beggining the rebuild”
Rake::Task[“db:drop_and_create”].invoke
Rake::Task[“db:migrate”].invoke
Rake::Task[“db:populate”].invoke
end

desc ‘destroy your database’

task :drop_and_create=> :environment do
db_name = ActiveRecord::Base.connection.current_database
p “Dropping the database”
ActiveRecord::Base.connection.drop_database(db_name)
p “Creating the database”
ActiveRecord::Base.connection.create_database(db_name)
db_config = YAML.load_file(File.join(RAILS_ROOT, “config”, “database.yml”))
ActiveRecord::Base.establish_connection(db_config[RAILS_ENV])
end

Now whenever you need to re-create your db and run all your migrations and run all your tasks for db, you need not go and run multiple commands like db:migrate, then db:your_rake_tasks, etc.

You can simply run the following:

rake db:rebuild

🙂 cool right.

Posted in Uncategorized | Tagged: , , , | 2 Comments »

db:session:create does not seem to work for creating Sessions table

Posted by arjunghosh on January 4, 2008

Well we all know when have multiple machines accessing the same session information, we need need to store the session information somewhere so that all the servers can access it. The solution is store the session information in a database and access the session information from the database.

So with that purpose in mind, I searched some rails books and found that rails has a way to putting sessions in the database.

rake db:session:create

So went ahead an entered this command at my console for my application. And lo behold, what has happened!!!

I get the following error:

(in F:/testdev/trunk)
rake aborted!
Don’t know how to build task ‘db:session:create’
(See full trace by running task with –trace)

So I checked the rake –help and also googled it. It seem lot of them have the same error. In fact it seem that AWS 2nd book also has this error as per an excellent writeup in a blog.

And here I found the solution:

rake db:sessions:create

The issue was with a single ‘s’. Now I use a rake version 0.7.3. Is that the problem? Am I using a lower version of rake?

Posted in Uncategorized | Tagged: , , , , , | 1 Comment »