guides.rubyonrails.org 读书笔记(九)
来源:互联网 发布:海军知耻 编辑:程序博客网 时间:2024/06/07 03:23
Migrations
Migrations are a convenient way for you to alter your database in a structured and organized manner.
1 Anatomy of a Migration
Migrations are not limited to changing the schema. You can also use them to fix bad data in the database or populate new fields:
def self.up
change_table :users do |t|
t.boolean :receive_newsletter, :default => false
end
User.update_all ["receive_newsletter = ?", true]
end
1.1 Migrations are Classes
Active Record provides methods that perform common data definition tasks in a database independent way.
- create_table
- change_table
- drop_table
- add_column
- change_column
- rename_column
- remove_column
- add_index
- remove_index
1.2 What’s in a Name
Migrations are stored in files in db/migrate, one for each migration class. The name of the file is of the form YYYYMMDDHHMMSS_create_products.rb, that is to say a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration.
以下的user store,充分说明了团队合作的问题。
For example Alice adds migrations 20080906120000 and 20080906123000 and Bob adds 20080906124500 and runs it. Alice finishes her changes and checks in her migrations and Bob pulls down the latest changes. Rails knows that it has not run Alice’s two migrations so rake db:migrate would run them (even though Bob’s migration with a later timestamp has been run), and similarly migrating down would not run their down methods.
Of course this is no substitution for communication within the team. For example, if Alice’s migration removed a table that Bob’s migration assumed to exist, then trouble would certainly strike.
1.3 Changing Migrations
You must rollback the migration (for example with rake db:rollback), edit your migration and then run rake db:migrate to run the corrected version.
In general editing existing migrations is not a good idea: you will be creating extra work for yourself and your co-workers and cause major headaches if the existing version of the migration has already been run on production machines.
可以减少对团队的其他成员或者说项目的其它部分造成影响。
Instead you should write a new migration that performs the changes you require. Editing a freshly generated migration that has not yet been committed to source control (or more generally which has not been propagated beyond your development machine) is relatively harmless. Just use some common sense.
2 Creating a Migration
2.1 Creating a Model
The model and scaffold generators will create migrations appropriate for adding a new model. This migration will already contain instructions for creating the relevant table. If you tell Rails what columns you want then statements for adding those will also be created.
rails generate model Product name:string description:text
2.2 Creating a Standalone Migration
If you are creating migrations for other purposes (for example to add a column to an existing table) then you can use the migration generator:
rails generate migration AddPartNumberToProducts
This will create an empty but appropriately named migration:
class AddPartNumberToProducts < ActiveRecord::Migration
def self.up
end
def self.down
end
end
You are not limited to one magically generated column:
rails generate migration AddDetailsToProducts part_number:string price:decimal
这里需要强调的是,通过这种方式只能修改model,修改对应的表,而此前通过scaffold生成的view和controller是不会改变的,需要手动修改。
3 Writing a Migration
3.1 Creating a Table
Migration method create_table will be one of your workhorses.
- guides.rubyonrails.org 读书笔记(九)
- guides.rubyonrails.org 读书笔记(二)
- guides.rubyonrails.org 读书笔记(三)
- guides.rubyonrails.org 读书笔记(四)
- guides.rubyonrails.org 读书笔记(五)
- guides.rubyonrails.org 读书笔记(六)
- guides.rubyonrails.org 读书笔记(七)
- guides.rubyonrails.org 读书笔记(八)
- guides.rubyonrails.org 读书笔记(十): Layouts and Rendering in Rails
- effectiveC++读书笔记(九)
- http://guides.ruby-china.org/
- RubyOnRails 小小引导(上)
- RubyOnRails 小小引导(上)
- C++Primer读书笔记(九)
- Deep Learning 读书笔记(九)
- Effective Java读书笔记(九)
- SQL读书笔记(九) UNION
- webpack学习-tree shaking(https://webpack.js.org/guides/tree-shaking/)
- 易忘
- linux 多线程概述
- 嵌入式编程知识
- 程序员的自我修养对relocation entry的翻译
- VPython - example - A simple ball acceleration
- guides.rubyonrails.org 读书笔记(九)
- man显示中文
- php和phpunit的安装与使用
- HDOJ的rank变回600+了
- jsp
- RO段、RW段和ZI段 转载:RO段、RW段和ZI段
- jquery之利用ajax与服务器交谈(温习ajax之获得响应)
- code ro rw zi 编译的一个ARM的程序,编译结果中的一句话
- break和continue的区别