views:

108

answers:

5

I have a rails application where each user has a separate database (taking Joel Spolsky's advice on this). I want to run DB migrations from the rails application to create a new database and tables for this user. What is the easiest way to do this? Maybe db migrations is not the best for this type of thing. Thanks!

+1  A: 

We use seperate configuration files for each user. So in the config/ dir we would have roo.database.yml which would connect to my personal database, and I would copy that over the database.yml file that is used by rails.

We were thinking of expanding the rails Rakefile so we could specify the developer as a environment variable, which would then select a specfic datbase configuration, allowing us to only have one database.yml file. We haven't done this though as the above method works well enough.

roo
A: 

It would be nice if it could be a completely automated process. The following process would be ideal.

  1. A user signs up on our site to use this web app
  2. Migrations are run to create this users database and get tables setup correctly

Is there a way of calling a rake task from a ruby application?

Kevin Kaske
+1  A: 

To answer part of your question, here's how you'd run a rake task from inside Rails code:

require 'rake'
load 'path/to/task.rake'

Rake::Task['foo:bar:baz'].invoke

Mind you, I have no idea how (or why) you could have one database per user.

Jordi Bunster
A: 

Mind you, I have no idea how (or why) you could have one database per user.

The following link covers the how and the why of multiple databases (Maybe this should be another question on stackoverflow?) http://wiki.rubyonrails.org/rails/pages/HowtoUseMultipleDatabases

Kevin Kaske
A: 

Actually I have discovered a good way to run DB migrations from an application:

ActiveRecord::Migrator.migrate("db/migrate/")

Kevin Kaske