Introduction to rake and database data migration under Ruby on Rails, rubyrails

Source: Internet
Author: User
Tags ruby on rails

Introduction to rake and database data migration under Ruby on Rails, rubyrails

I don't know whether you have written data to the Migration file through Migration. I believe you have done this for both laruence And Newbie. In fact, this is not impracticable, but it will introduce unnecessary troubles to you.

It is generally believed that the content in the db/migrate folder is about the evolution of your database Schema. Every new development or online environment should use the Migration to build available databases. However, if it is loaded here, the Business Code responsible for the details, such as the Migration Code of some historical data, after a period of time, the database structure has changed, but the Migration has not changed, gradually, the auxiliary code has become junk code, which not only cannot help build the environment, but also causes the execution process of rake db: migrate to be abnormally interrupted, which virtually increases the construction cost of the new environment.

Therefore, the correct method should be that Migration is only responsible for Schema-related matters, rather than asking about data details. All specific data details should be submitted to the rake task, in addition, these rake tasks are not static and will be discarded over time, but they can be deleted directly because they are irrelevant to other parts of the system. However, Rake is also well-designed for data migration. The details are as follows:

Bad Rake Task

# lib/tasks/temporary/users.rakenamespace :users do task :set_newsletter => :environment do  User.all.each do |user|   if user.confirmed?    user.receive_newsletter = true    user.save   end  end endend

The task will traverse all users and think about what if the dataset is very popular
Updating data through ActiveRecord triggers the verification and creation callback methods in the model.
Use the if Condition Statement to determine whether data needs to be updated
You cannot intuitively see what this task is doing. There is no desc, so you cannot find it through rake-T.
Good Rake Task

# lib/tasks/temporary/users.rakenamespace :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!" endend

With desc, we can clearly understand the intent of the task and it will be displayed in rake-T.
Solved the problem of if statement through scope
Counters and execution Status display are introduced to help us understand the running status.
Change the data to the transaction for execution. You can run the syntax to avoid inconsistency caused by data exceptions.
The last note is that sometimes SQL statements are simpler and more effective, especially when the dataset is large. An SQL statement can help you save a lot of unnecessary loops! In addition, remember to check the validity of the Rake task in advance before you go 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.