一直知道rails里面用migrate来进行数据库的版本控制,还是比较浅显易懂,做做笔记。
RoR的官方Wiki上有两篇文章:
http://wiki.rubyonrails.com/rails/pages/UnderstandingMigrations
http://wiki.rubyonrails.com/rails/pages/UsingMigrations
如何使用:
1. Create a migration
ruby 代码
- ruby script/generate migration description_of_migration
命名提示:
a.Migration的命名规则和model,controller一样
b.给一个mingration取一个和已经有的model类一样的名字会导致rake db:migrate诡异地失败(??未测试)
2. Edit the code to tell it what to do.
Edit the newly created file (in db/migrate). Define the up and a down methods.
3. Run the migration with Rake
ruby 代码
Tables
create_table
一个ID字段(主健)会被自动创建,对于has_and_belongs_to_many表来说,可以用:id=>false来取消
ruby 代码
- class AddTags < ActiveRecord::Migration
- def self.up
- create_table :images_tags, :id => false do |table|
- table.column :image_id, :integer
- table.column :tag_id, :integer
- end
- create_table :tags do |table|
- table.column :name, :string
- end
- end
-
- def self.down
- drop_table :tags
- drop_table :images_tags
- end
- end
合法的字段类型包括:integer, float, datetime, date, timestamp, time, text, string, binary, and boolean. 合法的字段选项包括:limit, null (比如说 :null => false 表示 NOT NULL), 以及 default (来设定默认值).
create_table可以有很多参数(见API参考)。有一个是options,可以将用原始的sql语句来添加到表后面作限制,比如说字符编码,表的类型等,比如:
ruby 代码
- create_table(:families, :options => 'DEFAULT CHARSET=UTF8') do |t|
Columns
Migrations work with columns in create_table and also with add_column.
Options when running rake db:migrate
Migrate to a specific version
ruby 代码
- rake db:migrate VERSION=17
Migrate a specific database
ruby 代码
- rake environment RAILS_ENV=production db:migrate
分享到:
相关推荐
要运行迁移,请单击“ Run Rails Migration $ bundle exec rake db:migrate 已知的问题 没有任何 发行说明 1.0.0 最初发布的rails-auto-migrate 1.0.2 修复自动迁移 想要查询更多的信息 享受!
Spring是Rails的预加载器。Spring 能够通过保持应用运行在后台,来提升开发模式下的速度。使得不需要在执行test,rake task以及migration时每次都加载。 标签:Spring
NULL 博文链接:https://thinking80s.iteye.com/blog/424942
Migration * Isolating Seed Data * Always add DB index Lesson 5. Controller * Use before_filter Lesson 6. View * Move code into controller * Move code into helper * Replace instance ...
从官网上下载的最新的rails4.0.3开发教材。不足之处是mobi版的,需要kindle阅读器,好在这个阅读器也是免费的。
rails g rails_settings:migration rake db:migrate 用法 定义设置 class User < ActiveRecord xss=removed> { :theme => 'blue' , :view => 'monthly' , :filter => false } s . key :calendar , :defaults => { :...
Migration Builder是一个交互式的命令行工具,用于构建Rails迁移。 目标是使Migration Builder能够生成95%的常见迁移类型,而无需使用Rails迁移语法。 注意:Migration Builder目前处于开发的极端阶段,因此请相应...
$ rails generate mongoid:migration 运行迁移: $ rails db:migrate $ rails db:migrate:down VERSION= $ rails db:migrate:up VERSION= $ rails db:rollback $ rails db:rollback_to VERSION= $ rails db:...
RailsAsyncMigrations ActiveRecord::Migration扩展程序以一种简单直接的方式使您的迁移异步。动机创建该库的目的是为了帮助在技术水平上难以扩展的小型公司。 小型项目不需要异步迁移队列,大公司在遇到扩展问题时...
migrate 创建迁移时,它应继承自ActiveRecord::Migration[5.2]自定义迁移路径该Rake文件希望你在你的迁移db/migrat4 ,但如果你想在不同的位置,编辑: #... snipActiveRecord::Migrator.migrate("lib/db/migrations...
installrails generate devise user ######安装CanCan: gem 'cancan'bundle install rails generate cancan : ability rails generate model role name : string rails generate migration UsersHaveAndBelongToMa
像 PHP 中的迁移一样的 Rails 如果脚本从未在特定数据库上运行,则可以创建空的迁移 SQL 脚本并在特定数据库上运行此脚本。 迁移 sql 文件名必须包含时间戳和类似20150118140555_DatabaseStructure.sql的描述该脚本...
flyway-rails 用于 Rails 数据库迁移的薄包装器 ...Flyway::Rails::MIGRATION_DIRECTORY = 'path/to/your/migrations' 然后,您可以通过运行以下命令列出新的 rake 任务: $ rake -T | grep flyway
要将Project资源添加到您的应用,请首先生成一个迁移: rails g migration CreateProjects # It's crucial to write really solid migrations # croods will use your database schema to build your resources. ...
Jstreejs :: Rails 这个宝石是为Ruby on Rails项目的资产管道而构建的。 安装 将此行添加到您的应用程序的...$ rails g migration add_ancestry_to_categories ancestry:string:index $ rake db:migrate applicat
version_fu version_fu是一个ActveRecord版本控制插件,该插件基于Rails 2.1中引入的脏属性检查。 已对其进行更新以与Rails 3兼容。安装 gem install version_fu如果您使用的是Rails ... ActiveRecord :: Migration def
rails g migration AddDeletedAtToClients deleted_at:datetime class AddDeletedAtToClients < ActiveRecord :: Migration def change add_column :clients , :deleted_at , :datetime end e
Rails中的Migration相对来说更适合做数据库的对象集合操作,而自动化的rake则是一个较好的选择,下面来浅谈Ruby on Rails下的rake与数据库数据迁移操作,需要的朋友可以参考下
添加迁移:docker-compose run rails rails g migration xxx 运行迁移:docker-compose运行rails rake db:migrate 生产设置 git克隆 编辑.env.production docker-compose -f docker-compose.production.yaml运行...
未读 Ruby gem可管理ActiveRecord对象的已读/未读...rails g unread:migration rake db:migrate 从以前的版本升级 如果您是从旧版gem,则应阅读。 用法 class User < ActiveRecord :: Base acts_as_reader # Optio