version: rails4
数据迁移
Rails 提供了一种基于领域的语言用来管理数据库模型,这称为数据转移。数据转移被保存为文件,能够在任何数据库中执行,Active Record 提供了命令 rake。下文是一个创建数据表的数据转移的例子:
classCreatePublications < ActiveRecord::Migration defchange create_table :publicationsdo|t| t.string :title t.text :description t.references :publication_type t.integer :publisher_id t.string :publisher_type t.boolean :single_issue
t.timestamps end add_index :publications, :publication_type_id end end |
Rails 保持了可追踪性对于那些已经被提交至数据库的文件,以及提供了回滚的特性。为了实际地创建数据表,你要运行 rake db:migrate,为了回滚,需要运行 rake db:rollback.
注意,上文的代码是无关乎数据库的:它可以运行在 MySQL,PostgreSQL,Oracle 和其他的数据库。
original: http://guides.rubyonrails.org/active_record_basics.html#migrations