# Tables deleted when running rake test:units

**URL:** <https://www.ruby-forum.com/t/tables-deleted-when-running-rake-test-units/192596>\
**Category:** Rails\
**Created:** [August 2, 2010, 7:38pm UTC](https://www.ruby-forum.com/t/tables-deleted-when-running-rake-test-units/192596 "2010-08-02T19:38:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![simaofreitas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@simaofreitas](https://www.ruby-forum.com/u/simaofreitas)\
**Post date:** [August 2, 2010, 7:38pm UTC](https://www.ruby-forum.com/t/tables-deleted-when-running-rake-test-units/192596/1 "2010-08-02T19:38:21Z")

</div>

Hi there! I’m creating an application in RoR and I’m implementing unit  
testing in all my models.

When I run every test on his own (by running ruby  
test/unit/some\_test.rb) all tests are successful.

But when I run all tests together (by running rake test:units) some  
tables from both databases (development e test) are deleted.

I’m using raw SQL (mysql) do create tables because I need composite  
primary keys and physical constraints so I figured it would be the best.  
Maybe this be the cause?

## All my tests are in this form:

require File.dirname(FILE) + ‘/…/test\_helper’  
require File.dirname(FILE) + ‘/…/…/app/models/order’

class OrderTestCase \< Test::Unit::TestCase

def setup

@order = Order.new(  
:user\_id =\> 1,  
:total =\> 10.23,  
:date =\> Date.today,  
:status =\> ‘processing’,  
:date\_concluded =\> Date.today,  
:user\_address\_user\_id =\> 3,  
:user\_address\_address\_id =\> 5,  
:creation\_date =\> Date.today,  
:update\_date =\> Date.today  
)  
end

################ Happy Path

def test\_happy\_path

assert @order.valid?, @order.errors.full\_messages

## end (…)

The errors I get when running the tests are something like this:

1. Error: test\_empty\_is\_primary(AddressTestCase):  
ActiveRecord::StatementInvalid: Mysql::Error: Table  
‘shopshop\_enterprise\_test.addresses’ doesn’t exist: SHOW FIELDS FROM  
addresses  
/test/unit/address\_test.rb:9:in new’  
/test/unit/address\_test.rb:9:insetup’

Any guesses? Thanks!

PS: When using postgres as the database engine, everything works fine  
with rake test:units! (of course, with the correct changes so the sql  
statements can work with postgres)

---

<div class="post-metadata">

**Author:** ![simaofreitas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@simaofreitas](https://www.ruby-forum.com/u/simaofreitas)\
**Post date:** [August 2, 2010, 7:48pm UTC](https://www.ruby-forum.com/t/tables-deleted-when-running-rake-test-units/192596/2 "2010-08-02T19:48:13Z")

</div>

SimÃ£o Freitas wrote:

> Hi there! I’m creating an application in RoR and I’m implementing unit  
> testing in all my models.
> 
> When I run every test on his own (by running ruby  
> test/unit/some\_test.rb) all tests are successful.

Slightly off-topic tip: use RSpec instead of Test::Unit. It’s a lot  
nicer.

> But when I run all tests together (by running rake test:units) some  
> tables from both databases (development e test) are deleted.

That’s odd. What is your database.yml file like?

> I’m using raw SQL (mysql) do create tables

With migrations or not?

> because I need composite  
> primary keys and physical constraints so I figured it would be the best.

BAD IDEA! Use the composite\_primary\_keys and foreigner plugins.

> Maybe this be the cause?

Perhaps. Are all your tables mentioned in schema.rb?

> ## All my tests are in this form:
> 
> require File.dirname(FILE) + ‘/…/test\_helper’  
> require File.dirname(FILE) + ‘/…/…/app/models/order’
> 
> class OrderTestCase \< Test::Unit::TestCase
> 
> def setup
> 
> @order = Order.new(  
> :user\_id =\> 1,  
> :total =\> 10.23,  
> :date =\> Date.today,  
> :status =\> ‘processing’,  
> :date\_concluded =\> Date.today,  
> :user\_address\_user\_id =\> 3,  
> :user\_address\_address\_id =\> 5,  
> :creation\_date =\> Date.today,  
> :update\_date =\> Date.today  
> )  
> end

You probably should start using factories (I’m fond of Machinist for  
this).

> ################ Happy Path
> 
> def test\_happy\_path
> 
> assert @order.valid?, @order.errors.full\_messages
> 
> ## end (…)
> 
> The errors I get when running the tests are something like this:
> 
> 1. Error: test\_empty\_is\_primary(AddressTestCase):  
> ActiveRecord::StatementInvalid: Mysql::Error: Table  
> ‘shopshop\_enterprise\_test.addresses’ doesn’t exist: SHOW FIELDS FROM  
> addresses  
> /test/unit/address\_test.rb:9:in new’  
> /test/unit/address\_test.rb:9:insetup’
> 
> Any guesses? Thanks!
> 
> PS: When using postgres as the database engine, everything works fine  
> with rake test:units! (of course, with the correct changes so the sql  
> statements can work with postgres)

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![simaofreitas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@simaofreitas](https://www.ruby-forum.com/u/simaofreitas)\
**Post date:** [August 2, 2010, 9:14pm UTC](https://www.ruby-forum.com/t/tables-deleted-when-running-rake-test-units/192596/3 "2010-08-02T21:14:51Z")

</div>

I didn’t know about Rspec and Machinist. I’ll try them thanks for the  
recommendation.

I’m using composite\_primary\_keys, I didn’t explain myself well lol.

I don’t use migrations.

OH! You’re right! There are tables missing from the schema.rb file.  
Don’t know the reason because when I ran the tests (and db creation  
previously) separately there was no error. Anyway, there’s an error in  
every table that was missing, related to the BIT datatype. Something  
like this:

# Could not dump table “addresses” because of following StandardError

# Unknown type ‘bit(1)’ for column ‘is\_primary’

So, maybe that datatype isn’t supported? I think I’ll just use TINYINTs  
lol

By the way, I don’t have a very large knowledge about rails, i’m still  
figuring it out, but here’s another question:

Can I use migrations with physical constraints in the database? Won’t  
they become “nulled” and inconsistent after a migration or any change in  
the database? All that will be lost right?

Thanks a lot for your help!

---

<div class="post-metadata">

**Author:** ![simaofreitas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@simaofreitas](https://www.ruby-forum.com/u/simaofreitas)\
**Post date:** [August 2, 2010, 9:30pm UTC](https://www.ruby-forum.com/t/tables-deleted-when-running-rake-test-units/192596/4 "2010-08-02T21:30:24Z")

</div>

I fixed the problem of the “disappearing tables” with the following code  
in environment.rb:

config.active\_record.schema\_format = :sql

The problem was with some datatypes not recognized by rails (BIT(1) and  
(YEAR(4)) I guess. All works fine now.

But I’ll do what you recommend and start using migrations!

Again, I can’t thank you enough! Thanks a lot!

---

<div class="post-metadata">

**Author:** ![simaofreitas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@simaofreitas](https://www.ruby-forum.com/u/simaofreitas)\
**Post date:** [August 2, 2010, 9:19pm UTC](https://www.ruby-forum.com/t/tables-deleted-when-running-rake-test-units/192596/5 "2010-08-02T21:19:37Z")

</div>

Please quote when replying. It is very difficult to follow the  
conversation if you don’t.

SimÃ£o Freitas wrote:

> I didn’t know about Rspec and Machinist. I’ll try them thanks for the  
> recommendation.

You’re welcome.

> I’m using composite\_primary\_keys, I didn’t explain myself well lol.
> 
> I don’t use migrations.

Well, start now! There are many advantages and virtually no  
disadvantages to using migrations.

> OH! You’re right! There are tables missing from the schema.rb file.

That would be a serious problem. The test framework uses rake  
db:schema:load to create the database.

> Don’t know the reason because when I ran the tests (and db creation  
> previously) separately there was no error. Anyway, there’s an error in  
> every table that was missing, related to the BIT datatype. Something  
> like this:
> 
> # Could not dump table “addresses” because of following StandardError
> 
> # Unknown type ‘bit(1)’ for column ‘is\_primary’
> 
> So, maybe that datatype isn’t supported? I think I’ll just use TINYINTs  
> lol

Use migrations and you won’t have to worry, since Rails handles the  
abstraction.

> By the way, I don’t have a very large knowledge about rails, i’m still  
> figuring it out, but here’s another question:
> 
> Can I use migrations with physical constraints in the database?

You mean such as foreign key constraints?

> Won’t  
> they become “nulled” and inconsistent after a migration or any change in  
> the database?

What gives you that idea?

> All that will be lost right?

No! Use the foreigner plugin like I suggested.

> Thanks a lot for your help!

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![simaofreitas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@simaofreitas](https://www.ruby-forum.com/u/simaofreitas)\
**Post date:** [August 2, 2010, 9:55pm UTC](https://www.ruby-forum.com/t/tables-deleted-when-running-rake-test-units/192596/6 "2010-08-02T21:55:33Z")

</div>

For the second time: please quote when replying!

SimÃ£o Freitas wrote:

> I fixed the problem of the “disappearing tables” with the following code  
> in environment.rb:
> 
> config.active\_record.schema\_format = :sql
> 
> The problem was with some datatypes not recognized by rails (BIT(1) and  
> (YEAR(4)) I guess. All works fine now.

In most cases, if you have to use schema\_format = :sql, something is  
wrong.

> But I’ll do what you recommend and start using migrations!
> 
> Again, I can’t thank you enough! Thanks a lot!

You’re welcome!

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![simaofreitas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@simaofreitas](https://www.ruby-forum.com/u/simaofreitas)\
**Post date:** [August 2, 2010, 10:03pm UTC](https://www.ruby-forum.com/t/tables-deleted-when-running-rake-test-units/192596/7 "2010-08-02T22:03:04Z")

</div>

Marnen Laibow-Koser wrote:

> For the second time: please quote when replying!

Sorry :X

> SimÃ£o Freitas wrote:
> 
> > I fixed the problem of the “disappearing tables” with the following code  
> > in environment.rb:
> > 
> > config.active\_record.schema\_format = :sql
> > 
> > The problem was with some datatypes not recognized by rails (BIT(1) and  
> > (YEAR(4)) I guess. All works fine now.
> 
> In most cases, if you have to use schema\_format = :sql, something is  
> wrong.

Hum… I’ll see what I can figure out! Thanks again

> > But I’ll do what you recommend and start using migrations!
> > 
> > Again, I can’t thank you enough! Thanks a lot!
> 
> You’re welcome!
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid
