Heroku Rails Rake Task to Sync Production & Local DB
Check out the Parity gem. It offers several commands to do the following Heroku Rails tasks easily -
- Backup DB's
- Restore DB's
- Run
rails console
- Tail logs
- Run migrations
- Deploy
You're of course primarily looking for the first two.
After installation, it expects that you have two git remote
values set named staging
and production
. development
isn't needed as it is assumed to be your local machine.
You can get the git url for the other two environments from your Heroku dashboard -> (your app) -> Settings -> Info
After you have that set up, it's as simple as
production backup
development restore production
The code is pretty simple, so I encourage you to read it. But it's essentially doing exactly what your rake code attempts to do by getting a public URL and restoring it.
Rake Task not working on Heroku
I needed to add :environment
on my rake task
task :my_task => :environment do
# do work
end
i also add the followind line:
config.dependency_loading = true
on production.rb file
Combining rake tasks for rails/heroku not working
I should use backticks (`) instead of the exec
ruby command. Here's what the code should like for the rake deploy_production
to work:
desc 'Push to heroku production, db:migrate, and restart app'
task :deploy_production => ['heroku:push', 'heroku:migrate', 'heroku:restart']
namespace :heroku do
task :push do
puts 'Deploying app to Heroku...'
`git push heroku master`
end
task :migrate do
puts 'Running database migrations ...'
`heroku run rake db:migrate`
end
task :restart do
puts 'Restarting app servers ...'
`heroku restart`
end
end
I had problems posting it immediately because I am new to stackoverflow, and I can't post an answer immediately to my own question.
The reason I prefer backticks over system
in ruby is because of the slight advantage discussed here: Ruby, Difference between exec, system and %x() or Backticks
Local version works but the production version does not
The problem appears to be an open comment (*/) in one of my CSS files. This came to my attention when I attempted to compile the assets with the command:
RAILS_ENV=production bundle exec rake assets:precompile
and got the error...
rake aborted!
Invalid CSS after "*/": expected identifier, was "/* Sections"
I deleted the extraneous "*/", forced a push to Heroku (git push -f heroku master
), deleted index.html and voila the right page was served.
Thank you to all for the help and recommendations!
unable to migrate database on production while running rake task
Looks like your database is not ready on Heroku.
Please veryify to have installed a mysql database for your application and then run heroku run db:setup
to build the schema and seed it.
Why does Rake task enhancement differ between my local environment and when deploying to Heroku Cedar?
i've been looking into this issue and I found out that the behavior of the assets:precompile
depending on if RAILS_ENV
and RAILS_GROUPS
are both set or not take a look at this locally.
# This works
→ bundle exec rake assets:precompile RAILS_ENV=production
>>>>>>>> hello from precompile:nondigest
>>>>>>>> hello from precompile
# This works
→ bundle exec rake assets:precompile RAILS_GROUPS=assets
>>>>>>>> hello from precompile:nondigest
>>>>>>>> hello from precompile
→
# This does not work :'(
→ bundle exec rake assets:precompile RAILS_ENV=production RAILS_GROUPS=assets
>>>>>>>> hello from precompile:nondigest
→
The problem comes from https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/sprockets/assets.rake in invoke_or_reboot_rake_task
method if you replace the Rake::Task[task].invoke
line with ruby_rake_task task
then it works like you would expect it to. I've been poking around on exactly why this is, and haven't found the reason.
Since both variables are set in the Heroku build pack, you could create a custom build pack without setting both GROUP and ENV settings, though I think that is overkill. In this scenario you should be able to enhance assets:precompile:primary
or assets:precompile:all
and achieve an outcome similar to your desired intent.
Failed to deploy rails6 app to heroku due to Can't detect rake task
I had the same issue today. All I did to solve it was to move the spring
gem from the development
group to the "main" group in my Gemfile
.
Gemfile before:
ruby '2.7.2'
group :development do
gem 'spring'
end
Gemfile before:
ruby '2.7.2'
gem 'spring'
I hope it also works for you.
Related Topics
Why Is Foreman Gem Ignoring the Port Environment Variable
Using Rest-Client to Download a File to Disk Without Loading It All in Memory First
Ruby No Implicit Conversion of Fixnum into String (Typeerror)
How Does One Downgrade Jekyll to Work with Github Pages
Capturing Logger Output Inside a Method
What Do I Need to Do to Get Hash.From_Xml() to Work
Ruby on Rails: Two References with Different Name to the Same Model
Gem Install Dm-Postgres-Adapter Build Error
How to Create a Folder (If Not Present) with Logger.New
3.Days.Ago, 2.Hours.From_Now etc Without Rails
Using Ruby CSV to Extract One Column
Emacs Ruby Method Parameter Indentation
Download and Write .Tar.Gz Files Without Corruption
Using Rvm, But Can't Set Current Ruby Version (Ubuntu 11.10)