CSC/ECE 517 Spring 2014/ch1a 1p fy

From Expertiza_Wiki
Revision as of 04:11, 20 February 2014 by Yfan5 (talk | contribs) (→‎Using Migrations)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Data migration is the process of transferring data between storage types, formats, or computer systems. It is a key consideration for any system implementation, upgrade, or consolidation. Data migration is usually performed programmatically to achieve an automated migration, freeing up human resources from tedious tasks. Data migration occurs for a variety of reasons, including: Server or storage equipment replacements or upgrades; Website consolidation; Server maintenance; and Data center relocation.<ref>[ http://www.techopedia.com/definition/1180/data-migration/# Janssen C, Data migration, ]</ref>

Data Migration

Categories

Data is stored on various media in files or databases, and is generated and consumed by software applications which in turn support business processes. The need to transfer and convert data can be driven by multiple business requirements and the approach taken to the migration depends on those requirements. This page is mainly focus on data migration within or between databases.

Data Migration in Ruby on Rails

Using Migrations

Migrations are a convenient way to alter your database schema over time in a consistent and easy way. They use a Ruby DSL so that you don't have to write SQL by hand, allowing your schema and changes to be database independent. When creating or updating data in a migration it is often tempting to use one of your models. After all, they exist to provide easy access to the underlying data. Here is an example on how to use Migrations to migrate data in your rails application.<ref name="migration-overview">migration-overview</ref>

A developer has a User model and a Profile model with the relationship has_one, which means each user has one profile. User table has a column named phone and profile table also has a column named phone. What the developer want to do is moving all the data in the phone column in user table to the phone column in profile table and then delete the phone column in user table.

class DataMigration < ActiveRecord::Migration
 def self.up
   User.find_each do |user|
 user.profile.update_attributes(:phone => user.phone) unless user.phone.blank?
   end
   remove_column :users, :phone
 end

 def self.down
  add_column :users, :phone, :string
 end
end

Using gems

There are several gems that support data migration in ROR, including gem data_migrate

Data migrations are stored in db/data. They act like schema migrations, except they should be reserved for data migrations. For instance, if you realize you need to titleize all yours titles, this is the place to do it. Running any of the provided rake tasks also creates a data schema table to mirror the usual schema migrations table to track all the goodness.

Data migrations can be created at the same time as schema migrations, or independently. Database (db:) tasks have been added and extended to run on data migrations only, or in conjunction with the schema migration. For instance, rake db:migrate:with_data will run both schema and data migrations in the proper order.<ref name="data-migrate gem by Ajvargo">data-migrate gem by Ajvargo</ref>

See also

References

<references />