Using Rake in Rails to manage MySQL databases as an excellent programmer, in addition to writing a large number of programs, it is inevitable to create a large number of databases. In the past few years, I have created and managed a large number of MySQL databases. in this process, I have used various tools for management, so that the process is simpler, for example, the preferred graphical management tool PHPMyAdmin and command line-based MySQL clent are all very useful. However, I always feel that we are a programmer rather than a database administrator, and there is always a gap between programming and database management. Why not manage the database structure in the same way as programming? After using Rails, I finally found the answer. With Rails, you can use programmers to manage MySQL databases.
1. use Migrations to manage data tables
In Rails, when you create a model named "contact", a data table named "contacts" is created. Therefore, the operations on the data table contacts can be converted to the model contact operation to access its attributes. Many new users are used to using some frameworks to operate data tables. In fact, you can use the functions provided by Rails to operate data tables. This is the Migrations function.
Most Rails developers use the basic functions of Migrations migration to create and manage databases. The data migration feature allows you to use the Ruby language to manage database solutions. you can take full advantage of Ruby-specific tools such as Rake to update databases based on commands provided by Ruby scripts. In addition, the data migration feature also has a built-in version control feature that can be used to roll back and forth modifications as in Subversion or CVS. Does it sound tempting?
Migrations is a bit like activity records (Active Record, an object that wraps a row in a database table or view, encapsulates database access, and adds domain logic to the data ), you can use Migrations to manage data tables in the form of programs, that is, you can create, modify, and delete tables, and the syntax is very simple. More importantly, Migrations provides a built controller.
In fact, when a new model is created under Rails, the Migration file is automatically created. For example, when creating a contact model, you can find a file named 00w.create_contacts.rb in the db Directory of the project. its content is as follows:
ClassCreateContacts defself. up
Create_table: contactsdo | t |
End
End
Defself. down
Drop_table: contacts
End
End
If you want to create a data table, you can modify the preceding content to the following:
ClassCreateContacts defself. up
Create_table: contactsdo | t |
T. column: name,: string,: null => false
T. column: email,: string
T. column: phone,: string,: limit => 10,: null => false
End
End
Defself. down
Drop_table: contacts
End
End
Now you can use the Migration function. run the following Rake command in the project directory:
%> Rakedb: migrate
Log on to the MySQL database and you can see that the contacts table has been created. What should I do if I want to cancel the previous creation? You can use the VERSINO option to roll back the preceding operations. Because the working principle of the data migration function is similar to that of the version control tool, you can roll back the database to an earlier version. The version number is determined by the number provided by the data migration script:
%> Rakedb: migrateVERSION = 0
Log on to the MySQL database again and you can see that the contacts table has been deleted. In addition, if you want to perform other more complex operations, you naturally want to create another migrations. For example, you want to create another table or modify the data type of a field. The following code may be run in the project directory to create a new migration file: