# Migration with required data

**URL:** <https://www.ruby-forum.com/t/migration-with-required-data/61766>\
**Category:** Rails\
**Created:** [May 26, 2006, 11:53pm UTC](https://www.ruby-forum.com/t/migration-with-required-data/61766 "2006-05-26T23:53:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sweatje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@sweatje](https://www.ruby-forum.com/u/sweatje)\
**Post date:** [May 26, 2006, 11:53pm UTC](https://www.ruby-forum.com/t/migration-with-required-data/61766/1 "2006-05-26T23:53:39Z")

</div>

I’m working on a bugtracker application in rails. I have a Bug model  
and I want to add a Priority to the mix. A Bug can have one priority.  
I generate a Priority model, modify my Bug model with a belongs\_to  
:priority and create my migration like so:

class CreatePriorities \< ActiveRecord::Migration  
def self.up  
create\_table :priorities do |t|  
t.column :name, :string, :limit =\> 25, :null =\> false  
end

```
add_column :bugs, :priority_id, :integer, { :null => false, :default 

```

=\> 1 }  
end

def self.down  
remove\_column :bugs, :priority\_id  
drop\_table :priorities  
end  
end

My question is how to handle getting data into the Priorities table  
during the migration. When I call add\_column, I update all Bug records  
to have a default Priority\_ID of 1, which works as long as I then insert  
the Priority values manually into the Priority table before testing the  
app, but I’d like a more seamless solution. I need all Bug records to  
have a VALID Priority and the Priorities table to be populated when the  
migration is complete.

How do others handle this situation?

---

<div class="post-metadata">

**Author:** ![sweatje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@sweatje](https://www.ruby-forum.com/u/sweatje)\
**Post date:** [May 27, 2006, 1:08am UTC](https://www.ruby-forum.com/t/migration-with-required-data/61766/2 "2006-05-27T01:08:38Z")

</div>

Migrations aren’t for populating for testing. That’s what fixtures are  
for (I assume that by testing, you mean unit tests, and not just  
running it in dev mode and looking at it). Use migrations to populate  
the priorities table (as that’s a fairly static part of your model, I  
assume), and put some example records in the bugs table using  
fixtures. You can get it to put a random number in (between 1 and  
however many priorities you have) by adding ERb into fixtures.yml.  
That way, adding more than one record is just a case of copy and  
paste.  
Hope this helps,  
-Nathan

---

<div class="post-metadata">

**Author:** ![sweatje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@sweatje](https://www.ruby-forum.com/u/sweatje)\
**Post date:** [May 27, 2006, 1:18am UTC](https://www.ruby-forum.com/t/migration-with-required-data/61766/3 "2006-05-27T01:18:34Z")

</div>

On 5/26/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> -Nathan  
> I thinks he is actually asking about manipulating data that already  
> exists.

> > ```
> > t.column :name, :string, :limit => 25, :null => false
> > 
> > ```
> > 
> > end
> 
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

---

<div class="post-metadata">

**Author:** ![sweatje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@sweatje](https://www.ruby-forum.com/u/sweatje)\
**Post date:** [May 27, 2006, 1:43am UTC](https://www.ruby-forum.com/t/migration-with-required-data/61766/4 "2006-05-27T01:43:44Z")

</div>

On 5/26/06, Jason S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My question is how to handle getting data into the Priorities table  
> during the migration. When I call add\_column, I update all Bug records  
> to have a default Priority\_ID of 1, which works as long as I then insert  
> the Priority values manually into the Priority table before testing the  
> app, but I’d like a more seamless solution. I need all Bug records to  
> have a VALID Priority and the Priorities table to be populated when the  
> migration is complete.

Well, keep in mind that a Migration is Ruby. It also runs in your  
Rails environ and has access to all your Models.

def self.up  
…  
Priority.create(:name =\> ‘Default’)  
Priority.create(:name =\> ‘Low’)  
Priority.create(:name =\> ‘Midnight Oil’)  
end

-Curtis

---

<div class="post-metadata">

**Author:** ![sweatje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@sweatje](https://www.ruby-forum.com/u/sweatje)\
**Post date:** [May 27, 2006, 4:21am UTC](https://www.ruby-forum.com/t/migration-with-required-data/61766/5 "2006-05-27T04:21:04Z")

</div>

Nathan:

I see where you’re coming from, but I’m not trying to populate the data  
for testing. My end goal was to pre-populate the priorities table with  
initial values and let the user later administer the tables and  
add/remove/change the values. So, adding the original values right in  
the migration is my best bet, using Curtis’ solution.

Curtis:

I’m a little embarrassed that I didn’t think of your simple solution!  
Sometimes I forget that all the code is just plain Ruby and that I have  
full access to my models!

Anyway, thanks for the input all. Definitely still tons to learn and  
figuring out more every day!

---

<div class="post-metadata">

**Author:** ![sweatje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@sweatje](https://www.ruby-forum.com/u/sweatje)\
**Post date:** [May 27, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/migration-with-required-data/61766/6 "2006-05-27T17:40:45Z")

</div>

Jason S. wrote:

> I’m a little embarrassed that I didn’t think of your simple solution!  
> Sometimes I forget that all the code is just plain Ruby and that I have  
> full access to my models!

Meh. No need to be embarrassed. 🙂 I forget too… In fact more  
frequently than I would like to admit. But rediscovering is part of  
what makes Ruby/Rails so damn fun! heehee… And for the record, I’m  
not opposed to dumping some “testing” data into a Migration. Not  
standard testing data to run _during_ tests, but development data… To  
me it’s silly to blow away a db (or even have the new guy migrate) and  
have to repopulate data by hand… This is Rails, it’s suppose to be  
fast. What is a better way to reset your database to a well-known state  
than:

rake migrate version=0  
rake migrate

You can always modify the data migrations later, or even utilize some  
conditional code to ensure only portions happen during production (code  
tables and other necessary data…you can leave your sesame street and  
hollywood actor data out). While you’re at it (during your  
“authentication” migration), you might as well drop your default “admin”  
user into it… When you’re creating code tables, you might as well  
populate them (and that will probably even be defaults in production  
code). You may as well have a few customers in the system (wrapped in  
conditional code so it only gets created in the development environ). I  
hear Charlie Sheen just LOVES to exist in development applications; Big  
Bird is of the same attitude. Make it easy…love the actors…give  
them a job…

-Curtis

---

<div class="post-metadata">

**Author:** ![sweatje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@sweatje](https://www.ruby-forum.com/u/sweatje)\
**Post date:** [May 27, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/migration-with-required-data/61766/7 "2006-05-27T19:34:22Z")

</div>

On May 27, 2006, at 8:39 AM, Curtis S. wrote:

> development data… To me it’s silly to blow away a db (or even  
> your sesame street and hollywood actor data out). While you’re at
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Guys-

```
If you are planning to use models in your migrations there are a few

```

things to be careful with that can be quite surprising. See this link  
for more details:

[http://toolmantim.com/article/2006/2/23/migrating\_with\_models](http://toolmantim.com/article/2006/2/23/migrating_with_models)

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![sweatje](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@sweatje](https://www.ruby-forum.com/u/sweatje)\
**Post date:** [May 27, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/migration-with-required-data/61766/8 "2006-05-27T20:19:45Z")

</div>

Thx for the link Ezra. That looks to be a good option for avoiding the  
issues that changing Models can cause.
