when running 'rake db:setup' getting deprecation warning - ruby-on-rails

hope someone can help me here:
I have
* changed the database in my application from sqlit3 to postgresql,
* replaced the gemfile to gem 'pg',
* update database.yml
all this, in order to be able to deploy to heroku.
when I run
rake db:setup
I am getting these errors:
DEPRECATION WARNING: Single arity template handlers are deprecated. Template handlers must
now accept two parameters, the view object and the source for the view object.
Change:
>> Coffee::Rails::TemplateHandler.call(template)
To:
>> Coffee::Rails::TemplateHandler.call(template, source)
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
Couldn't create 'school-grades-project' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I read that jbuilder should take care on the issue, so I upgraded jbuilder, but still the problem remains.
would appreciate some help here.
thanks,

It's a bit hard to diagnose this because you didn't specify the version of Rails that you're using or the contents of your database.yml file. But assuming it's a recent Rails version with support for database URLs it should work by following the descriptions in the Getting started guide for Rails.
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="postgres://myuser:mypass#localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
Did you uncomment the production settings?

Here I have a quick solution.
This issue came into existence after updating the application to rails 6.x, To resolve it update the following gems:
Updating coffee-rails to 5.0.0 fixed this for me
Then run the bundle update and start the application server solved the warning issue.

Related

How to run migrations on a different database server through Heroku?

We need to add the credentials of a database in database.yml file under a different environment like remote_database:
remote_database:
adapter:
encoding:
username:
...
And after adding all this, running the following command from the local terminal does the job:
RAILS_ENV=remote_database rails db:migrate
I'm trying to accomplish the same thing on Heroku. I have pushed the changes in config/database.yml, and I'm trying to execute the following command:
RAILS_ENV=remote_database heroku run rake db:migrate
# or
heroku run rake db:migrate RAILS_ENV=remote_database
Seems like Heroku is completely ignoring RAILS_ENV or the settings for remote_database env in config/database.yml file. Heroku always makes the changes in the regular database server connected with it that can be found at DATABASE_URL.
Is there a way to run the migrations on a different database server through Heroku?
Heroku injects database.yml and overrides it completely with Rails under 4.1 version or overrides partially and allows a way for us to prevent overriding from Rails 4.1. Check the complete explanation about Rails database connection behaviour on Heroku article
So, in your case
If you are using Rails 4.1+: you may try to add url key to your database.yml as described in Active Record 4.1+ Escape Valve section of above link.
If you are using Rails under 4.1 version: override database connection by an initializer. See Heroku article ("Otherwise if you are using an older version of Rails you will need to use an initializer" section)

warning: circular argument reference - now rake aborted! on rake db:migrate

On rake db:migrate, I am getting following error.
ruby-2.2.1/gems/activesupport-4.0.2/lib/active_support/values/time_zone.rb:282: warning: circular argument reference - now
rake aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
My rails gem gem 'rails', '4.0.2'
What's the solution for this?
From your error, it sounds like you are trying to use a Postgres database with your rails project. Basically, postgres runs separately from your rails project. You need to make sure you have:
a) installed postgres using something like homebrew
b) found a convenient way to start your postgres server when you need to (I recommend the one on postgresapp.com)
c) actually started the server before running the command that's producing this error (e.g., through opening the postgresapp or in a separate terminal window)
Conceptually, think of it this way: you can't migrate (e.g. 'change') a database that's inaccessible because the postgres server is down. That's what the error means.
As an alternative, you can use SQLite instead of Postgres. SQLite does NOT run as a separate process (https://www.sqlite.org/serverless.html), so you will run into these problems less. However, in your ruby code, you will need to pay attention to exactly how to configure your app for a postgres database or a sqlite database, whichever you choose. Here is a great SQLite tutorial for initial set-up:
http://www.integralist.co.uk/posts/active-record.html

Error when creating database record on rails console

I'm trying to create a new record through the rails console but I'm getting the following error:
PG::ConnectionBad: FATAL: database "my_database_development" does not exist
I've recently changed from Sqlite3 to PG to be able to deploy to Heroku. Is this what is giving the error?
Thanks a lot!
It looks like you have not yet run
rake db:create
This will "create" the databases on your PostgreSQL server. A step that you didn't have to do with SQLite, but Postgres requires it. As TK-421 said, make sure that your database.yml is configured for your OS and Postgres, not SQLite.
Here is a (possibly outdated) Railscast on the topic.
http://railscasts.com/episodes/342-migrating-to-postgresql
Is this running locally, or on Heroku? In config/database.yml, the local user you specify will need to have CREATEDB privileges (as a superuser, for example).
There's a little more configuration required in this case than for SQLite. Google will show you a bunch of tutorials specific to your OS.
If you see this problem in production on heroku, start the console like this:
rails console production
If you have that problem in development on your machine, check your config/database.yml. It is points to a local development database that does not exist.

Can't run Ruby on Rails tests on Heroku's hosted Postgres databases

I'm currently evaluating Nitrous.io and liking what I'm seeing so far. I've currently got a few databases for testing and development running on Heroku's hosted Postgresql service as well. I'm running into some issues when running tests though, and I'm hoping somewhere here may have a solution.
When I run rake db:test:prepare I'm getting the error:
FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
From what I've read elsewhere, that's trying to DROP the database, but Heroku's hosted databases don't allow that. Does anyone out there know how to run Rails tests on Heroku's Postgresql?
Nitrous.IO has released a package manager which will allow you to install Postgres within your Nitrous box. This can be used for your test database as well as your development database if needed:
https://github.com/action-io/autoparts
This also requires that you are using a Nitrous box is running version "bran" or later (see README). You may need to terminate/create a new box if you are running on version "arya".
To use autoparts, run parts search to see all of the packages available.
To install postgresql within the Nitrous box, run the following command:
parts install postgresql
Make sure that your config/database.yml file explicitly sets host: localhost for each database you wish to connect to, or else the pg adapter will fail to find the socket it needs to connect to.
According to the Heroku docs, you need to use "heroku pg:reset" in your rake task.
https://devcenter.heroku.com/articles/rake

database configuration does not specify adapter

I'm getting this error when I'm trying to connect to a mysql database. The problem is that the application works for weeks, and then randomly I get this message. When I get this error message the application is not able to reconnect to the database until I restart it.
I'm using a configuration file to connect to the database, and the adapter is specified...the database configuration is not generated at runtime.
Do you have any idea on what is going on?
when I tried to run a command line script (let's say 'my_script' here), the same error happened. The reasons were:
There is only production environment there.
I missed to set RAILS_ENV for the command line.
So, the following is the solution in my case:
$ RAILS_ENV=production my_script
I just had this problem, and it was caused by a typo in my configration.yml.
I originally had this:
production:
adapter:mysql
When I meant to have this:
production:
adapter: mysql
That one little space between adapter: and mysql makes the difference.
Another possible cause:
In Rails 3.2.x, establish_connection has a default argument set from the environment:
From connection_specification.rb:
def self.establish_connection(spec = ENV["DATABASE_URL"])
resolver = ConnectionSpecification::Resolver.new spec, configurations
spec = resolver.spec
The way ConnectionSpecification::Resolver works depends on ENV['DATABASE_URL'] giving a nil if not set. (Normally, it would be something like 'postgres://...').
So, if you happen to have misconfigured DATABASE_URL such that ENV['DATABASE_URL'] == '', that will give you database configuration does not specify adapter.
I had this error when I mistakenly started rails server with
sudo rails s -e "Production" -p 80
and I should have started rails with
sudo rails s -e "production" -p 80
I found another thing that can cause this problem: "mixing in" another YAML node using & and *.
I was originally doing something like the following to facilitate local, per-develop, Git-ignored config files:
http://blog.lathi.net/articles/2006/03/02/config-database-yml-goodness-for-teams
But, after some debugging, I came to find out that establish_connection was for some reason being called with only the mixed-in key-value pairs and not the main ones. I.e. adapter, host, and database were not being passed in. I have no idea why, and this used to work for me.
Anyhow, instead of mixing in another YAML node, I now put the entire development and test hashes in the local config file, and establish_connection is once again being called correctly.
For me, this command resolved the issue.
rake db:migrate RAILS_ENV=production
I've found a couple of clues that this might be related to older library (ActiveRecord) or gem versions. For example, problems with fixtures even though rest of app seems okay (after an upgrade) or this trac ticket, which "stops gems from requiring an adapter from an old Active Record gem". Both of these are old, though, but it might be worth making sure your gems are up to date (if possible).
Are you using the native rails MySQL adapter by any chance? This is now deprecated under rails, but it's conceivable it's still limping along.
I've taken a very quick look at connection_specification.rb, too, which is where this error is coming from, and my best guess is that a reconnect is failing... but why (since it was obviously okay when you first started the app)? Are you doing something wild like calling ActiveRecord::Base.establish_connection in your application controller (or elsewhere)?
Or perhaps something like: script runner is called from cron in the dead of night when the connection has dropped. Unfortunately, the runner is invoked with an incorrect RAILS_ENV. Thus the wrong stanza is read from database.yml, and that stanza contains an invalid adapter:?
I got the same error, by typing in the following command:
db:migrate RAILS_ENV=product
Should've been:
db:migrate RAILS_ENV=production
If you get this error while deploying with Capistrano. Make sure you are setting the correct RAILS_ENV via
set :rails_env, 'production'
For example I was not explicitly setting the Rails environment in for the Capistrano staging deployment configuration. And thus Capistrano used 'staging' as the RAILS_ENV, resulting in the above error. Setting it to production like above in the staging.rb file solved the issue.
Do remember that RAILS_ENV=staging will look for a staging specification in your database.yml just as setting RAILS_ENV=production will look for a production specification in database.yml file.
Provide database configs, as shown below, for every rails environment you target using, for example
bundle exec cap staging deploy
production:
adapter: mysql2
encoding: utf8
database: rails
username: rails
password: pass
host: 127.0.0.1
port: 3306
pool: 5
timeout: 5000
staging:
adapter: mysql2
encoding: utf8
database: rails
username: rails
password: pass
host: 127.0.0.1
port: 3306
pool: 5
timeout: 5000
Remember to use the C-Based ruby gem for mysql. The ruby-based is unstable for production.
Try installing the gem
gem install mysql
Remember to copy libmySQL.dll into the ruby bin directory.
I had this error with another problem; I had specified 'development' twice, and 'test' not at all.
There are alot of bad tutorials out there on the internet that show yaml files like so:
development:
encoding: utf
database: dbname
...etc
YAML files are case sensitive and require TWO SPACES for the inner contents of each given db-type attribute. Like so:
development:
encoding: utf
database: dbname
...etc
UPDATE: I got this error again today. My VPS server had installed Rails 3.2.8 when my app was running Rails 3.2.6.
Definitely check your Gemfile and your database.yml file (of course). The problem here is clearly stated---Rails is not communicating with your database specifically due to an adapter (aka gem)
We had this issue with one of our older apps. Someone had created a boutique named environment, that even if RAIL_ENV was set to production, it was looking for a database configuration called legacy_<RAIL_ENV>, so I had to make a database environment called legacy_production for this app to work.
If you are maintain someone else's app, I would look for a copy of this app's database.yml that is working, perhaps it has some oddly named configuration. You can search your codebase for establish_connection to see if it is defining some strange variant.
I met this problem due to the 'multiple database support issue'. In my app/model folder, there is a file defined a redundant database connection:
class CacheCleanerActiveRecord < ActiveRecord::Base
establish_connection "cache_cleaner_#{Rails.env}"
self.abstract_class = true
end
but this database is not found in my database.yml ( because it's not used at all ).
so the solution is quit simple: remove this file and everything is fine !
You may have an error like:
RAILS_ENV= test
A space after the equals sign is not allowed, change it to:
RAILS_ENV=test
This happens to me, finally I found that RAILS_ENV is case sensitive, in my enviroment I set
RAILS_ENV=DEVELOPMENT , which is wrong, the value of RAILS_ENV must be lowercase.
$ RAILS_ENV=DEVELOPMENT rails server webrick
=> Booting WEBrick
=> Rails 4.2.5 application starting in DEVELOPMENT on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
config.eager_load is set to nil. Please update your config/environments/*.rb files accordingly:
* development - set it to false
* test - set it to false (unless you use a tool that preloads your test environment)
* production - set it to true
Exiting
/home/fangxing/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_c
onnection': 'DEVELOPMENT' database is not configured. Available: ["default", "development", "test", "production"] (ActiveRecord::AdapterNotSpecified)
from /home/fangxing/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/connection_specification.rb:211:in `res
olve_connection'
$ RAILS_ENV=development rails server webrick
RubyDep: WARNING: Your Ruby is outdated/buggy. (To disable warnings, set RUBY_DEP_GEM_SILENCE_WARNINGS=1)
RubyDep: WARNING: Your Ruby is: 2.3.0 (buggy). Recommendation: install 2.3.1.
=> Booting WEBrick
=> Rails 4.2.5 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-07-20 16:41:09] INFO WEBrick 1.3.1
[2016-07-20 16:41:09] INFO ruby 2.3.0 (2015-12-25) [x86_64-linux]
[2016-07-20 16:41:09] INFO WEBrick::HTTPServer#start: pid=19881 port=3000
rails -e "production" is okay
only rails -e production returns error
database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
call rake assets:precompile:all
This is probably not the most likely issue to cause this error, but here it is just in case.
My problem was that I was building the database settings in a Hash using symbols as keys and then serializing it with #to_yaml to database.yaml. ActiveRecord expects the environment names to be Strings, not Symbols, so it wasn't picking up the database settings when reading the generated file. I fixed it by using string keys in the hash.
For Rails4, commenting the line fetch(:default_env).merge!(rails_env: 'production') in production.rb and adding set :rails_env, :production fixed it.
You need to specify the environment when running the server or command as your database.yml file may have only production adapter while simply runnig rake db:migrate for example will take environment variable as development.
Just for the sake of completeness, I just got this error because I natively created a parametrised Rails runner script that takes an email address, and named the command line option -e -- which of course is the one the Rails runner uses for the environment. So it was trying to find an environment configuration that matched the email address!
Luckily, just before the ActiveRecord error mentioned in the title, it gave me an error message that helped me twig what the problem actually was:
You did not specify how you would like Rails to report deprecation notices for your test#example.com environment, please set config.active_support.deprecation to :log, :notify or :stderr at config/environments/test#example.com.rb
Check the spelling of adapter I had adaptor and got this error.
I had this error message when upgrading from Rails 4 to 5. I was calling
establish_connection "myconnection"
where "myconnection" is a valid key in my database.yml. However, passing this parameter as a string is apparently no longer supported. Using a symbol instead got rid of the problem.
I got this from copying and pasting sudo rails server webrick –e production -d from documentation into the CLI.
I'll shut the door on my way out 😔

Resources