Manage MySQL database with rake in Rails

Source: Internet
Author: User
Tags mysql database

As a good programmer, in addition to writing a large number of programs, creating a large number of databases is inevitable. In the past few years, the author has created and managed a large number of MySQL databases, in the process used a variety of tools to manage to make its process simpler, such as the preferred graphical management tool phpMyAdmin, as well as the command-line based MySQL Clent, they are very useful. However, in the author's mind and body always feel that we are a programmer, rather than a database administrator, the total sense of programming and database management some leap. Why not manage the database structure in the same way as programming? Ever since you started using rails, you've finally found the answer. With the capabilities of rails, you can use the programmer's way to manage the MySQL database.

First, use Migrations Management data table

In rails, when you create a model called contact, a data table named contacts will be created. Therefore, the operation of the data table contacts can be converted to the model contact operation, which can access its properties. And a lot of beginners are accustomed to using some framework to manipulate the data table, in fact, you can use the functions provided by rails itself can operate the data table. This function is the migrations function.

Most rails developers use the basic functionality of migrations migration to create and manage databases. The Data Migration feature allows you to manage your database solution using Ruby language, and you can take advantage of some ruby-specific tools, such as rake, to update the database based on instructions provided by the Ruby script. Also, the data Migration feature has a built-in version control feature that can be rolled back and forth, as in subversion or CVS. Does it sound tempting?

Migrations is a bit like an active record (an active recording, an object that wraps a row in a database table or view, encapsulates database access, and adds domain logic to the data), and can be created by migrations, a program-managed datasheet Modify and delete tables, and the syntax is simple. More importantly, migrations provides a built controller.

In fact, when you create a new model under rails, the migration file is created automatically. For example, when you create a contact model, you can find a file named 001_create_contacts.rb in the project's DB directory, which looks like this:

Class Createcontacts < activerecord::migration
def self.up
create_table:contacts do |t|
      End
-
def self.down
drop_table:contacts
End

If you want to create a data table, you can modify the above content to read as follows:

Class Createcontacts < activerecord::migration
def self.up
create_table:contacts do |t|
         T.column:name,: string,: null => false
         T.column:email,: string
         t.column:p Hone,: string,: Limit => 10,: Null => false End-
def self.down
drop_table:contacts
End

Now you can use the migration feature to run the rake command below in the project's directory:

%>rake db:migrate

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.