Why we get an error on the command rake db:migrate
Rails Error: Unable to access log file. Please ensure that /home/mahaloo/mahaloo/releases/20120329200051/log/development.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
rake aborted!
unable to open database file
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Whats wrong there. I tryed to deploy via capistrano, i used this tutorial to setup capistrano http://teachmetocode.com/screencasts/basic-deployment-with-capistrano/
You're either missing the log directory or file. Have you run cap deploy:setup ?
Otherwise manually create the log file first.
This is likely because you're following the practice of not checking your database.yml into source control. If that is the case, you can make a copy of your database.yml in your deploy shared/config folder, and create a Capistrano task to symlink that back into your release folder. Something like this (in namespace deploy)
task :create_symlinks do
run "ln -nfs #{shared_path}/db/production.sqlite3 #{release_path}/db/production.sqlite3"
run " -nfs #{shared_path}/config/ldap.yml #{release_path}/config/ldap.yml"
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
Then call this in a hook
after "deploy:finalize_update", "deploy:create_symlinks"
after "deploy:finalize_update", "deploy:migrate"
I think that would work, that's how we're step up on our project. This is similar to these questions:
database.yml deployment best practice
Capistrano - can't deploy my database.yml
How to manage Rails database.yml
Have you try with sudo if your enviroment its on linux, for example, i got that error trying to run the migration, rake db:migrate, so i used sudo rake db:migrate and that's work, maybe because the rake when its trying to consult development.log doesn't have the right permissions or something like that.
Related
I'm trying to run the following command on my remote server (either via capistrano or ssh):
bundle exec RAILS_ENV=production script/delayed_job start
But I'm getting this error message: bundler: not executable: script/delayed_job
Never saw this before, and google had nothing for me. Any idea what might be the problem?
Maybe it does not have permissions to run? Try running this command
chmod +x script/delayed_job
and then executing the file again.
I am not sure if if it is a fundamental misunderstanding of the capistrano rbenv gem or some issue with the gem itself, but I had similar issue with delayed_job, where the bin/delayed_job file just would not get the executable permission when copied to the server by capistrano. So I wrote a task which I had run before invoking the delayed_job:restart task.
Note - Adding this answer because earlier one is from 2014, and also I wanted to show how to add the task, so the permission change can happen during deployment itself.
Created a task in lib/capistrano/tasks folder (in namespace delayed_job):
namespace :delayed_job do
desc 'Ensure that bin/delayed_job has the permission to be executable. Ideally, this should not have been needed.'
task :ensure_delayed_job_executable do
on roles(delayed_job_roles) do
within release_path do
execute :chmod, :'u+x', :'bin/delayed_job'
end
end
end
end
after 'deploy:publishing', 'deploy:restart'
namespace :deploy do
task :restart do
invoke 'delayed_job:ensure_delayed_job_executable'
invoke 'delayed_job:restart'
end
end
I'm trying to run my seed file on a remote server using capistrano. My deploy is OK, so there is no issue there. Here is the code for running the seed file in config/deploy.rb
namespace :seed do
desc "Run a task on a remote server."
# run like: cap staging rake:invoke task=a_certain_task
task :default do
run("cd #{deploy_to}/current; /usr/bin/env bundle exec rake #{ENV['db:seed']} RAILS_ENV=#{rails_env}")
end
end
I am evoking this task by running 'cap seed'.
Whats weird is it looks like tests are running when I run this..HERE is a snippet.
Maybe the problem is with #{ENV['db:seed']} part. Isn't it should be just db:seed. The eniviroment variable db:seed doesn't exist so You are calling a pure rake command.
Try this:
run("cd #{deploy_to}/current; /usr/bin/env bundle exec rake db:seed RAILS_ENV=#{rails_env}")
I am using Linode with Ubuntu 10.04 and Capistrano, Unicorn, & Nginx to deploy.
How do I do the equivalent of heroku run rake db:reset with this setup? Is it as simple as cap deploy:cold again to run the migrations?
I've already deployed and want to drop all databases and rerun all the migrations but am not sure which commands to run with this setup to do so.
I wrote a tiny little file you can copy to run arbitrary rake tasks via capistrano: http://jessewolgamott.com/blog/2012/09/10/the-one-where-you-run-rake-commands-with-capistrano/
once setup, you can:
cap sake:invoke task="db:reset"
For Capistrano 3 without actual dropping the database. Use bundle exec cap db:reset
namespace :db do
desc 'Resets DB without create/drop'
task :reset do
on primary :db do
within release_path do
with rails_env: fetch(:stage) do
execute :rake, 'db:schema:load'
execute :rake, 'db:seed'
end
end
end
end
end
You could add the following to your deploy.rb file
namespace :custom do
task :task do
run "cd #{current_path} && bundle exec rake db:reset RAILS_ENV=#{rails_env}"
end
end
Then run cap custom:task to clear the database.
If you are using Capistrano 3, consider using the capistrano-rails-collection.
You can also use copy the code directly from db.rake file from the repository.
Or, if you want a full-fledged solution to run all your rake tasks on a remote server, check out the Cape gem.
I am using capistrano, and the whenever gem, on a fresh deploy to a server without the whenever gem installed, capistrano attempts to run
whenever --clear-crontab
BEFORE the rake gems:install command has been run, its clear (from this) that this command runs after deploy_code but so does my command that installs the gems (below)..
after "deploy:update_code", "deploy:symlink_config"
deploy.task :symlink_config, :roles => :app do
# create a symlink to the database.yml file located in the shared_path
run "ln -nsf #{shared_path}/config/database.yml #{current_release}/config"
# install any missing gems
run "cd #{current_release} && sudo rake gems:install --trace RAILS_ENV=#{rails_env}"
# migrate the database
run "cd #{current_release} && rake db:migrate --trace RAILS_ENV=#{rails_env}"
end
Is there a way to order these tasks, because on a cold deploy I always get whenever: not found and have to manually install the whenever gem on the remote server
What I ended up doing is removing the require "whenever/capistrano" from the config\deploy.rb to avoid the "automatic" deploy. Instead I have added a task that executes the --clear-crontab and --update-crontab. This works as it will execute in the sequence I set it to.
I have based it off this post, which deals with a slightly different problem but has the same solution - not to use the "automatic" integration with capistrano.
I have a rails app that is not in the root directory of the repository. When it is deployed, some other static files are deployed with it in a parent directory. The structure is something like this:
root
-- otherstuff
-- railsapp
When I do a deployment with cap deploy:migrations, the Capistrano command that gets executed looks like this, which of course doesn't work:
cd /u/apps/minicart/releases/20100717215044; rake RAILS_ENV=staging db:migrate
How do I change this so that it will be:
cd /u/apps/minicart/releases/20100717215044/railsapp; rake RAILS_ENV=staging db:migrate
I made it work by adding a task that executes this command after deploy:finalize_update, but I would prefer to use the built in method, plus my hacked version is executed with every deployment.
Any advice would be appreciated.
Tim
This turned out to be very simple.
I added a deploy namepace to my deploy.rb file and then redefined the migrate method. Now my method runs on cap deploy:migrations.
namespace :deploy do
desc "Migrating the database"
task :migrate, :roles => :app do
run <<-CMD
cd #{release_path}/minicart; RAILS_ENV=#{stage} rake db:migrate
CMD
end
end