# Rake test:units is trying to load my schema every time

**URL:** <https://www.ruby-forum.com/t/rake-test-units-is-trying-to-load-my-schema-every-time/67755>\
**Category:** Rails\
**Created:** [September 6, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/rake-test-units-is-trying-to-load-my-schema-every-time/67755 "2006-09-06T16:39:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chubbard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@chubbard](https://www.ruby-forum.com/u/chubbard)\
**Post date:** [September 6, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/rake-test-units-is-trying-to-load-my-schema-every-time/67755/1 "2006-09-06T16:39:48Z")

</div>

My unit tests are failing to run. Every time I run them it fails before  
getting to the actual tests because it keeps trying to update the  
database schema (db:schema:load). My tests were running just fine, then  
I created and new migration, updated my development database, rake  
db:test:clone\_structure, and then rake test:units. Why is rake trying  
to update my database for me? How do I make it stop or realize that my  
test database it up to date? I’ve checked the schema\_version table, and  
it says the latest version.

Charlie

---

<div class="post-metadata">

**Author:** ![chubbard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@chubbard](https://www.ruby-forum.com/u/chubbard)\
**Post date:** [September 6, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/rake-test-units-is-trying-to-load-my-schema-every-time/67755/2 "2006-09-06T17:52:24Z")

</div>

On Wed, Sep 06, 2006, Charlie H. wrote:

> My unit tests are failing to run. Every time I run them it fails before  
> getting to the actual tests because it keeps trying to update the  
> database schema (db:schema:load). My tests were running just fine, then  
> I created and new migration, updated my development database, rake  
> db:test:clone\_structure, and then rake test:units. Why is rake trying  
> to update my database for me? How do I make it stop or realize that my  
> test database it up to date? I’ve checked the schema\_version table, and  
> it says the latest version.

This is normal. The tests will essentially drop your test database and  
recreate it each time they’re run, in order to ensure that they’re  
working off the most current incarnation of your app.

If for some reason they’re unable to do that, it says to me something is  
broken with your project or your database. What is the message you get  
when they fail?

Ben

---

<div class="post-metadata">

**Author:** ![chubbard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@chubbard](https://www.ruby-forum.com/u/chubbard)\
**Post date:** [September 6, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/rake-test-units-is-trying-to-load-my-schema-every-time/67755/3 "2006-09-06T18:09:03Z")

</div>

The message I keep getting is that one of my tables already exists.  
It’s the first one in my schema. I noticed that some of my tests have  
trouble between runs when it goes to drop certain tables, and it says  
there’s data or foreign key problems. I fgured that was the order of  
the fixtures, but I don’t know. How does Rails know the order to drop  
things in so you don’t get foreign key constraint issues?

How can I debug this? Is there a rake tasks that lets me drop my  
database? Is it user permissions? I’m logging in as root.

Charlie

Ben B. wrote:

> On Wed, Sep 06, 2006, Charlie H. wrote:
> 
> > My unit tests are failing to run. Every time I run them it fails before  
> > getting to the actual tests because it keeps trying to update the  
> > database schema (db:schema:load). My tests were running just fine, then  
> > I created and new migration, updated my development database, rake  
> > db:test:clone\_structure, and then rake test:units. Why is rake trying  
> > to update my database for me? How do I make it stop or realize that my  
> > test database it up to date? I’ve checked the schema\_version table, and  
> > it says the latest version.
> 
> This is normal. The tests will essentially drop your test database and  
> recreate it each time they’re run, in order to ensure that they’re  
> working off the most current incarnation of your app.
> 
> If for some reason they’re unable to do that, it says to me something is  
> broken with your project or your database. What is the message you get  
> when they fail?
> 
> Ben

---

<div class="post-metadata">

**Author:** ![chubbard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@chubbard](https://www.ruby-forum.com/u/chubbard)\
**Post date:** [September 6, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/rake-test-units-is-trying-to-load-my-schema-every-time/67755/4 "2006-09-06T19:31:51Z")

</div>

I’m using MYSQL 5. I know I shouldn’t use root, but for development and  
testing I don’t see the harm. Once in production I’ll use different  
users.

If it’s deleting tables how does it get the order right for deleting  
tables without violating foreign key constraints?

Ben B. wrote:

> On Wed, Sep 06, 2006, Charlie H. wrote:
> 
> > The message I keep getting is that one of my tables already exists.  
> > It’s the first one in my schema. I noticed that some of my tests have  
> > trouble between runs when it goes to drop certain tables, and it says  
> > there’s data or foreign key problems. I fgured that was the order of  
> > the fixtures, but I don’t know. How does Rails know the order to drop  
> > things in so you don’t get foreign key constraint issues?
> 
> Well, I’m not entirely sure this is correct, but I _believe_ that it  
> works by deleting all the tables, then cloning the development database.  
> Yeah, digging through the rake tasks confirms this; the test task  
> (through a few steps of prereqs) calles db:test:purge.
> 
> > How can I debug this? Is there a rake tasks that lets me drop my  
> > database? Is it user permissions? I’m logging in as root.
> 
> db:test:purge will empty your test database.
> 
> Don’t log in as root 🙂 Set up new users for your databases.
> 
> What database engine are you using? Is your test database different  
> from your production/development databases?
> 
> Ben

---

<div class="post-metadata">

**Author:** ![chubbard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@chubbard](https://www.ruby-forum.com/u/chubbard)\
**Post date:** [September 6, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/rake-test-units-is-trying-to-load-my-schema-every-time/67755/5 "2006-09-06T19:24:38Z")

</div>

On Wed, Sep 06, 2006, Charlie H. wrote:

> The message I keep getting is that one of my tables already exists.  
> It’s the first one in my schema. I noticed that some of my tests have  
> trouble between runs when it goes to drop certain tables, and it says  
> there’s data or foreign key problems. I fgured that was the order of  
> the fixtures, but I don’t know. How does Rails know the order to drop  
> things in so you don’t get foreign key constraint issues?

Well, I’m not entirely sure this is correct, but I _believe_ that it  
works by deleting all the tables, then cloning the development database.  
Yeah, digging through the rake tasks confirms this; the test task  
(through a few steps of prereqs) calles db:test:purge.

> How can I debug this? Is there a rake tasks that lets me drop my  
> database? Is it user permissions? I’m logging in as root.

db:test:purge will empty your test database.

Don’t log in as root 🙂 Set up new users for your databases.

What database engine are you using? Is your test database different  
from your production/development databases?

Ben

---

<div class="post-metadata">

**Author:** ![chubbard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@chubbard](https://www.ruby-forum.com/u/chubbard)\
**Post date:** [September 6, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/rake-test-units-is-trying-to-load-my-schema-every-time/67755/6 "2006-09-06T19:33:06Z")

</div>

> What database engine are you using? Is your test database different  
> from your production/development databases?

Sorry forgot your last question. Yes I have three seperate database for  
development, testing, and production.

Charlie

---

<div class="post-metadata">

**Author:** ![chubbard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@chubbard](https://www.ruby-forum.com/u/chubbard)\
**Post date:** [September 6, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/rake-test-units-is-trying-to-load-my-schema-every-time/67755/7 "2006-09-06T20:52:49Z")

</div>

On 9/6/06, Charlie H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > works by deleting all the tables, then cloning the development database.  
> > What database engine are you using? Is your test database different  
> > If it’s deleting tables how does it get the order right for deleting  
> > tables without violating foreign key constraints?

It doesn’t get the order right, basically. You will need to specify  
your fixtures in an order that will allow them to be dropped without  
any problems.

DHH has said not to use database constraints [1], and as such we  
continue to run into problems like these. I’ve found your best bet is  
not to use a database at all when testing, and to use mocks instead  
[2].

Pat

[1] [http://www.loudthinking.com/arc/000516.html](http://www.loudthinking.com/arc/000516.html)  
[2] [http://glu.ttono.us/articles/2006/09/02/mocking-models](http://glu.ttono.us/articles/2006/09/02/mocking-models)

---

<div class="post-metadata">

**Author:** ![chubbard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@chubbard](https://www.ruby-forum.com/u/chubbard)\
**Post date:** [September 6, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/rake-test-units-is-trying-to-load-my-schema-every-time/67755/8 "2006-09-06T21:48:56Z")

</div>

Thanks. After I did a rake db:test:purge is corrected itself and was  
happy again. I guess whenever I do a db:test:clone\_structure that  
causes the unit test framework to be unhappy.

I’m afraid I don’t buy DHH’s opinion on constraints. I too advocate for  
application database architecture and laughed at the 90210 joked he said  
because he’s right it’s the old way of thinking about business  
integration. But, I’m afraid constraints are an imporant thing and  
rails should learn to work with them. The biggest argument I have for  
adding constraints to your data is so that you’ll know when something is  
violating your data. Does your program work because you added  
constraints? No that’s not the point of them. At least you’ll know  
when you aren’t working properly and the exception stack trace will  
point you to the offender fairly quickly.

For now I’ll just keep adding my fixtures in the right order. Actually  
I’m getting pretty sick of fixtures as a means to communicate test data.  
There has to be a better way.

Thanks  
Charlie

Pat M. wrote:

> On 9/6/06, Charlie H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > works by deleting all the tables, then cloning the development database.  
> > > What database engine are you using? Is your test database different  
> > > If it’s deleting tables how does it get the order right for deleting  
> > > tables without violating foreign key constraints?
> 
> It doesn’t get the order right, basically. You will need to specify  
> your fixtures in an order that will allow them to be dropped without  
> any problems.
> 
> DHH has said not to use database constraints [1], and as such we  
> continue to run into problems like these. I’ve found your best bet is  
> not to use a database at all when testing, and to use mocks instead  
> [2].
> 
> Pat
> 
> [1] [http://www.loudthinking.com/arc/000516.html](http://www.loudthinking.com/arc/000516.html)  
> [2] [http://glu.ttono.us/articles/2006/09/02/mocking-models](http://glu.ttono.us/articles/2006/09/02/mocking-models)
