Error when loading YAML config files in Rails [duplicate] - ruby-on-rails

This question already has answers here:
database.yml &references not working
(2 answers)
Closed 3 years ago.
I am configuring Rails with MongoDB, and find a strange problem when paring config/mongo.yml file.
config/mongo.yml is generated by executing script/rails generate mongo_mapper:config, and it looks like following:
defaults: &defaults
host: 127.0.0.1
port: 27017
development:
<<: *defaults
database: tc_web_development
test:
<<: *defaults
database: tc_web_test
From the config file we can see the objects development and test should both have a database field. But when it is parsed and loaded in config/initializers/mongo.db,
config = YAML::load(File.read(Rails.root.join('config/mongo.yml')))
puts config.inspect
MongoMapper.setup(config, Rails.env)
the strange thing comes: the output of puts config.inspect is
{"defaults"=>{"host"=>"127.0.0.1", "port"=>27017}, "development"=>{"host"=>"127.0.0.1", "port"=>27017}, "test"=>{"host"=>"127.0.0.1", "port"=>27017}}
which does not contain database attribute. But when I execute the same statements in a plain ruby console, instead of using rails console, mongo.yml is parsed in a right way.
{"defaults"=>{"host"=>"127.0.0.1", "port"=>27017}, "development"=>{"host"=>"127.0.0.1", "port"=>27017, "database"=>"tc_web_development"}, "test"=>{"host"=>"127.0.0.1", "port"=>27017, "database"=>"tc_web_test"}}
I am wondering what may be the cause of this problem. Any ideas? Thanks.

Depending on your system, Ruby may have been compiled with Psych support, which replaces the older Syck parser. The issue you're seeing (which only involves using a "dry" yaml file with the defaults) has already been fixed in Psych, but is not yet in a released Ruby version.
For now, you can either force the YAML parser to use Syck instead of Psych by putting this at the end of your boot.rb (but beware -- a future version of Ruby will no longer include Syck):
YAML::ENGINE.yamler = 'syck'
Or you could just use a non-DRY YAML file (without the defaults) for the time being.

Related

How can I keep AWS-SDK gem from trying to run .load_yaml_config?

I am working on an old-ish codebase that supports connections to many different databases. I am trying to create a new environment so I can open a rails console with a connection to the particular database that I need to use for the part of the application I'm working on. The name of the environment is nhl_development.
So far, I've created a new config file and added relevant database config:
# database.yml
generic: &generic
adapter: postgresql
encoding: utf8
pool: 30
dev_default: &dev_default
<<: *generic
host: <%= ENV['FOO']%>
username: <%= ENV['BAR']%>
password: <%= ENV['BAZ']%>
pool: 30
nhl_development:
<<: *dev_default
database: nhl
# config/environments/nhl_development.rb
Stats::Application.configure do
# copied entirely from config/environments/development.rb
end
When I run rails c nhl_development from my command line, I get the following error from the AWS-SDK gem:
/usr/local/rvm/gems/ruby-2.2.1/gems/aws-sdk-v1-1.60.2/lib/aws/rails.rb:110:in `load_yaml_config': config/aws.yml is missing a section for `nhl_development` (RuntimeError)
...
...
from /home/me/app/config/environment.rb:5:in `<top (required)>'
I understand the basic point of this error message. When I open config/aws.yml there is no set of config for nhl_development. However, what I don't understand is:
Where is AWS::Rails.load_yaml_config being called? I don't think I even want/need AWS to load configuration in this environment. The stack trace leads back to config/environment.rb:5 but that's just the line that initializes the Rails app. There is no initializer for AWS in config/initializers and I've done a find-in-project for .load_yaml_config with no results. I can get everything working by adding a section of config for this environment in config/aws.yml but I don't to load any AWS config at this time. How can I keep AWS::Rails.load_yaml_config from being called in this environment so that I don't have to mess with config/aws.yml right now??
It's the gem itself that does this. When it is required (which bundles will do if it is in your Gemfile) it loads this file which registers an initializer that (among other things) calls load_yaml_config.
You could conceivably solve this by tuning off autorequire for that one gem (require: false) in your Gemfile and ensuring that the gem isn't loaded by any other means until after initialisers have run but that sounds a bit hacky. I think the easiest way is just to add an entry to that yaml file

RunTimeError: YAML syntax error (Unknown)

I have started using and learning Ruby on Rails, and I wanted to use Postgres instead of sqlite3, after a bunch of stuff I went through to install the gem (I'm using Ruby 2.1.2 and Rails 4.1 in Ubuntu 14.04) when I run Rails after I create my welcome index page I got this error:
YAML syntax error occurred while parsing
/home/charlie/Documents/Projects/#potsuri/config/database.yml. Please
note that YAML must be consistently indented using spaces. Tabs are
not allowed. Error: (): found character that cannot start any
token while scanning for the next token at line 26 column 13
This is what I have in Line 26 column 13
database: #potsuri_development
This is from line 24 to line 26
development:
<<: *default
database: #potsuri_development
I'm new using Postgres so I'm not sure how to fix this "syntax error".
I don't think this has anything to do with Postgres, but, rather YAML/Ruby.
I assume by the look of it that you've got a Ruby class instance variable you're trying to interpolate into the YAML file? I think the issue is that pure YAML doesn't know anything about Ruby, and so that syntax (the # in particular) isn't expected.
The # character is reserved in YAML, so if you escaped it, i.e. \#, it would probably read it in as a literal # character, but would likely not interpolate it.
If your YAML file is being loaded in an erb (Embedded Ruby) context, and the variable you're trying to interpolate exists, then using Embedded Ruby may work to get your value in there, i.e.
database: <% #potsuri_development %>
Note: I did not downvote your question.

Failing to access environment variables within `database.yml` file

I have the following developement section of my development.yml file:
development:
adapter: postgresql
host: localhost
database: testtb
username: app_user
password: ENV['APP_USER_POSTGRES_PASSWORD'] <= Troublesome line
When I open a rails console via bundle exec rails console and type ENV['APP_USER_POSTGRES_PASSWORD'] I get back the DB password I've specified in my local profile. However, when I start my rails server, it can't connect to the DB, failing with
PGError FATAL: password authentication failed for user "app_user"
This was previously working when I had the DB password actually typed out in plain text, rather than trying to access it via ENV['...'], but for obvious reasons I want to keep the actual password out of this file entirely (and therefore out of the code repository) while still being able to commit other, non-secure changes to the database.yml file.
Is there something special about the syntax I'm missing, or are the environment variables for some reason not available when the database.yml file is being loaded?
Update: Some people report in the comments that this doesn't work as of Rails 4.2.x.x. I haven't tried it myself, so YMMV.
Ah, finally figured out the simple solution - it accepts embedded Ruby:
password: <%= ENV['APP_USER_POSTGRES_PASSWORD'] %>
Short and quick solution if you are running a Rails version > 4.2 Run the following command:
spring stop
..then run rails console or other rails command. My issue was that Spring server needed to be restarted in order to refresh/pickup my new ENV vars. I was starting up Rails console and it couldn't see them until I shut down Spring.
Previous versions of Rails didn't have this issue since they didn't use Spring server.
Another tool to help you troubleshoot -- Use the following command to print out your database.yml config. You can run it from the command line, but I prefer to run this within Rails console since then you can use awesome_print to make it pretty:
Within rails console:
puts ActiveRecord::Base.configurations
...or using awesome_print
ap ActiveRecord::Base.configurations
Or instead from the command line:
bin/rails runner 'puts ActiveRecord::Base.configurations'

Database not being selected in rails project when attempting to rake db:migrate

Working with a rails app, having some manner of weird database / rake issues.
When I execute:
rake db:migrate
I am getting the following error:
Mysql2::Error: No database selected: SHOW TABLES
(See full trace by running task with --trace)
The trace isn't revealing much useful information. Can be seen here: http://pastebin.com/WdsguudC
The config file looks right, and the user is getting logged in, or I would have gotten some kind of access error. The database exists, the user has correct permission, and I can access and manipulate it manually. I have done a bunch of googling, and haven't found anything helpful. Not sure if there is any other code that needs provided, because this seems like fairly low level problem.
after all that it was a spacing issue in the yaml.
Note that ruby has exchanged its YAML parser in a recent 1.9.2 version.
This might also cause this problem.
In order to switch back to the old YAML parser syck, use this in boot.rb:
require 'yaml'
YAML::ENGINE.yamler= 'syck'
Well, it is a common issue for us beginners. This issue comes from the moment when you create your new project in rails. Let’s say to have an example
$ rails new toy –d mysql
After you do the bundle and start your server, most likely you will have an error. To correct it you need to go to your database.yml and modify the following:
Add a password in the password field as shown below, this is the password you use to secure mysql.
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: mypassword
socket: /tmp/mysql.sock
Also, comment out the database adding a hash tag (#)before the name as shown below
development:
: *default
database: #toy_development
Then restart your command line and go to the root of your application and type:
$ rails s
You have to see the Ruby on Rails welcome page..
After, you need to create a database.
Create a DATABASE.
The issue message is saying that not DATABASE is selected. It is because I didn’t create one. When you work with MySQL you have to create one, so:
Go to the root of my application and type:
$ mysql –u root –p
$ Passwor: mypassword (Enter your password, this is the one you entered to secure MySQL)
Note: This example works wit a project called toy and the user I wanted to grant privileges is mark and the password I’ll give is 45mark. Below you will see where I apply these elements. Remember to apply your own elements on each part of the statement.
Create and user for this project
Once you are in, you will see the pointer (mysql> ), so type after it:
mysql> GRANT ALL PRIVILEGES ON toy_development.* TO 'mark'#'localhost' IDENTIFIED BY '45mark';
Then type:
mysql> exit;
Check that it is working by typing:
$ mysql –u mark –p toy_development
Enter password: 45mark (You enter the one you gave)
Open database.yml file and configure what is needed and fix as required. In my case I will chance the username to mark and the password to 45mark
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: mark
password: 45mark
socket: /tmp/mysql.sock
- Also, REMOVE the hash tag (#) added before
development:
: *default
database: toy_development
Save it.
Go to the root of the application and type
$ rake db:schema:dump
Done!!
I hope this helps. Happy coding!!
Thanks
I had the same issue with ruby 1.9.2-p180 , upgraded to p290 and it works
Just restart the server; in the command line:
Press Ctrl + C
execute:
rails s
I had a similar error when i typed rake db:schema:dump and it turns out that I just have to comment out all the databases on my yaml file except my development one.
Give a try to this.
rake db:test:prepare
Install this to see if you have actually created a table or not. Open the "development.sqlite3" in db folder
http://sqlitebrowser.org/
Its a simple error checkout the entire database.yml file and see that where is default decription is given database name is given or not if not then look below it there will another development name is also given where configuration of database is use check that give your database name in it
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: 12345
host: localhost
development:
<<: *default
database: db_name
One potential cause is that there is a DATABASE_URL environment variable defined.
$ echo $DATABASE_URL
=> mysql2://root#localhost:3306
If you get a similar output to the above url (i.e., the database name is not present), then you might want to add the database name to the string or unset the env var.
$ export DATABASE_URL=mysql2://root#localhost:3306/my_rails_app_development
$ unset DATABASE_URL
If you unset the var, you probably want to specify the database details in database.yml instead.

is there a way to edit herokus yml file

I setup a remote connection locally and need to push it to heroku. When I pushed it to heroku I got an error saying:
RemoteDBName is not configured.
I'm just assuming (also searched and saw) heroku uses their own config.yml file.
Figured this out, for anybody connecting to a remote database on heroku that might see this:
Heroku replaces your database.yml file with their own, overwriting anything in yours.
To get around this:
Create a new file in your config folder, name it whatever.yml
Setup the connection string in this file.
Create a new file in your initializers folder, I called mine load_remote.rb. In this file write this line of code:
REMOTE_DB = YAML.load_file("#{RAILS_ROOT}/config/YOURNEWFILEHERE.yml")
Establish your connection in any of the remote models with this line of code:
establish_connection Remote_DB['Whatever you named your connection string in the yml file here']
Let me show you how database configuration is done when you work with Heroku. I think this might be a bit vague in the documentation, some people get confused over it. Lets utilize the console:
zero:~/Projects/crantastic $ heroku console
Ruby console for crantastic.heroku.com
>> puts File.read(Rails.configuration.database_configuration_file)
---
production:
encoding: unicode
adapter: postgresql
username: something_random
port: 5432
host: somewhere.at.heroku
database: something_random
password: something_random
=> nil
>>
Heroku in practice replaces your apps database.yml when you push your site to their servers. Your data will be stored in one of their fancy PostgreSQL servers no matter what you use locally - this means that you don't have to think about database.yml at all (except for development purpses, naturally). Taps makes sure that everything's db agnostic. If you want to push your latest development db to Heroku, simply run heroku db:push

Resources