Run Rails Tests without Dropping Test Database - ruby-on-rails

Just wondering if there's a way to run Rails tests without dropping the database. I'm currently only executing unit tests and am using the following rake command to do so: rake test:units.
Thanks for the help in advance!
Just in case this is relevant:
Rails 3
Ruby 1.8.7 (MRI)
Oracle 11g Database
activerecord-oracle_enhanced-adapter

In Rails 5 (and possibly earlier versions), just comment-out the following line in spec/rails_helper.rb:
ActiveRecord::Migration.maintain_test_schema!
This will prevent rake test or rspec from attempting to drop your test DB. You'll need to run migrations manually as well.

For Rails 5.2 this behaviour can be modified setting maintain_test_schema to false in test/test_helper.rb before importing rails/test_help:
ActiveRecord::Base.maintain_test_schema = false
require "rails/test_help"
rails/test_help will check the value of maintain_test_schema to decide if it has to drop/create/migrate the test database or not.

After doing some research, I have found that there isn't a way to do this. The test rake tasks will always drop the database, even when providing the TEST= option as Bohdan suggests.
By using the --trace option, this can be proven. Here is the output:
$ rake test:units TEST=test/unit/post_test.rb --trace
(in /Users/johnnyicon/Development/ror/test-app)
** Invoke test:units (first_time)
** Invoke test:prepare (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
** Execute test:prepare
** Execute test:units
Reading through the Ruby on Rails Guides for Testing, it describes what some of these rake tasks mean. The one to pay particular attention to is the db:test:load task, which you see on the 7th line from the bottom of the output as ** Execute db:test:load. The guides say the following about this task:
Recreate the test database from the
current schema.rb
So even if I were to execute the unit tests one by one as Bohdan suggests, the rake task would still recreate the database. Not the answer I was hoping for, but it isn't a problem anymore.
The reason I was asking to begin with was because I did not have access to another database to use for testing, so I was using my development database for testing as well. But since then, I've been able to get another database dedicated for testing.
Thanks anyway Bohdan! I appreciate the help!

This is a rather old post that does the monkey patching of overriding purge/load tasks:
http://www.pervasivecode.com/blog/2007/09/22/making-rails-raketest-not-drop-your-pgsql-database/

For those looking for a way to skip Rails' default behavior, try adding this to your Rakefile:
Rake::Task["db:test:prepare"].clear
Rake::Task["db:test:load"].clear
Rake::Task["db:test:purge"].clear

Could you not write a custom Rake task that monkey patched the Rake db:test:load task to do nothing?

Related

How to debug rails db:structure:dump?

I have the line config.active_record.schema_format = :sql in my application.rb file but I'm still having the problem that when I type rails db:migrate it doesn't output the :sql version. In fact it doesn't output anything. When I type in rails db:structure:dump it doesn't output anything either. However, when I type in rails db:schema:dump it outputs the schema.rb file. I'm using postgresql and rails 5.0.
It seems to me that for some reason rails db:structure:dump is not working but I don't know how to debug this. Any help will be greatly appreciated.
~/main$ rails db:schema:dump --trace
** Invoke db:schema:dump (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:schema:dump
Similar but different question:
In Rails 5, setting config.active_record.schema_format = :sql but still getting schema.rb created on db:migrate

Rails activestorage does not create migration files

I make an image-upload for a user profile using the activestorage gem built into rails, but it is not working, the intriguing part is that there are no errors, the migration just does not get created.
What is wrong here, any ideas on how I can fix this?
rails version - 6.0
I added require 'active_storage/engine' to the config/application.rb file already
And then ran rails active_storage:install
What is even more intriguing is that I get the same output without adding the require active_storage/engine to application.rb. And there are no errors
output
** Invoke active_storage:install (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute active_storage:install
** Invoke active_storage:install:migrations (first_time)
** Execute active_storage:install:migrations
** Invoke railties:install:migrations (first_time)
** Invoke db:load_config (first_time)
** Invoke environment
** Execute db:load_config
** Execute railties:install:migrations```
I expect the migration file to be created after this but it is not.

Test database structure is not created when using Factory girl with Rspec (Rails)

I am using FactoryGirl in my rails application instead of Fixtures.
When i try to use factory girl in my test and create some test data, it shows like
PG:Error relation "users" doesn't exists (i have a model named User)
But when i run rake db:test:clone, and then run the test, the test is passed. The rake db:test:clone command clones all the table structure from development db to test db, and this fixes the issue.
But is there any way for me to not to run rake db:test:clone when using FactoryGirl?
or what am i missing?
Update :
I found out one other thing, i have another application which uses Rspec and FactoryGirl. In that application below are executed when running rake spec --trace command
** Invoke spec (first_time)
** Invoke db:test:clone_structure (first_time)
** Invoke db:structure:dump (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:structure:dump
** Invoke db:test:load_structure (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:test:purge
** Execute db:test:load_structure
** Invoke db:structure:load (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:structure:load
But in any of the new application i see the below executed when running rake spec --trace
** Invoke spec (first_time)
** Invoke noop (first_time)
** Execute noop
** Execute spec
Please suggest what am i missing?
regards
Balan
Every time you run a db:migrate, run a db:test:prepare as well, so the database changes are mirrored on your test database as well.
I found the solution, we need to create a rake task customrake.rake under /lib/tasks
and add the line task :spec => 'db:test:prepare', this will make sure rake db:test:prepare is executed before running Specs.

rake spec - prerequisites seem nonsensical

I'm trying to set up a reliable continuous integration server that runs rspec. I've read this answer, and indeed if I run db:reset that seems to get me in a good state to run the tests. In fact, if I run db:reset and then after it, on the cli, run rake spec - my tests run.
However, if I make one rake task that does both, like this:
desc 'Run all tests, used by Jenkins CI'
task :run => [ 'db:reset' ] do
Rake::Task["spec"].invoke
end
By the time the tests run, the database is empty. That appears to be because rake:spec calls the following:
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load
** Execute spec
If I'm reading it right, it will (line 2) abort if there are pending migrations. Then, eventually db:schema:load again, which loads the database from schema rb... which I'd already done. And, it makes me particularly unclear why, if it's going to re-wipe the db, it would abort if migrations wouldn't have been applied.
Worse still, in my case the first thing the spec task does is bail because the users table doesn't exist... and that was defined in schema.rb (which at this point has been loaded twice).
Any ideas where I'm misunderstanding?

rake db:migrate doesn't work and brings me back to the command line rails 3.1.1

Using rails 3.1.1 for windows with railsinstaller
>rake db:migrate
after a pause, brings me right back to the command line. No errors, no messages, just right back to the command line.
I tried
>rake --trace db:migrate
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
** Execute db:migrate
** Invoke db:schema:dump (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:schema:dump
Doesn't look like anything is wrong, but obviously something isn't working right.
Can anyone help?
If there are no migrations to be run, there will be no output. The --trace command outputs the various steps that rake goes through to prepare, execute, and clean up after the migration. However, when it sees that there is no migration to be run, then it doesn't actually make any DB changes. Only DB changes cause additional output.
The only difference between this and a migration is that the changes to the DB will be output to the command line. No changes => no output.
Make sure you are in the right folder (maybe in the right branch, when using git) and make sure that you have created a migration file.
rails generate migration MigrationName
Edit the generated file as you wish.

Resources