How do I use an Existing Database in my Rails Tests? - ruby-on-rails

I have a postgreSQL database with sample users, content and other data. How can I use it as my test database?
The tests will create new data in it, so I want to be able to easily reset it before each run of tests. Is there an easy way to convert a database into a format that can be used to easily re-create it?

This is the bad Idea to do but you can do this by changing config/database.yml
as
development:
adapter: postgresql
encoding: unicode
database: development_db
pool: 10
username: username
password: password
host: localhost
test
adapter: postgresql
encoding: unicode
database: test_db #change this db name to your development_db or whatever you want to use is test environment
pool: 10
username: username
password: password
host: localhost
Change Test environment database to which you want to use(database with sample users)
Note: If you want re-use original database again then I would suggest take backup of database with sample users then use this in test environment

If your data is not too complex, I would use fixtures for the job. The test database is rolled back after every test which can slow down your tests considerably when your test suite grows and your database must be prepopulated before every test.

Related

Rails migration with a different user than regular connections

How can I connect to the DB with a different user when running rails migrations?
The use case is to create a different Postgres user for migrations than that used to run the webserver. Whereby the regular webserver DB user is limited to CRUD for data, but not have grants or abilities like DROP etc.
In order for that to happen, I need Rails migrations to run with a different user with full Create and Drop table access. (Creds could live in the Rails encrypted credentials file, but storing them in ENV on server is probably fine).
So, to be clear, setting up the users is not a problem, I can do all that. I just want to know if there is a best-practice way to change the user/creds used by rails db:migrate
You can create a new environment which has its own database user configured in config/database.yml. Example:
default: &default
adapter: postgresql
database: dev_db_name
username: dev_user_name
password: dev_password
development:
<<: *default
production:
<<: *default
database: production_db_name
username: production_user_name
password: production_user_password
migrations:
<<: *default
database: production_db_name
username: migration_user_name
password: migration_user_password
So when you run migrations you have to specify the new environment:
RAILS_ENV=migrations rake db:migrate
IMPORTANT: When create a new environment be sure that you complete this required steps:
Create a new config/environments/YOUR_ENVIRONMENT.rb file
Create a new database configuration entry in config/database.yml if your application uses database
Create a new secret key base entry in config/secrets.yml for apps on Rails 4.1 and higher
More information click here
You can set the database connection through ENV["DATABASE_URL"]. This takes precedence over and is merged with over any settings in config/database.yml:
DATABASE_URL=postgresql://localhost/some_other_database?username=max&password=p4ssw0rd rails db:migrate
If you want to do anything more advanced like using the encrypted credentials to store the password I would recommend writing a custom rake task.

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!

Testing a Rails Application with Multiple Databases

I have a rails application which uses two databases in production environment, Users and Process. The Users model uses this customized ActiveRecord class:
class UserActiveRecord < ActiveRecord::Base
establish_connection "#{Rails.env}_users_db"
end
class User < UserActiveRecord
...
Notice that the connection to a specific database is established depending on the environment. To simplify things, in the testing environment I have a single database with all the tables from the two production databases, and my database.yml file looks something like this:
test:
adapter: postgresql
database: db_test
host: localhost
pool: ...
timeout: ...
username: ...
password: ...
test_users_db:
adapter: postgresql
database: db_test <--- Notice that this is the same database only in test env
host: localhost
pool: ...
timeout: ...
username: ...
password: ...
The application runs fine in production, but when I run any test that refers to the User class, the test blocks at the exact point where User is used and nothing happens, it doesn't show any error, it doesn't exit, it just keeps waiting.
I double-checked and the table USERS exists in the test database, in fact if I delete it manually I get the error that the table doesn't exists and when I create it again I get the same behavior reported in the previous paragraph.
I don't have a clue why this is happening, any idea on how can I solve this issue? or how can I debug it so that I can get to the root of the problem? If it helps, I'm using Ruby 1.9.3, Ruby on Rails 3.2.19 and PostgreSQL 9.3.5; any additional details will be posted as required.
Have you tried explicitly stating that it's the same database? Try editing your database.yml file to look like this:
test: &default_test
adapter: postgresql
database: db_test
host: localhost
pool: ...
timeout: ...
username: ...
password: ...
test_users_db:
<<: *default_test

Database not visible

I am trying to switch database from SqLite3 to Postgresql. My site connects to database in db/development but I can't see that file there. This is my database.yml:
development:
adapter: postgresql
database: db/development
pool: 5
timeout: 5000
username: user
When I create new database with createdb -O rolename dbname I don't see that database anywhere. I downloaded Navicat to browse my database but I can't find it.
I created a new migration after I configured app to use postgresql and it successfully migrated to that database. The problem is I can't see it anywhere. How can I insert data to database or open in in GUI if I don't see it anywhere?
When I run psql -U user db/development nothing happens.
When you're using PostgreSQL, the database: should be a simple database name, not something that looks like a filename. You should probably have something more like this:
development:
adapter: postgresql
database: appname_development
pool: 5
timeout: 5000
username: user
where appname is some sort of name for your application.
PostgreSQL doesn't use a simple file in your Rails directory tree to hold the database, PostgreSQL (and MySQL and SQL Server and ...) will use a collection of files off somewhere in its own directory and you generally shouldn't worry about where they are or what their structure is. If you want to dump the database for backup purposes then you'll want to use pg_dump.
You must have to provide host and port too.
adapter: postgresql
host: < host > # HOST
port: < port > # Port
database: < database name > # Database Name
username: < user_name > # User Name
password: '< password >' # Password
Default port of postgres will be 5432.

How does interacting with databases on different computers/far away work? (Ruby)

So, I'm fairly new to dealing with databases, and it makes sense to me when the database is on the local machine. But, how would I deal with a database that is far away/in a different computer? How is the connection set-up? How would I be able to tell Ruby to go toy with that database? I think SQLite is required to be on the local machine, but what about PostgreSQL or MySQL? I'm positive large projects require this sort of set-up with databases somewhere else and whatnot.
Also, this means teams should be able to all interact with the same database, correct?
I've tried finding articles and reading about it, but I can't seem to find any information about this.
In ruby on rails, we have a config/database.yml file where we can do database connectivity.
To connect to the remote system's database do:
1 - Give permission to your system to access the database of remote system
Grant all on databasename.* to username#ipaddress of your system identified by password
2 - Update the database.yml file
development:
adapter: mysql
database: databasename
username: username
password: password
host: ip of remote system
Configuring database.yml for your rails app
development:
adapter: mysql
database: development_database
username: root
password: [password]
host: localhost
test:
adapter: mysql
database: test_database
username: root
password: [password]
host: localhost
production:
adapter: mysql
database: production_database
username: root
password: [password]
host: localhost
Don't forget that these databases are not just files that the local program accesses -- they are servers in their own right, and the local program submits requests (select, insert etc) to them for the database server to process and return a result.
This also explains why multiple teams can access the same database -- the database server processes are just communicating with multiple programs at the same time (and the resolution of which program sees which data when they are all accessing and changing the same tables is one of the reasons why databases are so complex).
So the location of the database is only relevant in that it can take longer to send requests to, and retrieve results from, it over the network.

Resources