Launch rspec tests with sqlite :memory: - "migrations are pending" - ruby-on-rails

I want to run tests using rspec on my ruby on rails app using sqlite with the memory database. However, everytime I launch rspec it tells me that migrations are pending, event if I run the migrations before hand. Is there a way to do the migrations everytime before I run the tests ?
Here's my database configuration
test:
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
database: ":memory:"

You need to load schema in your tests instead of relying on migrations.
As advised in this blogpost replace
ActiveRecord::Migration.maintain_test_schema!
with
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.verbose = false
load "#{Rails.root.to_s}/db/schema.rb"

Related

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

Heroku create db with default data

I'm noobie in rails and I have a problem with db. My 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: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
And I succeeded in filling my db with this
namespace :db do
desc "Erase and fill database"
task :populate => :environment do
require 'populator'
require 'faker'
[Country, Region, City, Turbaza].each(&:delete_all)
ActiveRecord::Base.transaction do
Country.populate 5 do |country|
country.name = Faker::Address.country
Region.populate 1..2 do |region|
region.country_id = country.id
region.name = Faker::Address.state
City.populate 1..2 do |city|
city.region_id = region.id
city.name = Faker::Address.city
Turbaza.populate 1..2 do |turbaza|
turbaza.city_id = city.id
turbaza.name = Populator.words(1..3).titleize
end
end
end
end
end
end
end
So I've got my development db filled with data, but I cannot understand how I can do this to my production db and. Please, could anyone help me with this?
if you have already deployed you app into Heroku you can run you rake task via heroku-cli
https://devcenter.heroku.com/articles/heroku-command
heroku run rake db:populate
P.S Heroku provides only pg for free. So i you want to upload your up with pg i have to do
add gem 'pg' to Gemfile
bundle install
commit you changes
rebuild your project in heroku
Running heroku run rake db:populate should do the trick, have you tried? And (IMO) better solution would be just using seeds (http://railscasts.com/episodes/179-seed-data) and running heroku run rake db:seed

Using Sqlite3 test database and Postgres dev/production database in Rails

I currently have a rails project which I deploy to a production server which uses a postgres database. I develop my rails project in Windows, which means that if I want to test locally, I have to change all of the databases in my database.yml file from postgres over to sqlite3 (because setting up Windows to run a postgres server appears to be a pain).
What I would like to be able to do is format my database.yml something like this:
development:
adapter: postgresql
encoding: utf8
database: <%= begin IO.read("/home/www-data/.db/.dev_name") rescue "" end %>
pool: 5
username: <%= begin IO.read("/home/www-data/.db/.user") rescue "" end %>
password: <%= begin IO.read("/home/www-data/.db/.pass") rescue "" end %>
# 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: postgresql
encoding: utf8
database: <%= begin IO.read("/home/www-data/.db/.prod_name") rescue "" end %>
pool: 5
username: <%= begin IO.read("/home/www-data/.db/.user") rescue "" end %>
password: <%= begin IO.read("/home/www-data/.db/.pass") rescue "" end %>
That way I can run rails s -e test locally and test with an sqlite3 database, but when I deploy to my development and production servers I can use postgres.
The problem I am having is that, with the changes to my database.yml shown above, when I run rails s -e test locally I get an error saying that rails could not find the pg gem which seems to imply that it is still trying to use either the development or the production server.
With all the warnings acknowledged, the answer to the question would be to use group in your Gemfile like
gem 'pg', group: [:development, :production]
gem 'sqlite3', group: :test

how to trigger a rake:db:prepare that it also migrates an external database connection

I'm currently trying to perform some tests on a rails app that has two database connections.
As of course I wouldn't like to delete the 2nd database each time I perform a test, I have set up a mechanism to connect to another 2nd-DB if the environment is "test" (see below).
My question: How could I tell rake test to also perform a rake db:test:prepare on the 2nd DB?
This query already touched my problem but couldn't help me completely.
Some code to explain:
My ActiveRecord-Classes that connect to the 2nd db inherit from the following class InformixConnect:
class InformixConnect < ActiveRecord::Base
self.abstract_class = true
case Rails.env
when 'production', 'development'
establish_connection :development_informix
when 'test'
establish_connection :test_informix_dummy
else
raise "Please specify a correct informix Environment."
end
end
like
class RenewalNotify < InformixConnect
set_table_name :renewal_notify
set_primary_key :renewal_notify_id
end
(yes, I know... The schema does not follow the rails conventions. It's a legacy one)
database.yml contains
...
development:
adapter: postgresql
database: rails_development
host: 127.0.0.1
reconnect: true
username: rails
password: guessone
...
development_informix:
adapter: informix
database: SOMETHING
username: yyy
password: yyy
test_informix_dummy:
adapter: sqlite3
database: db/test_informix_dummy.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: &TEST
adapter: sqlite3
database: db/test.db
verbosity: silent
timeout: 5000
So I'd need to setup the "test_informix_dummy" db each time I perform a rake test

Factory girl saving records in my development database

I have a very strange problem and I don't know where I should look to find it. I am developing a rails 3 app using rspec and factory girl for testing. For some reason, whenever I run any rails commands (eg to rake the db, start the dev server etc) one factory user is created and stored in my development database. The worst part is, it always has the same email, which I am validating the uniqueness of in my app, so the commands won't run until I go in manually delete the record.
I have looked all through my factories file and I don't think I am doing anything strange there, and suggestions where else I might for the code that is doing this?
EDIT: HERE IS MY database.yml
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysql2
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: ATBTracking_development
pool: 5
username: [NOT TELLING]
password: [NOT TELLING]
socket: /var/run/mysqld/mysqld.sock
# 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: mysql2
encoding: utf8
reconnect: false
database: ATBTracking_test
pool: 5
username: [NOT TELLING]
password: [NOT TELLING]
socket: /var/run/mysqld/mysqld.sock
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: ATBTracking_production
pool: 5
username: [NOT TELLING]
password: [NOT TELLING]
socket: /var/run/mysqld/mysqld.sock
I figured it out. In my Gemfile, I had:
group :development, :test do
gem 'capybara'
gem "rspec-rails"
gem "guard-rspec"
gem "factory_girl_rails"
...
end
I moved factory girl out of this block onto its own line so it is in the test group only like this:
gem 'factory_girl_rails', :group => :test
No more problems
db/Seeds.rb maybe...but I think that only runs on db:reset and db:seed

Resources