ActiveRecord connection requires reset? - ruby-on-rails

An application in production mode is returning content as expected. It is assumed it is connecting to the configured database. The database.yml file is as follows
default: &default
adapter: postgresql
user: deploy
password: 888xxx888xxx
schema_search_path: public
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
timeout: 10000
development:
<<: *default
database: fort_development
<<: *default
database: fort_test
production:
<<: *default
database: fort_production
However, when connecting to the database via the console, whether in development (where the db does not exist) or in production, the console is connecting to the wrong database. It is also referencing a username which was removed from the database.yml file (and app subsequently restarted).
note: at one time the string myapp was referenced in the yml file for the schema_search_path attribute, however it was a commented out line (There is no certainty as to whether it was ever activated.'
> bundle exec rails c
irb(main):001:0> ActiveRecord::Base.connection_config
=> {:adapter=>"postgresql", :user=>"deploy", :password=>"888xxx888xxx", :schema_search_path=>"public", :pool=>5, :timeout=>10000, :database=>"myapp", :username=>"deploy", :host=>"127.0.0.1"}
> bundle exec rails c production
irb(main):001:0> ActiveRecord::Base.connection_config
=> {:adapter=>"postgresql", :user=>"deploy", :password=>"888xxx888xxx", :schema_search_path=>"public", :pool=>5, :timeout=>10000, :database=>"myapp", :username=>"deploy", :host=>"127.0.0.1"}
The console and the app are not in synch. What can make the console work with different values and how can it be reset to work with the proper configuration data?

For anyone stumbling upon this, there is another place where rake can take instructions for connecting via ActiveRecord.
.rbenv-vars
this file within the directory for the application was mistakenly set as:
DATABASE_URL=postgresql://deploy:PASSWORD#127.0.0.1/myapp
Another potential sources of problems (not clear why as the files properly parses in yaml) is to have the database.yml file only reference the production environment
production:
adapter: postgresql
database: fort_production
password: [...]
pool: 5
schema_search_path: public
timeout: 10000
user: deploy

Related

How to setup a MySQL DB with rails in google could SQL

I have a rails app setup on a google cloud instance. I want to have the db in a SQL instance for the extra performance. But I cant see how to do this for a rails app.
I understand you create the SQL instance, start it, install mysql, on it but then how can I have the db and tables added? Creating them manually isn't going to be the solution because normally with rails apps you run rake db:create and rake db:migrate create the DB with tables and columns but this just makes a development.sqlite3 file not a mysql db..
I haven't deployed a rails app before so I think I'm missing something.
Here is my config/databast.yml file
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
mysql_settings: &mysql_settings
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: root
host: 130.211.71.150
database: dbname
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
I cant find out what needs to be done to have the db be created and tables and columns migrated into the mysql DB.
In your gem file you can put the sqlite gem under a development/test block and you can add the mysql gem in a production block.
In your database.yml file you can keep the development settings you have currently but then add another setting for production settings. Here you can include your mysql db settings (including the host and port of your SQL instance node)
When you launch your app, you can then launch it locally in development mode to use sqlite for development, but when deploying you can launch in production mode to utilize the mysql specific settings. From there you should be able to use db:create db:migrate etc to connect to that host and setup your Db.
Here is a nice article describes this process.
https://www.digitalocean.com/community/tutorials/scaling-ruby-on-rails-setting-up-a-dedicated-mysql-server-part-2
As a team, we chose to use mysql for local development as it more closely mimics what your prod environment will be like.

Rake tasks seem to ignore database.yml configuration

I am using MAC OS X along with postgresql installed via Homebrew. I am developing using Rails 4.2.1 and ruby 2.2.0. The connection with postgresql server is fine but for some reason every application accesses the database "kstavrou" which is my system username, as a development database and creates the rest as defined by database.yml. That is troublesome if you have more than 1 rails app.
rake db:create output:
Konstantinoss-MacBook-Pro:ecomm-intel kstavrou$ rake db:create
kstavrou already exists
ecomm_intel_test already exists
strange thing is that if I empty database.yml still connects fine to postgresql and tries to create again the database "kstavrou" executing there all the migrations, without trying to create the test database.
rake db:create output:
Konstantinoss-MacBook-Pro:ecomm-intel kstavrou$ rake db:create
kstavrou already exists
database.yml
default: &default
adapter: postgresql
host: localhost
encoding: utf8
username: pguser
password: 123456
pool: 5
production:
<<: *default
database: ecomm_intel_prod
development:
<<: *default
database: ecomm_intel_dev
test:
<<: *default
database: ecomm_intel_test
Well the problem was that the ENV['DATABASE_URL'] was set (by some install script) so it was overwriting the database.yml configuration, as noted by steve klein, so I just removed it.
`export DATABASE_URL=postgres:///$(whoami)`

Correctly configuring a Postgres DB for a Rails 4.2 app on Heroku

I'm confused about how to configure the DB in a Rails 4.2 app that uses Postgres and Heroku.
Following the advice in this Heroku guide, you'll get a config/database.yml like this:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
development:
<<: *default
database: app_name_development
test:
<<: *default
database: app_name_test
production:
<<: *default
database: app_name_production
But when I tried this, my development and test environment were using the same DB as the staging environment (note the file has no configuration for staging). That's no good.
This Heroku guide for connecting to the DB in Ruby mentions that any Rails apps before 4.2 would have their database.yml file overwritten by Heroku. Heroku will parse the DATABASE_URL environment variable and create a new database.yml file.
So I guess it's possible to just leave out the configuration in database.yml for any environments that you have on Heroku, such as staging and production. Your database.yml file could essentially look like Hound's (note the lack of a production configuration).
development: &default
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
test:
<<: *default
database: app_test
But since we're using Rails 4.2, I don't think Heroku will override the database.yml file. In that case, do you have to specify DB configuration in database.yml for our environments on Heroku? Or is it still safe to leave them out? If we do need to specify the configuration for Heroku environments, will the following be sufficient?
staging:
url: <%= ENV['DATABASE_URL'] %>
production:
url: <%= ENV['DATABASE_URL'] %>
I'm also confused about the proper configuration for the development and test environments. As I mentioned above, the first configuration shown has those environments using the staging DB on Heroku, instead of a local DB.
This Heroku guide says to export the DATABASE_URL environment variable for your app to connect (once Postgres is installed and you can connect to it).
Assuming you export the DATABASE_URL env var as specified in the article, what does your configuration for development and test have to look like? Do we go with the configuration as shown in the first guide, e.g.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
development:
<<: *default
database: app_name_development
test:
<<: *default
database: app_name_test
Or do we use a configuration as shown in this Heroku guide (which uses host and username)
development:
adapter: postgresql
host: localhost
username: user
database: app-dev
Update 1: Here's what I now know. A staging and production config isn't necessary in config/database.yml if you deploy to Heroku, no matter your Rails version. Prior to 4.2, Heroku would generate it's own database.yml file based on the value of the DATABASE_URL environment variable, overwriting your config file (if it exists). Since Rails 4.2, your app will use the DATABASE_URL environment variable directly (bypassing the database.yml file), so Heroku doesn't need to (and won't) generate a config file.
I also figured out why my development and test environments were using the remote staging DB from the Heroku app instead of the local DBs specified in their database.yml config. It was because my local .env file for development is based off of my staging .env file which contains environment variables for connecting to the database such as DATABASE_URL. Because DATABASE_URL was present in my development .env file, my Rails 4.2 app was using it and thus connecting to the staging DB. To fix it, I removed those environment variables from the development .env file and created the local DBs (and ran the migrations) with bundle exec rake db:setup.
Update 2: This section of the Rails Guides goes into more detail about how to configure the DB, worth a read: http://guides.rubyonrails.org/configuring.html#configuring-a-database
Most of your assumptions are correct. The following is a reasonable database.yml configuration file.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
development:
<<: *default
database: app_name_development
test:
<<: *default
database: app_name_test
staging:
url: <%= ENV['DATABASE_URL'] %>
production:
url: <%= ENV['DATABASE_URL'] %>
Make sure that the RAILS_ENV is properly set on Heroku (either staging or production), or Rails will default to development.
Locally, the test will pick the test environment. By default, the app will start in development mode, using the development environment.
In fact, many developers choose to ignore the database.yml in version control and never publish it into the repo. The reason for that is, that databases may be different on different machines, that's a reason not to keep the configuration common.
I'm working on a Rails 4.2 project right now, and Heroku has no problem with having no database.yml at all (both with PostgreSQL and MySQL, we tested both). Why? Because DATABASE_URL provides all the information necessary to access the database, even adapter name. How? Here's the formula:
adapter://username:password#hostname:port/database?options
Locally, I'm using Postgres with peer authentication: the database server assumes the same username that I'm using in my OS, username is covered, password is useless. Local machine is assumed when no host is given, although I can't tell if it tries to communicate via TCP/IP or Unix domain sockets, so I'm fine without host.
So the configuration you refer to as "shown in the first guide" is reasonable: it contains a minimum amount of settings and allows you to create more environments quite easily.
Heroku doesn't create a database.yml on rails 4.2 because as of that version rails will detect the presence of that environment variable and use it to configure the database connection.
Adding
production:
url: <%= ENV['DATABASE_URL'] %>
Makes it a little more obvious what is happening for those who might not be aware of it but won't change the behaviour. The configuring rails guide has more info in interactions between database.yml and DATABASE_URL if you need it.
To connect with ActiveRecord without Rails (e.g. sinatra):
url = URI.parse(ENV['DATABASE_URL'])
ActiveRecord::Base.establish_connection(
encoding: 'unicode',
pool: 5,
timeout: 5000,
reconnect: true,
adapter: url.scheme,
host: url.host,
database: url.path.sub(%r{^/}, ''),
username: url.user,
password: url.password
)

Working on a rails app locally with a remote Postgres connection?

Is there a way to configure the database.yml file to connect to Heroku's Postgres remotely?
I'm having trouble understanding how Heroku, Rails and PG gem work together.
It looks like during deployment, Heroku re-writes the database.yml file - is it possible to see the contents of this updated .yml file and use it locally?
Below are the steps to access Heroku db from local development:
Login to your heroku account.
Navigate to this URL https://postgres.heroku.com/databases/ OR you can get heroku db url by running this command in the terminal: heroku pg:credentials:url
Select your application database.
You will able to see your heroku pg credentials:
Host xxxxxxxxx.77777.amazonaws.com
Database 42feddfddeee
Username 44444444444
Port xxxx
Password 777sfsadferwefsdferwefsdf
collect above details and put in your databse.yml file development evn:
adapter: postgresql
host: < host > # HOST
port: < port > # Port
database: < database name > # Database Name
username: < user_name > # User Name
password: '< password >' # Password
Restart you application,
Best of luck..!!
I am not a postgres user but this should work.
You can alter your database.yml to include host and port
production:
adapter: postgresql
encoding: unicode
database: di_production
pool: 5
username: user
password:
host: heroku.host.name
port: <postgres listen port>
And of course, you should allow connection on the server side, both at the firewall level and database level.
A simple hack to see contents of #{Rails.root}/config/database.yml is to write code to load this yml into an object, then print it out on the UI
DB_CONFIG = YAML.load(File.read("#{Rails.root}/config/database.yml", __FILE__))
puts DB_CONFIG["production"].inspect # or what ever method you want to print it
I am a bit of a newbie and may have misunderstood your question - but. Developed a ROR application using postgress database. Then uploaded to Heroku. I ran DB/Migrate/schema.rb to set up the remote Postgresql database.
From memory heroku run rake db:init (but I could be wrong). Whenver I update the database in develpment to get update in Heroku I have to promote code and run heroku run rake db:migrate
From my config/dtabase.yml
development:
adapter: postgresql
encoding: unicode
database: di_development
pool: 5
username: davidlee
password:
test:
adapter: postgresql
encoding: unicode
database: di_test
pool: 5
username: davidlee
password:
production:
adapter: postgresql
encoding: unicode
database: di_production
pool: 5
username: user
password:
and it works. I can't remember doing anything else.
Pierre

Heroku Push Errors

Good afternoon,
I browsed the other questions on this topic, and it appears nobody has asked about this issue. I was expecting otherwise!
In any case - I have a PostgreSQL database server running on my local machine with a MYAPP_DEVELOPMENT database. I've tried to do a
heroku db:push
But keep getting the following error:
Failed to connect to database:
Sequel::DatabaseConnectionError -> PGError: FATAL: role "brandon" does not exist
This obviously has something to do with the permissions & users on the local/heroku shared database, but I'm honestly not that good with this kind of stuff. Any help would be appreciated. I'm currently including the "database.yml" file in my slug to Heroku, which has all of the login/password info for my local database... hence why I was not expecting this kind of error.
Thanks!
** EDIT **
Here is the contents of my database.yml file (edited for clarity):
common: &common
adapter: postgresql
encoding: unicode
username: user
password: secret
test:
<<: *common
database: myapp_test
development:
<<: *common
database: myapp_development
production:
<<: *common
database: myapp_production
I figure it must be something on the Heroku setup side. Note that nowhere in my database.yaml file does "brandon" show up. I'm not quite sure where it is pulling that from. My database user name is not that (although that is my name haha)
This error generally means a username has not been specified under the appropriate environment in your database.yml file.
Your database.yml file should resemble the following (I use MySQL locally, hence the adapter and socket parameters):
common: &common
adapter: mysql2
encoding: utf8
reconnect: false
pool: 5
username: <your_username>
password: <your_password>
socket: /tmp/mysql.sock
development:
<<: *common
database: appname_dev
test:
...
staging:
...
production:
<<: *common
database: appname_prod
If that doesn't solve your problem, check to ensure Heroku is running in the correct environment:
heroku config --app <appname>
Ensure RACK_ENV is set to production
If not: heroku config:add RACK_ENV=production --app <appname>
(It would have been helpful if you pasted the contents of your database.yml file.)
You will have to use the Heroku specific ENV var to use the DB which is usually ENV['DATABASE_URL']
Edit: If you have a db mapper like datamapper you'd write it like this:
DataMapper.setup(:default, ENV['DATABASE_URL'] || LOCAL_DB_URL)
which then either uses the Heroku DB if it exists or defaults to your local DB which is stored in the LOCAL_DB_URL var.
Thanks for everyone's input to this one. I ended up fixing my issue by explicitly providing my login details to Heroku during the db:push... e.g.:
heroku db:push postgres://postgres:PASSWORD#127.0.0.1:5432/DATABASE
Not sure why it isn't using the (same) information in my database.yml file, but oh well.

Resources