# File into database migration

**URL:** <https://www.ruby-forum.com/t/file-into-database-migration/85552>\
**Category:** Rails\
**Created:** [February 14, 2007, 3:11pm UTC](https://www.ruby-forum.com/t/file-into-database-migration/85552 "2007-02-14T15:11:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![laurin1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/919ad9/32.png) [@laurin1](https://www.ruby-forum.com/u/laurin1)\
**Post date:** [February 14, 2007, 3:11pm UTC](https://www.ruby-forum.com/t/file-into-database-migration/85552/1 "2007-02-14T15:11:32Z")

</div>

Hi,

I am trying to figure out an approach to load in our initial data into  
our  
database.

I have written some load\_data migrations which populate a lot of the  
stuff,  
how some of the database  
items are images etc and I am trying to figure out how to approach added  
them to the database  
during a rake db:migrate

I am thinking if I store the files in a folder off the RAILS\_ROOT I  
should  
be able to run code in the migration  
script to add them to the database. Does this sound like a valid  
approach?

Thanks,

Keith

---

<div class="post-metadata">

**Author:** ![laurin1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/919ad9/32.png) [@laurin1](https://www.ruby-forum.com/u/laurin1)\
**Post date:** [February 14, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/file-into-database-migration/85552/2 "2007-02-14T16:35:50Z")

</div>

On Feb 14, 2007, at 9:10 AM, Keith D. wrote:

> I am thinking if I store the files in a folder off the RAILS\_ROOT I  
> should be able to run code in the migration  
> script to add them to the database. Does this sound like a valid  
> approach?
> 
> Thanks,
> 
> Keith

Here’s one way of building initial data into a migration. I put the  
data into a subdirectory of db/migrate called ‘initial\_data’ so it’s  
a bit more clear that it was relevant at a particular point in time.  
You could certainly get the data some other way that using YAML  
fixtures loaded by ActiveRecord, but that all depends on what format  
your data has now.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

=== db/migrate/001\_create\_borogroves.rb  
require ‘active\_record/fixtures’

class CreateBorogroves \< ActiveRecord::Migration  
class Borogrove \< ActiveRecord::Base  
belongs\_to :jabberwocky  
acts\_as\_list :scope =\> :jabberwocky  
serialize :toves  
end

def self.up  
create\_table :borogroves do |t|  
t.column :name, :string  
t.column :description, :string  
t.column :jabberwocky\_id, :integer  
t.column :position, :integer  
t.column :toves, :text  
end

```
 Borogrove.reset_column_information
 say_with_time("Load initial data from fixture") do
   directory = File.join(File.dirname( __FILE__ ), "initial_data")
   say "Fixture directory is: #{directory}", true
   Fixtures.create_fixtures(directory, Borogrove.table_name)
 end

```

end

def self.down  
drop\_table :borogroves  
end  
end

=== db/migrate/initial\_data/borogroves.yml  
\<% stanza = Jabberwocky.find\_by\_name(‘stanza’) %\>  
\<% counter = 0 %\>  
stanza\_\<%= counter += 1 %\>:  
id: \<%= counter %\>  
name: “Fred”  
description: “short”  
jabberwocky\_id: \<%= stanza.id %\>  
position: \<%= counter %\>  
troves:  
gyre: “only when brillig”  
gimble: occasionally  
slithy: certainly

# and so on…

---

<div class="post-metadata">

**Author:** ![laurin1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/919ad9/32.png) [@laurin1](https://www.ruby-forum.com/u/laurin1)\
**Post date:** [February 15, 2007, 10:52am UTC](https://www.ruby-forum.com/t/file-into-database-migration/85552/3 "2007-02-15T10:52:45Z")

</div>

Thanks for this, not exactly what I wanted to do but it set me off on  
the  
right path.

I didn’t know about yaml but once you suggested it I found this:

[http://nubyonrails.com/articles/2005/12/27/dump-or-slurp-yaml-reference-data](http://nubyonrails.com/articles/2005/12/27/dump-or-slurp-yaml-reference-data)

What I have found is that I can add binary data to the database and then  
export it out,  
and then re-import it later.

Keith
