Embedded Ruby in database.yml not being parsed in RubyMine - ruby-on-rails

I am using RubyMine 7.0.4 to build a Rails(4.2.0) application with Postgres as my database. I am using environment variables to hold my local configuration (DB_USERNAME and DB_PASSWORD), but RubyMine can't parse the embedded ruby in the database.yml file.
database.yml excerpt:
...
development:
<<: *default
database: my_app_development
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
...
I even tried switching it to the actual credentials within the <%= %> tags, but same issue:
...
development:
<<: *default
database: my_app_development
username: <%= 'my_app' %>
password: <%= 'password' %>
...
This seems to be a RubyMine issue, since everything is executing perfectly from the command line/the application works fine. However, I like having my database integrated with the IDE, so it'd be great if I could get this working again.
Is there anything that I can do to execute the embedded ruby instead of treating it as a string?

This is a bug within RubyMine that is unfortunately still not yet fixed.
For further info please refer to https://youtrack.jetbrains.com/issue/RUBY-9223
The only fix I managed to come up with so far, is to add the database.yml to your .gitignore file (if you're pushing your project to a remote repo) and store the username and password as plain text.
The second approach could be just inputting your db_password and db_username as plain texts for development and test databases, and using ENV["DB_USERNAME"] and ENV["DB_PASSWORD"] for the production DB only.

Related

How to get global variables from database config file in rails application?

I want to get secret informations from other place beside rails application and the host. I made a global variable file under config/initializers to get the keys. Then set them in the database connection file. Like this:
config/initializers/get_secrets.rb
##username = 'A'
##password = 'B'
config/database.yml
development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: <%= ##username %>
password: <%= ##password %>
host: 127.0.0.1
port: 3306
When ran rails console, got:
./config/initializers/get_secrets.rb:1: warning: class variable access from toplevel
(erb):10: warning: class variable access from toplevel
>
It seems not a good usage in this case. Then is there a right way?
In Rails 5.2+, we have a master.key file and a credentials YAML file. The master.key file encrypts your credentials (so never show anyone this key except your server).
You can activate this by doing:
EDITOR=vim rails credentials:edit
The file:
aws:
key: 123
google_key: 456
secret_key_base: f230ffddd1d1f151j3bchjas9a292j221...
If you want to use a credential, it looks like this:
Rails.application.credentials.google_key
or for nested resources like the AWS one:
Rails.application.credentials.aws[:key]
But if you're on Rails 5.1 or less, you don't have this ability. So, you do it in a different way:
Environment variables. Your program environment can always export an ENV and retrieve it. I always do this for simple nuisances like test email passwords:
export GMAIL_PASSWORD="1234" and its usage, ENV['GMAIL_PASSWORD']
Secrets.yml. This is a file that basically predates master.key + credentials by functioning as an in between credential handler for your environments. It's janky and often erroneously committed to repos by accident, but we do what we can:
development:
secret_key_base: 1112sdasae1d13d1tfd1d3...
GMAIL_PASSWORD: '1234'
test:
secret_key_base: _your_secret_ as above
production:
secret_key_base: <%= secure_token %>
And it is accessed like Rails.application.secrets.GMAIL_PASSWORD
If you cannot access your variables, it may be because the secrets.yml file is not being loading in or you are in the wrong environment. Notice those vars are nested in particular environments.

How to configure database.yml for production

I would like to deploy my application via Dokku to a VPS.
Putting together the dokku-postgres documentation and the relative, scarce internet documentation on the matter (one at GitHub), it seems necessary to configure database.yml to use the url environment variable url: <%= ENV['DATABASE_URL'] %>
Since I could not find any other sources of information, I wonder how database.yml should be configured, and how Rails will connect to the postgres service created with Dokku.
For instance, taken for granted that linking url to the DATABASE_URL variable is necessary, will this be enough to establish a connection between my Rails application and the postgres service or would it be still necessary to use a username and a password? In the latter case, what username and password am I expected to use?
Below is how at present my database.yml looks like.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: asarluhi
development:
<<: *default
database: fireworks_app_development
test:
<<: *default
database: fireworks_app_test
production:
<<: *default
database: fireworks_app_production
pool: 25
username: fireworks_app
password: <%= ENV['FIREWORKS_APP_DATABASE_PASSWORD'] %>
This file was created as it is (apart from a higher pool size for production) when I created the application. How would you suggest to edit the production section?
The dokku-postgres documentation states that the following (and nothing else) will be set on the linked application by default:
DATABASE_URL=postgres://postgres:SOME_PASSWORD#dokku-postgres-lolipop:5432/lolipop
In place of the lollipop postgres service example, I would use fireworks_app_production to match the name of the database in database.yml
Are username and password still necessary after pointing url to the DATABASE_URL variable? Am I expected to add or remove anything else?
You don't need to worry about the database.yml with dokku, just upload your app to the server, let's use "fireworks" as the name for example on this.
when you upload the first time the app, this is created automatically so you don't need to create it.
then you install the postgres plugin and run
# that will create the container for the database
$ dokku postgres:create fireworks
# and then you link both, the app with the database
$ dokku postgres:link fireworks fireworks
you don't have to worry about anything else, with that dokku will connect this
then you just have to run db:migrate and everything is ready to work!

How to set rails environment variables in database.yml file?

On my local machine, I want to set environment variables for all of the sensitive information in the database.yml file. My file looks like this:
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: <%= ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
socket: <%= ENV['SOCKET'] %>
development:
<<: *default
database: <%= ENV['DEVELOPMENT_DATABASE'] %>
test:
<<: *default
database: #JetStreamIQ-2_0_test
production:
<<: *default
database: <%= ENV['PRODUCTION_DATABASE'] %>
username: <%= ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
I thought that I could just set these environment variables in my .bashrc file, but that doesn't seem to be working. My .bashrc file looks like this:
export DATABASE_USERNAME="root"
export DATABASE_PASSWORD="*****"
export SOCKET="/var/run/mysqld/mysqld.sock"
export DEVELOPMENT_DATABASE="shoppe_development"
export PRODUCTION_DATABASE="#"
When I run my server with
rails s
I get an error that says:
Access denied for user 'root'#'localhost' (using password: YES)
I understand that there is a problem with the database username and password because of the way I've configured my database.yml file, but I'm just not sure what it is.
Is there something big I'm missing here? Any help would be much appreciated.
Thanks!
I'd like to offer a couple of tips here.
You can run database.yml through erb to test what you're getting. For example, erb config/database.yml. This will help you determine what the problem may be.
You can find out every environment variable set in your shell with the set command. This will allow you to confirm that the environment variables you're expecting are being set (in .bashrc).
Hope this is helpful.
It turns out that after I edited my bashrc file, I needed to exit out of that terminal session and open another terminal for the changes to be finalized :/ After I did that my application started perfectly.
If you dont want to restart your terminal, you just need to execute your .bashrc file:
source /path/to/.bashrc
in the same terminal as your rails server.
Is it a typo or are you really setting JETSTREAMIQ_DATABASE_PASSWORD but then using DATABASE_PASSWORD in your database.yml? Because that would do it.
Can you connect to mysql using the actual values directly using the mysql command line app?
you may need to put your environment variables at the top of .bashrc file. cause it has some return executed into the file. so it return before actually executing your export command
something like this:
nano ~/.bashrc
export DATABASE_VAR="my_own_var"
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
Simply set environment variable
DATABASE_URL=postgres://user:pass#localhost/dbname
It will override database.yml settings
Offcourse it supports more parameters, like
DATABASE_URL=mysql2://user:passwd#host:port/dbname?encoding=utf8mb4&collation=utf8mb4_unicode_ci
You can also source your ~/.bashrc file using the following command on your terminal:
exec $SHELL
Have you tried to access your local database with that data from the console?
Maybe you have to grant access for localhost
GRANT ALL ON shoppe_development.* TO 'root'#'localhost';
Besides that, I recommend you the gem dotenv to handle your env vars for development environment

fe_sendauth: no password supplied encountered with Amazon RDS

I know many others have encountered this problem, but I'm limited in the Postgres configuration I can do since I am using Amazon RDS...
Here is the relevant section of my database.yml
default: &default
host: <%= ENV["POSTGRES_HOST"] %>
username: <%= ENV["POSTGRES_USER"] %>
password: <%= ENV["POSTGRES_PASSWORD"] %>
adapter: postgis
encoding: unicode
database: <%= ENV["POSTGRES_DB"] %>
port: 5432
pool: 5
When I hard code the host, leave everything else as is, and load localhost:3000, I get fe_sendauth: no password supplied error. However, when I hard code all of the values instead of using an ENV variable, everything loads as normal with no errors.
I'm using Amazon RDS, so I have no ability to edit pg_hba.conf, but is this just a simpler problem of RoR not having access to ENV variables?
Yup, it's pretty clear that your ENV values are returning nil. Keep in mind that environment variables are loaded a bit differently on your production server than localhost. In most deployment configurations you can't simply set a Unix environment variable and have it be detectable in ENV.
I can't really tell much about how your ENV variables are being set from your provided code, but you should consider using Figaro (https://github.com/laserlemon/figaro) or dotenv (https://github.com/bkeepers/dotenv) to manage your ENV variables for you. However, it will require the extra step of having to manage an environment variable file outside of source control.

How do I handle database.yml development mode configuration with multiple develpers?

what's the standard way of dealing with development mode database config for database.yml when there are multiple developers on a project?
Should all devs have the same database setup? Is that a smart requirement?
Or should there be some type of gitignore and symlinking taking place? I did this and after branching, the database.yml disappeared :(
I've also come up with an erb solution that seems to work well enough, but not sure if there will be unintended consequences. The following will allow devs to sed environment variables in their bash_profile in case they have a different local setup than the default. This will allow our database.yml file to stay in git.
development:
adapter: postgresql
database: <%= ENV['DEV_DB_DATABASE'] || 'app_development' %>
username: <%= ENV['DEV_DB_USERNAME'] || 'postgres' %>
password: <%= ENV['DEV_DB_PASSWORD'] || '' %>
host: localhost
encoding: UTF8
Add database.yml.sample file to the rails app, also add database.yml to the .gitignore. This way all developers can have different database setups.

Resources