# Two database migration (active record)

**URL:** <https://www.ruby-forum.com/t/two-database-migration-active-record/137876>\
**Category:** Rails\
**Created:** [May 17, 2008, 12:22pm UTC](https://www.ruby-forum.com/t/two-database-migration-active-record/137876 "2008-05-17T12:22:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [May 17, 2008, 12:22pm UTC](https://www.ruby-forum.com/t/two-database-migration-active-record/137876/1 "2008-05-17T12:22:20Z")

</div>

Hello.

I want to write script, that will migrate one database (with models) to  
another database (with another models) using ActiveRecord and all of its  
benefits. It is possible? Please give me any tips.

I am moving database form old app to new one. It would be great to use  
ActiveRecord for this complex task.

Thank you.

rndrfero

---

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [May 17, 2008, 3:23pm UTC](https://www.ruby-forum.com/t/two-database-migration-active-record/137876/2 "2008-05-17T15:23:14Z")

</div>

> I want to write script, that will migrate one database (with models) to  
> another database (with another models) using ActiveRecord and all of its  
> benefits. It is possible? Please give me any tips.
> 
> I am moving database form old app to new one. It would be great to use  
> ActiveRecord for this complex task.

An important rule of programming is to always be as incremental as  
possible.  
The bigger a change you make, the higher the risks go.

If you want to deliver new features to a client, as soon as possible,  
leave  
the data in the old database, and hook up to them using ActiveRecord’s  
system to set your models’ table names, primary keys, etc.

The book /Rails Recipes/ lists these techniques under “Integrating with  
Legacy Database”.

Don’t make the mountain come to Mohammed, if Mohammed can just go to the  
mountain!

Over time, when you need new tables, or if you find occassion to  
refactor  
the existing tables, you can rename them closer to ActiveRecord’s  
notorious  
opinions.

---

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [May 21, 2008, 6:00am UTC](https://www.ruby-forum.com/t/two-database-migration-active-record/137876/3 "2008-05-21T06:00:34Z")

</div>

Hi Frantisek,

I recently did this. Here is some code, maybe you can figure it out from  
this. If not post questions.

# copying data from table named legacy\_aid\_stations to table

# named aid\_stations

def migrate\_aid\_stations  
# connect to legacy table. :legacy\_development is defined in  
# databases.yml  
LegacyAidStation.establish\_connection(:legacy\_development)

```
# get all rows from legacy table
@lAidStations = LegacyAidStation.find(:all)

# clear any rows that may be in new table
AidStation.delete_all
@aidStations = Array.new
@lAidStations.each do |l|
  aidStation = AidStation.new

  # copy fields from legacy object to new object
  aidStation.name = l.AidStationName.split(/ /).map {|word|
      word.capitalize}.join ' ' # capitalize 1st letter of each word
  aidStation.sortOrderCW = l.SortOrderCW
  aidStation.sortOrderCCW = l.SortOrderCCW

  aidStation.save
  @aidStations.push aidStation
end

```

end

---

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [May 21, 2008, 6:02am UTC](https://www.ruby-forum.com/t/two-database-migration-active-record/137876/4 "2008-05-21T06:02:24Z")

</div>

Steven L. wrote:

> Hi Frantisek,
> 
> I recently did this. Here is some code, maybe you can figure it out from  
> this. If not post questions.
> 
> # copying data from table named legacy\_aid\_stations to table
> 
> # named aid\_stations
> 
> def migrate\_aid\_stations  
> # connect to legacy table. :legacy\_development is defined in  
> # databases.yml  
> LegacyAidStation.establish\_connection(:legacy\_development)
> 
> ```
> # get all rows from legacy table
> @lAidStations = LegacyAidStation.find(:all)
> 
> # clear any rows that may be in new table
> AidStation.delete_all
> @aidStations = Array.new
> @lAidStations.each do |l|
> aidStation = AidStation.new
> 
> # copy fields from legacy object to new object
> aidStation.name = l.AidStationName.split(/ /).map {|word|
> word.capitalize}.join ' ' # capitalize 1st letter of each word
> aidStation.sortOrderCW = l.SortOrderCW
> aidStation.sortOrderCCW = l.SortOrderCCW
> 
> aidStation.save
> @aidStations.push aidStation
> end
> 
> ```
> 
> end

Here’s the definition of :legacy\_development in databases.yml:

legacy\_development:  
adapter: mysql  
database: hardrockOrig  
username: root  
password: booyah  
socket: /tmp/mysql.sock
