A brief talk on rake and database Data Migration Operation _ruby topic in Ruby on Rails

Source: Internet
Author: User
Tags ruby on rails

I do not know if you have to transfer data to the migration file experience, I believe that both the veteran and novice have done it. In fact, doing this is not going to work, but this practice will slowly introduce you to some unnecessary problems.

It is generally thought that the contents of the Db/migrate folder are about the evolution of your database schema, and each new development or online environment will build the available databases through these migration. But if it's loaded here, responsible for the details of the business code, such as some historical legacy data migration code, and so on, when the structure of the database changes, but migration did not follow the changes, and gradually once the auxiliary code, became the garbage code, not only can not help build the environment, but also let rake The db:migrate execution process is interrupted, which virtually increases the cost of constructing a new environment.

So the right thing to do is thatmigration is only responsible for schema-related matters, not the details of the data, the details of the data, all of which are done by the rake task, and the rake tasks are not static, and they will be discarded over time, But because they do not want to control the rest of the system, you can delete them directly. However, the use of rake to do data migration is also fastidious, specifically as follows:

Bad Rake Task

# lib/tasks/temporary/users.rake
namespace:users do
 task:set_newsletter =>: Environment
  do User.all.each do |user|
   If user.confirmed?
    User.receive_newsletter = True User.save end End end

The task traverses all users and think about how the dataset would be
Updating data by ActiveRecord triggers validation and create callback methods in the model
To determine if you need to update data by using the IF condition statement
Can not intuitively see what this task is doing, not a desc, so also can not find it through RAKE-T
Good Rake Task

# lib/tasks/temporary/users.rake
namespace:users do
 desc ' Update confirmed users to receive newsletter '
 Task Set_newsletter:: Environment do
  users = user.confirmed
  puts ' going to update #{users.count} users '

  Activerecord::base.transaction do
   Users.each do |user|
    user.mark_newsletter_received!
    Print "."
   End
  End

  puts ' all done now! '
 End End


By Desc We can clearly understand the intent of the task, and it will also be shown in rake-t
Solves the problem of an if statement by scope
The introduction of counters, and the execution status display, allows us to understand when the program is running
The change of data is carried out in a transaction and can be syntactically inconsistent due to data anomalies
The last thing to add is that sometimes it can be simpler and more efficient to use SQL statements, especially if the dataset is large, a SQL can save you a lot of unnecessary loops! In addition, it is best to check the effectiveness of the rake task prior to the development environment.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.