Two database migration (active record)

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

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.

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

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