rake corrupts development database - ruby-on-rails

When I run rake to run specific/all test(s), it corrupts development database.
I verified that db for each environment is different.
database.yml
postgre_common_config: &postgre_common_config
adapter: postgresql
encoding: unicode
pool: 5
username: postgres # if using default PostgreSQL user then the value should be: postgres
password: postgres
host: localhost
development:
database: mag_development
<<: *postgre_common_config
test:
database: mag_test
<<: *postgre_common_config
production:
database: mag_production
<<: *postgre_common_config
test_helper.rb
ENV['RAILS_ENV'] = 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
fixtures :all
end
This is really annoying as it takes lot of time to prepare data to test specific feature manually. Please help.

Related

rails deploy migrating run with root

When i do deploy, migrating run as root.
i have error:
Mysql2::Error: Access denied for user 'root'#'localhost' (using password: YES)
But in my /shared/config/database.yml
default: &default
adapter: mysql2
pool: 5
timeout: 5000
database: sa
development:
<<: *default
user: root
password: ""
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
user: sa
password: "mypassss"
my capfile:
require "capistrano/rvm"
require "capistrano/bundler"
require "capistrano/rails/migrations"
require "capistrano/passenger"
Why migrating run as root but not as user sa?
Yhe problem is that i use user: sa , but need to use username: sa.
Solved

RSpec slow when Rails use another Rails DB

I got a strange situation on RSpec when Rails testing.
Normaly by using spring RSpec file taken time is very fast.
But, when using another Rails DB RSpec file taken time is 2 or 3 seconds.
My Rails app use another Rails app database.yml to use the same db.
Why is RSpec so slow? and How do I make RSpec fast?
I confirmed this is happened at Rails4 and Rails5.
Environment
DB PostgreSQL 9.4.5
This is my code.
class Test
def self.test
'test'
end
end
This is my test code.
require 'rails_helper'
RSpec.describe Test do
describe "Test.test" do
it "test" do
expect(Test.test).to eq 'test'
end
end
end
This is my database.yml. Two rails app use same yml file.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: qserver_development
test:
<<: *default
database: qserver_test

How to set up test database configuration in Rails

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

DatabaseCleaner cleans test and development?

Why does this configuration seem to clean both my test and my development databases? It's pretty annoying to have to reseed development every time I run rspec.
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
database.yml
development:
adapter: postgresql
database: m_development
encoding: utf8
pool: 5
username: booltox
password:
test:
adapter: postgresql
database: m_test
encoding: utf8
pool: 5
username: booltox
password:
Pretty foolish of me but this might help someone else. Don't forget to define your environment in your spec_helper.rb (thanks #dgilperez):
ENV['RAILS_ENV'] ||= 'test'

Rails database migration failing because of Postgres inet type

I have a Rails(4.1.0) app that works fine on Heroku. However, on my local machine, rake db:migrate fails due to a table for devise that uses inet datatype and I am using sqlite3 for testing.
I have included the postgres gem as well as postgres_ext but still coming up with the error:
undefined method `inet' for #<ActiveRecord::ConnectionAdapters::Table:0x00000005fae9e8>/home/app/db/migrate/20141107192501_add_devise_to_users.rb:19:in `block in up'
If testing locally using Postgres is acceptable just setup the correct adapters. A sample database.yml:
common: &common
adapter: postgresql
encoding: utf8
template: template0 # Required for UTF8 encoding
username: <%= ENV["POSTGRES_USER"] %>
password: <%= ENV["POSTGRES_PASSWORD"] %>
host: <%= ENV["POSTGRES_HOST"] %>
development:
<<: *common
database: 'my_app_dev'
# 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:
<<: *common
database: 'my_app_test'

Resources