I went and made some changes to my database and models. now when I run:
cap production deploy
I get an error:
PG::DuplicateTable: ERROR: relation "ideas" already exists
If this were on my local computer I would just reset the db:
rails db:migrate:reset
but since I am using capistrano I don't really know what cap task would help here? If there is one?
I did attempt to use the capistrano-rails-collection gem which has a method
cap production rails:rake:db:reset
Rails 5 made changes to what you can run in a production environment. So now I get an error like this.
01 rake aborted!
01 ActiveRecord::ProtectedEnvironmentError: You are attempting to run a destructive action against your 'production' database.
01 If you are sure you want to continue, run the same command with the environment variable:
01 DISABLE_DATABASE_ENVIRONMENT_CHECK=1
I am not clear on how I would run cap with an environment variable so either I need a way to fix the initial error from the deploy command or I need to know how to run a cap task with this environment variable set. Open to other options though.
Related
When I try to deploy with Capistrano it gets to assets:precompile and I get this message:
01:08 deploy:assets:precompile
01 ~/.rvm/bin/rvm default do bundle exec rake assets:precompile
01 rake aborted!
01 Devise.secret_key was not set. Please add the following to your Devise initializer:
01
01 config.secret_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
01
01 Please ensure you restarted your application after installing Devise or setting the key.
I don't want to add my secret key into devise.rb because I don't want it in version control. I am using an environment variable for secret_key_base which Devise.secret_key is supposed to fall back on. In fact, when I ssh into the server, manually navigate to the failed release and go into the rails console I find that Devise.secret_key works fine. It seems the only time it doesn't work is during the Capistrano deploy.
Edit: I can also manually run bundle exec rake assets:precompile just fine through ssh. The problem just seems to be Capistrano, but my environment variables are in /etc/environment so they should be loaded.
You need to declare the ENV variable in your capistrano production file.
Set an env variable on your computer(or the computer, or virtual machine where deploy is being called), then add the following code to your capistrano production file.
in config/deploy/production.rb
set :default_env, {
"SECRET_KEY_BASE" => ENV['PRODUCTION_SECRET_KEY'],
....
}
You should always set env variable like this when using capistrano, because sometimes depending on the way you have capistrano set up, env variables on the server will not work, you could be using a different user to deploy, or you could be using rbenv or rvm. Whatever the reason, this is the best way to set env variables when using capistrano. And you don't have to set them on the server or keep them in version control.
Reason
Capistrano use non-login, non-interactive shell modes, which doesn't load you environment variables.
When you do ssh to server, it uses login, interactive shell modes, which loads your environment variables
Solution
Use another way to set environment variables. I often use Figaro and it works with Capistrano.
To prevent sensitive information are included in Git, add config/application.yml to Capistrano's linked_files and fill correct sensitive information to <shared directory>/config/application.yml on server
When running my RoR app in Heroku shows this error:
We're sorry, but something went wrong.
If you are the application owner check the logs for more information.
Checking the heroku logs, this shows http://pastebin.com/yTWtRMVD
My main page is https://salty-stream-26420.herokuapp.com/ and my callback URL is https://salty-stream-26420.herokuapp.com/auth/twitter/callback in OmniAuth.
It runs in Cloud9 perfectly but not in Heroku. What is wrong?
Thank you, Alex.
This:
2016-05-03T13:47:23.544506+00:00 app[web.1]: PG::UndefinedTable: ERROR: relation "submissions" does not exist
suggests to me that you have to simply migrate your database on the remote server.
Try running heroku run rake db:migrate
You will have to create environment variables in the config/database.yml file for the username, password and database name
Those same environment variables need to be defined under
Heroku login >> application >> settings >> reveal config vars
By default you will get a DATABSE_URL something as
postgres://wwwbirawxqwmws:7bef28c02299c9c6054a646b5ce19276bbab52feca6692dac8305b32dbd7d36c#ec2-81-XX-XXX-X26.compute-1.amazonaws.com:5432/d6n59y5r09ko99
where wwwbirawxqwmws is USER
and 7bef28c02299c9c6054a646b5ce19276bbab52feca6692dac8305b32dbd7d36c is PASSWORD
and d6n59y5r09ko99 is the DATABASE
These three variables needs to be assigned in the heroku config, then you will be able to run
heroku run rake db:create
heroku run rake db:migrate
so i've successfully installed Harrys Prelauncher on Heroku (https://github.com/harrystech/prelaunchr)
and to export my collected emails into a csv i need to run this command (bundle exec rake prelaunchr:create_winner_csvs)
is there any way to run that command through pgadmin or some other program?
or is the only way for me to download my heroku database and run the command locally? also how and what would i need to do this?
i'm pretty new to rails and postgresql and would really appreciate if someone could help me out!
Because the rake task creates files locally you can't just run it on heroku via heroku run rake. You can however set up your local database.yml to connect to your heroku postgresql instance and run the rake task locally.
Run heroku pg:credentials to get the required database values.
Fill in the production environment of config/database.yml with the values you obtained from step 1 (for the value of 'database' in the yml file, use dbnmae from step 1)
Test your connection with RAILS_ENV=production rails db. This should drop you into a psql console.
Run the rake task. RAILS_ENV=production bundle exec rake prelaunchr:create_winner_csvs
The files will save locally in lib/assets as indicated by the documentation.
From within the directory of the project you can use
heroku run rake prelaunchr:create_winner_csvs
You should probably create a UI form in to your application.
On click on export CSV, it should run background job on heroku (Using delayed jobs).
heroku run rake prelaunchr:create_winner_csvs
Use send_data ruby method. To send your generated and dumped data file on to your browser.
Download the file on to your local system from running heroku instance.
Hope this will resolve your problem.
Cheers!!!
I ran into this problem recently while developing the Prelaunchr campaign for a client. Assuming you have a local version of your app and are using Postgres Copper in Heroku, you can "pull" your Heroku database down to your local machine, set that as your development database in database.yml, and run the rake task from your local app, which should now have the same database as your heroku version. Here is the command to pull the db (subbing out name_for_database & heroku_app_name with your own):
heroku pg:pull HEROKU_POSTGRESQL_COPPER_URL name_for_database --app heroku_app_name
Make sure to restart your local server to see the new database info populated.
I've set up delayed_job and it works fine under development.
However, whenever I attempt to run the script under production and run
RAILS_ENV=production script/delayed_job start
I get this:
/Users/simon/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1161:in `initialize': FATAL: database "myapp_production" does not exist (PG::Error)
It feels like I'm missing something really basic here (from what I gather it's looking for the production database on my local machine?), but I'm a bit stuck as to where to look and what might cause this problem.
Any pointers would be greatly appreciated.
Well this is pretty much obvious - you are starting your dj in production env, so it tries to connect to the db that is described in the "production" section of config/database.yml. It seems that you haven't created this db yet.
To create this DB run in terminal
RAILS_ENV=production bundle exec rake db:create
I'm running Ubuntu on EC2 and using Capistrano to deploy my app.
Capistrano won't let me run "rake db:schema:load" so I used rails to create a new migration called CreateTables. Then I copied in the create_tables from the schema.rb and ran "cap deploy:migrate".
But, I get this error: uninitialized constant CreateTables
Any ideas?
Thanks
For some reason, when I tried $ cap deploy:cold, I was able to get the tables created via a migration.