"rails server" using the wrong database - ruby-on-rails

I recently switched a relatively new rails app from sqlite3 to Amazon RDS and configured my database.yml file to use the RDS database in the production environment only.
But now, whenever I try to do any local action on my database (e.g. rails server, rails console, rake db:migrate, etc.) it does that action to the production DB on Amazon's servers rather than my local sqlite3 DB, which is my development DB.
# database.yml
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: mysql2
host: mydb.mydbhost.us-east-1.rds.amazonaws.com
reconnect: false
database: mydb
username: myusername
password: mypassword
What am I doing wrong?
UPDATE: Here's my environment.rb file:
# environment.rb
# Load the rails application
require File.expand_path('../application', __FILE__)
# Heroku environment variables for local use
heroku_env = File.join(Rails.root, 'config', 'heroku_env.rb')
load(heroku_env) if File.exists?(heroku_env)
# Initialize the rails application
Myapp::Application.initialize!

your not using productions settings
try
rails s -e production
or
RAILS_ENV=production rails s
RAILS_ENV=production rake db:migrate

Figured out the problem after taking a day to get away from it. It's a silly mistake on my part, but I thought I'd post the solution in case someone else encounters a similar issue.
As you can see from my environment.rb file above, I have a heroku_env.rb file, which contains all of my heroku-specific environment variables on my local machine for development purposes. In that file, I declared a ENV['DATABASE_URL'] variable, which links to my Amazon RDS database. Deleting this from the file solved the problem!
Thanks to everyone who offered answers to help!

Related

Rails : How to configure database.yml for Heroku?

I'm new on RoR. I build a little app using ActiveAdmin and Devise and I wish to deploy it on Heroku.
When I had push my app on Heroku it run properly but the db seems to be empty ! In effect, my local login dont match when I try to log in my ActiveAdmin administration panel...
In addition, the others db of my app are totaly empty...
I guess that i had not fill the database.yml correctly but I dont find how I'm suppose to do it... :/
Database.yml :
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# 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: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
I would be grateful if you could help me or direct me to a solution!
Thank you for your attention ! ^^
production:
<<: *default
database: db/production.sqlite3
You can't use sqlite3 on heroku, because it is filesystem-based and on each restart a new dyno is created, with a completely new filesystem. The old dyno is killed and your sqlite3 data file with it.
Use a client-server DB on heroku, like Postgresql. This is well covered in heroku guides.
These steps is how to push the local database to heroku and to create database
heroku pg:backups:restore 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump' DATABASE_URL
Get database info
heroku pg
-> Gave me the name of the database
eg: HEROKU_POSTGRESQL_CYAN
Reset the database on heroku
heroku pg:reset HEROKU_POSTGRESQL_CYAN
Look for the name of the local database
Opened config/database.yml and found the database
name for the development environment.
eg: fashions_development => put the name that the terminal give it to you
Run push the local database to Heroku
Opened config/database.yml and found the database
heroku pg:push fashions_development HEROKU_POSTGRESQL_CYAN

How Heroku or Rails config a database?

I am new to Rails. I pushed my toy app from cloud9 to Heroku and magically my Heroku app is using a PostgreSQL even though my database.yml from the Heroku toolbelt for my app at Heroku is as follows:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# 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: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
I know it's using a PostgreSQL because I can remotely access the DB using pgAdmin III.
So what's happening inside Heroku?
When using Heroku, the Heroku server will inject its own database yml file (That will use database url).
This is why the database.yml in your code will have no effect.
You can read more here
https://devcenter.heroku.com/articles/rails-database-connection-behavior
Heroku does not allow the use of SQLite. It will use PostgreSQL and will automatically creates one for you when you upload your app.
SQLite is not really made to be a production database. Since SQLite runs in memory, if you used it as a database on Heroku your entire database would be cleared once per day. There is more information here.
I am actually surprised you did not get any errors when deploying. In the past when I had accidentally left SQLite on, I always got a "push failed".
Heroku has a really good tutorial for changing your database from SQLite to PostgreSQL.

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.

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
)

Database connection on Heroku

Wow I've been stuck on this one for days. I'm having trouble connecting to database.yml on Heroku. I'm on Cedar and ruby 1.9.2. My dev and test dbs are sqlite3 and the prod db is postgreSQL to cope with Cedar rules.
Here is the code in my ruby script:
Rails.env.production? ? (env = "production") : (env = "development")
dbconfig = YAML::load(File.open('config/database.yml'))[env]
ActiveRecord::Base.establish_connection(dbconfig)
All goes well in local but when I push to Heroku, I get:
ArgumentError: syntax error on line 17, col 0: `adapter = uri.scheme'
from /usr/local/lib/ruby/1.9.1/syck.rb:135:in `load'
It looks like Heroku doesn't like my database.yml. Here's an overview:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: unicode
database: foo
port: 5432
host: foobar.amazonaws.com
username: foo
password: bar
First, Heroku overwrites your config/database.yml with its own Heroku-specific version. That's how Heroku automatically connects your application to its own postgresql databases. To tell Heroku about your own posgresql database, you should set up the correct config variables and you might as well omit the production database from your the config/database.yml in your repository because Heroku will ignore it anyway.
Second, the config/database.yml file is an ERB template for a YAML file. You must first run the file contents through Evaluated Ruby (ERB) before running the the output through YAML.

Resources