I am working on existing rails project and just finished writing some basic RSpec tests for that.
In our database.yml file we have connection to 3-4 different databases and all are either pointing to either production database or staging database.
For testing environment I created a different yml file called database.test.yml and then symlinked it to database.yml file, so that the correct testing databases connections are created.
My database.test.yml looks something like this:
abc:
adapter: mysql2
host: localhost
reconnect: true
username: monty
password: python
database: abc
pqr:
adapter: mysql2
host: localhost
reconnect: true
username: monty
password: python
database: pqr
test:
adapter: mysql2
host: localhost
reconnect: true
username: monty
password: python
pool: 50
database: testing
My problem now is that how can I enforce that other developers/testers use this database.test.yml and not accidentally run the main database.yml file which contains connections to staging and production databases.
EDIT:
To further clarify my question, I have establish_connection in various models that connect to different databases. So I am not sure the earlier suggestions will solve my problem.
Example:
class Abc < ActiveRecord::Base
establish_connection :abc
end
One way I can think of for solving my problem is doing something like this:
class Abc < ActiveRecord::Base
establish_connection "abc_#{Rails.env}"
end
and in the database.yml:
defaults: &defaults
adapter: mysql2
encoding: utf8
username: root
password: secret
abc_production:
database: abc
<<: *defaults
abc_development:
database: abc_devlopment
<<: *defaults
abc_test:
database: abc_test
<<: *defaults
But I am not sure if this the best practice and I really don't feel to change the application code to setup test environment is good practice.
This idea is based on: http://blog.nistu.de/2012/03/25/multi-database-setup-with-rails-and-rspec
Any nudge or pointers towards the correction direction will be greatly helpful.
You could have something, like:
default: &default
adapter: mysql2
encoding: utf8
database: my_db_name
username: root
password: my_password
host: 127.0.0.1
port: 3306
development:
<<: *default
database: xxxx_development
# 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: xxxx_test
production:
<<: *default
database: xxxx_production
Related
I have seen many answers on here to "solve" this similar issue,However my database.yml in my app is not formatted the same.
Rails S: Works
Rails C: Works
Rails db:create -> Error: fe_sendauth:
adapter: postgresql
encoding: unicode
host: ''
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: chic_development
host: localhost
test:
<<: *default
database: chic_test
production:
<<: *default
database: chic_production
username: chic
password: <%= ENV['CHIC_DATABASE_PASSWORD'] %> ```
My Postgres App is on port 5433, not 5432. I am unsure if this is relevant. But in General I am constantly having issues with my PG being flighty.
Please Help.
I looked up my postgres.app and looked to see the server name and used that as username..got my password and put them in my database.yml file like this...
username: xxxxxx
password: xxxxxxxxxxx
...under the "default" block of code then bundled everything and it worked. Thanks
I created two rails apps sample and test. There are two databases. users table in sample app with the field report_id refers to reports table in test app.
I want to display the test app data in sample app by fetching the unique_id field. I want to display reports data for a particular user by connecting these two databases.
How can I achieve this in the simplest way?
For rails6, you can provide two connections and specify databases for each connection,
eg
adapter: postgresql
encoding: unicode
username: username
password: password
pool: 5
host: localhost
development:
primary:
<<: *default
database: database1
adapter: postgresql
secondary:
<<: *default
database: database2
adapter: postgresql
and for production environment, you can use database url's like
primary:
url: <%= ENV['DATABASE_URL'] %>
secondary:
url: <%= ENV['SECONDARY_DATABASE_URL'] %>
You can connect to two databases from each rails project:
#config/sabple_database.yml
default: &default
encoding: utf8
adapter: mysql2
port: 5500
development:
<<: *default
database: sample_db
host:
username:
password:
#config/initializers/sample_database.rb
SAMPLE_DB = YAML.load_file(File.join(Rails.root, "config", "sample_database.yml"))[Rails.env.to_s]
#models
class SampleDbBase < ActiveRecord::Base
self.abstract_class = true
establish_connection SAMPLE_DB
end
#models/my_model.rb
class MyModel < SampleDbBase
end
Read more about that. So, then you can create the same models without migration(allows using Reports.find(report_id)).
Also, you can write a query to some database using ActiveRecord::Base.establish_connection and execute.
Thanks to rails 6. multi DB connection will come default from rails 6.
Please use GitHub version of rails. and configure the database.yml:
development:
primary:
<<: *default
database: multiple_databases_development
animals:
<<: *default
database: multiple_databases_development_animals
migrations_paths: "db/animals_migrate"
For more detail please have a look at https://github.com/eileencodes/multiple_databases_demo
I'm trying to upload a rails app to heroku and I'm running into problems to change my app from sqlite3 to postgres, mainly when I run rake db:create I get:
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
This is what my database.yml looks like:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: anagram_development
# 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: anagram_test
production:
<<: *default
database: anagram_production
Do you have postgresql installed locally? If so, double check that it's running. Your database.yml file is also missing some required information to connect to a postgres database. You need to provide username, password and hostname.
Here's an example of mine which connects to a local postgres db:
development:
adapter: postgresql
encoding: unicode
database: app_development
pool: 10
username: <%= %x(whoami) %>
password:
For heroku/production, you can use the following:
production:
url: <%= ENV['DATABASE_URL'] %>
pool: <%= ENV['DB_POOL'] || 10 %>
In my rails application I have two databases, I was able to get this up and running with following settings
database.yml
development: &defaults
adapter: mysql
encoding: utf8
database: <Database1>
username: <user_name>
password: <password>
host: localhost
second_development: &defaults
adapter: mysql
encoding: utf8
database: <Database2>
username: <user_name>
password: <password>
host: localhost
test: &defaults
adapter: mysql
encoding: utf8
database: <Database1_test>
username: <user_name>
password: <password>
host: localhost
second_test: &defaults
adapter: mysql
encoding: utf8
database: <Database2_test>
username: <user_name>
password: <password>
host: localhost
But the problem is when running the test cases, I'm using 'mocha', 'guard' and 'notahat-machinist', and when I try to run the test cases, it first runs the schema.rb file. But the problem is it creates the schema from the first test database only
test: &defaults
adapter: mysql
encoding: utf8
database: <Database1_test>
username: <user_name>
password: <password>
host: localhost
this will makes the test related to the second test database 'Database2_test' fail. What would be the workaround for this.
to generate a schema.rb file which has both the database schema.
thanks in advance
The schema.rb generate is define by the connection. So you need use the good connection in your test to have the good schema.
Rails new app.
The current database.yml is like that:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
I need to edit this for postgresql database.
How can I do this?
Simply:
development:
adapter: postgresql
encoding: unicode
database: blog_development
pool: 5
username: blog
password:
host: localhost
Source: Configuring Rails Applications
development:
adapter: postgresql
encoding: utf8
database: name
username: hading
password: my_db_password
pool: 5 # not mandatory
timeout: 5000 # not mandatory
host: localhost
port: your postgresql port number (5432 or 5433)
As Zabba said it's
development:
adapter: postgresql
encoding: unicode
database: blog_development
pool: 5
username: blog
password:
As mentioned in the Configuring Rails Applications. But you might want an additional min_messages: WARNING, to get rid of the nasty NOTICE messages postgresql gives you during a migration. So my database.yml entry looks like this
development:
adapter: postgresql
encoding: unicode
database: blog_development
pool: 5
username: blog
password:
min_messages: WARNING
You might be interested in generating new app with postgres default:
rails new myapp --database=postgresql
as mentioned here: https://devcenter.heroku.com/articles/getting-started-with-rails4
development:
adapter: postgresql
encoding: utf8
database: name
username: hading
password: my_db_password
host: localhost # not mandatory
pool: 5 # not mandatory
timeout: 5000 # not mandatory
Simply use
rails new app_name --database=postgresql
or if existing application try
development:
adapter: postgresql
encoding: unicode
database: app_dev
pool: 5
username: username
password: password
host: localhost
Another way is to have the common values in &default and then have individual values for the other environments, which can be based on environment variables:
default: &default
adapter: postgresql
encoding: unicode
port: <%= ENV.fetch("POSTGRESQL_PORT", "5432") %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: <%= ENV['POSTGRESQL_USER_NAME'] %>
password: <%= ENV.fetch("POSTGRESQL_PASSWORD", "myS3cr3tP4ssw0rd") %>
host: <%= ENV['POSTGRESQL_HOST'] %>
development:
<<: *default
database: <%= ENV['POSTGRESQL_DB'] %>-development
host: db
test:
<<: *default
database: <%= ENV['POSTGRESQL_DB'] %>-test
host: db
production:
<<: *default
database: <%= ENV['POSTGRESQL_DB'] %>
Here all the values can come from environment variables (if you use Docker or Bitbucket Pipelines) or you can have them in your .env files.