Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8389 (new enhancement)

Opened 2 years ago

Last modified 1 year ago

[PATCH] Rebuild the test database with migrations

Reported by: matthewrudy Assigned to: core
Priority: normal Milestone: 2.x
Component: Railties Version: edge
Severity: normal Keywords: rake test tasks db:test:prepare migrations rebuilding database
Cc: cch1

Description

I wish:

  1. To add an option to rake that allows rake test to initialize the database by

a: dropping the test database

b: running rake db:migrate on the test database

  1. To alter the rake db:test:prepare task to allow this
  2. To add and make use of a rake db:test:rebuild_from_migrations task for this purpose

I note:

  1. Rails' db structure centers around the migration
  2. Running a rake:test copies the structure of the development database
  3. Running a rake:test doesn't copy any content to the test database
  4. Running migrations can create necessary data in the database
  5. One can't currently explicitly rebuild the test database via rake using migrations alone.

I believe:

  1. Recreating the test database from the migrations is a valid and consistent approach to running tests.
  2. That there are circumstances where structure-alone recreation can break rake-based testing (eg a class that caches on boot will break in rake, but not in other circumstances).
    class Cacher < ActiveRecord::Base
        INSTANCES = self.find(:all)
    
        def find_cached(id)
            return INSTANCES.detect{|i| i.id == id}
        end
    end
    
  3. Setting config.active_record.schema_format = :migrations in environment.rb is the best way to configure this option.
  4. That my solution works and is of benefit to the community.

Attachments

rebuild_test_db_from_migrations.diff (1.9 kB) - added by matthewrudy on 05/18/07 09:31:45.

Change History

05/18/07 09:31:45 changed by matthewrudy

  • attachment rebuild_test_db_from_migrations.diff added.

05/20/07 19:44:02 changed by matthewrudy

I've just seen this code snippet that does the same.

http://snippets.dzone.com/posts/show/2031

don't know if that's a better implementation. What it does differently is check the development database for its current version and then run the migrations up to that point.

05/27/07 18:30:04 changed by josh

  • summary changed from Rebuild the test database with migrations to [PATCH] Rebuild the test database with migrations.

11/01/07 15:33:03 changed by cch1

  • cc set to cch1.

I would love to see this functionality in Rails.

11/10/07 15:54:21 changed by cch1

Any chance of a comment from someone on the core team about the merit in building the test database using migrations?

It seems consistent with Rails' strong support for migrations in general and provide some middle ground between :sql and :ruby options...

  • :sql -not portable but very capable of representing The Schema.
  • :ruby -portable but not very capable of representing The Schema.
  • :migration -as portable as you want to make it and very capable of representing The Schema. And of course migrations are wonderfully adaptable to being extended with code for things like loading base data.

11/10/07 16:51:01 changed by matthewrudy

Hey guys. Thanks for taking a look at this again.

I'd kind of forgotten about it. But I totally agree with cch1, that it is entirely consistent as an option, although, in reality, it can make your tests run slow. So it is an option to think hard about using.

An additional solution to my original problem is to set config to :preload_fixtures, but the configuration for that lives somewhere else.