# Migrations and fixtures

**URL:** <https://www.ruby-forum.com/t/migrations-and-fixtures/66601>\
**Category:** Rails\
**Created:** [August 27, 2006, 6:20pm UTC](https://www.ruby-forum.com/t/migrations-and-fixtures/66601 "2006-08-27T18:20:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlewan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@dlewan](https://www.ruby-forum.com/u/dlewan)\
**Post date:** [August 27, 2006, 6:20pm UTC](https://www.ruby-forum.com/t/migrations-and-fixtures/66601/1 "2006-08-27T18:20:51Z")

</div>

I’d like to be able to regenerate my development database, tables and  
data, from script, so that if I ever have a bug in my development  
code, I can easily get the database back into a known state.

What is the best way to do this? Can I use fixtures to populate the  
tables? Can I share fixtures between tests and migrations?

At the moment, I’m using migrations to do this. For example:

class CreateFoodTable \< ActiveRecord::Migration  
def self.up  
create\_table “foods” do |t|  
t.column :name, :string  
t.column :grade, :string  
end

```
Food.create({
  :name => "Pasta",
  :grade => "Good"
})
Food.create({
  :name => "Fish",
  :grade => "Raw"
})

```

end

def self.down  
drop\_table “foods”  
end  
end

And then to regenerate the database, I’d run:

rake migrate VERSION=0  
rake migrate

Any tips appreciated.

Douglas

---

<div class="post-metadata">

**Author:** ![dlewan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@dlewan](https://www.ruby-forum.com/u/dlewan)\
**Post date:** [August 27, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/migrations-and-fixtures/66601/2 "2006-08-27T21:19:11Z")

</div>

Hi –

On Sun, 27 Aug 2006, Douglas L. wrote:

> I’d like to be able to regenerate my development database, tables and  
> data, from script, so that if I ever have a bug in my development  
> code, I can easily get the database back into a known state.
> 
> What is the best way to do this? Can I use fixtures to populate the  
> tables? Can I share fixtures between tests and migrations?

You can load any of the databases from your fixtures:

rake db:fixtures:load

and you can specify particular fixtures by appending  
FIXTURES=something,other and also of course choose a  
database/environment with RAILS\_ENV=desired\_environment.

David

–  
[http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
[Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
[http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.
