ActiveRecord task executing in wrong environment with Whenever - ruby-on-rails

I have to run a Cron task involving some data cleaning in my ActiveRecord database. I am using Whenever gem. Here is the code :
schedule.rb
every 1.hour do
rake 'notifications:clear'
end
notifications.rake
namespace :notifications do
task clear: :environment do
Rpush::Notification.delete_all
end
end
Running this gives me the following error:
rake aborted!
ActiveRecord::NoDatabaseError: FATAL: role "user_prod" does not exist
I am in development environment. Here is my database.yml file :
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: development_database
test:
<<: *default
database: test_database
staging:
<<: *default
database: staging_database
username: user_staging
password: <%= ENV['DATABASE_PASSWORD'] %>
production:
<<: *default
database: production_database
username: user_prod
password: <%= ENV['DATABASE_PASSWORD'] %>
Any ideas on why my ActiveRecord instruction seems to connect to my production environment ? Thanks in advance !

Update Your code with below :-
In schedule.rb file :-
every 1.hour do
rake 'notifications:clear', :environment => "development"
end
And finally executed this command :-
whenever --update-crontab
OR
Clear existing cron jobs.
crontab -r
Update cronjob with the environment.
whenever --update-crontab --set environment='development'

Related

Unable to create Database in Rails using rake db:create

I created a new rails project and did few configuration changes in database.yml.
Then had to create the database using the command rake db:create inorder to continue developing the application but I get the error below.
Els-MacBook-Pro:eshop el$ rake db:create
warning ../../package.json: No license field
FATAL: role "eshop" does not exist
Couldn't create 'eshop_development' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: FATAL: role "eshop" does not exist
Tasks: TOP => db:create
(See full trace by running task with --trace)
Els-MacBook-Pro:eshop el$
I will also post the contents of my database.yml below:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
host: localhost
database: eshop_development
username: eshop
password: eshop
test:
<<: *default
host: localhost
database: eshop_test
username: eshop
password: eshop
production:
<<: *default
database: eshop_production
username: eshop
password: <%= ENV['ESHOP_DATABASE_PASSWORD'] %>
I will be grateful if someone can help me figure out why I cannot create a db. I have read all types of solutions and none seems to be working. Thanks.
In your database.yaml you need to add a port: 5432, and then Try rails db:create, as rake has been deprecated.
create a role in your postrgesql database named 'eshop'

Rails_admin dont work in production

I got some problems with my rails_admin gem in the production when I trying make first user admin in Rails console.
In the development all works fine. Look at the error and code.
Error terminal:
2.3.0 :001 > u = User.first
ActiveRecord::NoDatabaseError: FATAL: database "myApp_development" does not exist
database.production.yml
default: &default
adapter: postgresql
encoding: UTF-8
pool: 5
development:
<<: *default
database: myApp_development
username: deployer
password: password
test:
<<: *default
database: myApp_test
username: deployer
password: password
production:
<<: *default
database: myApp_production
username: deployer
password: password
To run rails console in the production environment you should use bundle exec rails console production or bundle exec rails console RAILS_ENV=production, the command bundle exec rails console run console in the development env by default.
Create the database table and do
rake db:migrate RAILS_ENV=production
rails console RAILS_ENV=production

rake aborted:PG::ConnectionBad: fe_sendauth: no password supplied

I'm on Cloud9 Ubuntu Template and I installed postgres. I'm getting an error when I try to do a "rake db:migrate".
rake aborted:PG::ConnectionBad: fe_sendauth: no password supplied
Related settings in my database.yml file
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: <%= ENV['USERNAME'] %>
password: <%= ENV['PASSWORD'] %>
host: <%= ENV['IP'] %>
development:
<<: *default
database: app_development
It seems like Cloud9 issue. I refer https://community.c9.io/t/fe-sendauth-no-password-supplied-error-after-setting-up-postgrsql-on-rails/2206/2
Run below commands on your c9 terminal.
$ source ~/.profile
$ rake db:create
$ rake db:migrate
It works for me.

Rake db:create fails to connect to server: No such file or directory

I'm trying to upload a rails app to heroku and I'm running into problems to change my app from sqlite3 to postgres, mainly when I run rake db:create I get:
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
This is what my database.yml looks like:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: anagram_development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: anagram_test
production:
<<: *default
database: anagram_production
Do you have postgresql installed locally? If so, double check that it's running. Your database.yml file is also missing some required information to connect to a postgres database. You need to provide username, password and hostname.
Here's an example of mine which connects to a local postgres db:
development:
adapter: postgresql
encoding: unicode
database: app_development
pool: 10
username: <%= %x(whoami) %>
password:
For heroku/production, you can use the following:
production:
url: <%= ENV['DATABASE_URL'] %>
pool: <%= ENV['DB_POOL'] || 10 %>

Rails database setup on Travis-CI

I'm trying to use Travis Continuous Integration on a Rails project. The documentation says that the test db must be configured as following for SQLite3:
test:
adapter: sqlite3
database: ":memory:"
timeout: 500
But I'd like to keep my default configuration for local tests. Is it possible to keep both my local settings and the Travis requirements?
My solution for this problem is fully based on a blog post with a few differences:
Travis CI specific settings in config/database.travis.yml;
cp config/database.travis.yml config/database.yml in before script section of .travis.yml;
I don't have config/database.yml in source tree.
Here is full listing for both files:
# .travis.yml
language: ruby
rvm:
- 1.9.3
env:
- DB=sqlite
- DB=mysql
- DB=postgresql
script:
- RAILS_ENV=test bundle exec rake db:migrate --trace
- bundle exec rake db:test:prepare
- bundle exec rake
before_script:
- cp config/database.travis.yml config/database.yml
- mysql -e 'create database strano_test'
- psql -c 'create database strano_test' -U postgres
# config/database.travis.yml
sqlite: &sqlite
adapter: sqlite3
database: db/<%= Rails.env %>.sqlite3
mysql: &mysql
adapter: mysql2
username: root
password:
database: strano_<%= Rails.env %>
postgresql: &postgresql
adapter: postgresql
username: postgres
password:
database: strano_<%= Rails.env %>
min_messages: ERROR
defaults: &defaults
pool: 5
timeout: 5000
host: localhost
<<: *<%= ENV['DB'] || "postgresql" %>
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
#mrm's blog post doesn't say anything about answering your question.
I faced the same problem where my postgreql credentials are different on my local machine than travis default. This is the simplest solution I came up with:
# config/database.yml
test:
adapter: postgresql
database: medscraper_test
username: <%= ENV['TRAVIS'] ? 'postgres' : 'MY_TEST_USERNAME' %>
password: <%= ENV['TRAVIS'] ? '' : 'MY_TEST_PASSWORD' %>
Note that Travis CI automatically sets TRAVIS environment variable.
Your solution would be:
# config/database.yml
test:
adapter: sqlite3
database: <%= ENV['TRAVIS'] ? '":memory:"' : 'db/test.sqlite3' %>
timeout: 500
I just wrote a blog post describing how to do this.

Resources