views:

589

answers:

5

For a RoR installation, is there any way to run rake commands without root access?

To put it another way, is there any way to get db:create and db:migrate to be run without root access (perhaps automatically or something)? Or can I run rake commands from a RoR controller?

A: 

Well, it is a bit of a chicken-egg problem, you may be able to start your RoR instance without the database created but I doubt it. If your hosting provider is able to host RoR apps, there must be a way for them to run rake for you or to let you run it somehow.

Keltia
I'll check that. I think that the RoR instance might start without the db created, in fact... as long as I don't call a controller that needs it...
Yar
Nope, there's no problem, you can start the RoRT without the db create being run...
Yar
A: 

Since it sounds like you are running into troubles with creating the database, is there a way to do it from the hosting control panel? Still, how are you going to migrate your database? Sounds like you might need to look at a new host. I use Slicehost and think they are great :)

Scott Miller
Thanks Scott. No problem creating the database nor migrating with the MySql dump scripts, etc. But I'd prefer to not even bother...
Yar
+3  A: 

Take a look at rails-2.X.X/lib/tasks/databases.rake and you can see the code called to create, drop, and migrate your database.

Once a rails environment is initialized, you can use the code inside the rake task file to create, drop, and migrate.

I do not know if you can do this at the controller level before it errors, but you can always try. You could also do it after rails has finished initializing in the environment file.

config/environment.rb

...

ActiveRecord::Migration.verbose = false
ActiveRecord::Migrator.migrate(File.join(Rails.root, 'db', 'migrate'))
Samuel
interesting. I'll be figuring this out shortly along with my first rails app/deploy. thanks!
Yar
A: 

Give this code a try:

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
Rake::Task["db:version"].invoke

I just tried it in ./script/console and that worked. It wouldn't work without the require lines.

I use it to call other rake tasks from a rake task (when it's not a pre-req but something that has to happen in the middle).

Note, that won't get you any of the output from the command. If you want that you could just go with good old backticks and run the command like this:

output = `rake db:version`

That'll launch another process, but I don't think there's a problem with that.

Otto
A: 

Just to be clear, you do not need root access, you need just shell (ssh) access to that machine.

How are you deploying it without access ? If you're using capistrano than you already have shell access and it can run those tasks for you.

Mihai A