Rails-从模型生成迁移脚本 [英] Rails - Generating migration script from model

查看:83
本文介绍了Rails-从模型生成迁移脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Rails,并且遇到了Migrations.似乎每次我想编辑模型时,都需要添加一个迁移脚本,即使我尚未投入生产.

I am learning rails, and I came across Migrations. It seems that everytime I want to edit a model, I would need to add a migration script, even though I am not yet in production.

您可以编辑模型,向模型中添加所需的所有属性,并在发布模型之前自动生成迁移脚本吗?

Can you edit your model, add all your attributes you need to it, and before releasing it, have the migration script autogenerated?

谢谢!

推荐答案

我同意到目前为止的评论.迁移的想法是,当您要添加新字段时,可以轻松地灵活地调整数据模式以适合您的应用程序.这是一个简单而美观的系统.

I agree with the comments so far. The idea of migrations is to make it simple to fluidly adapt your data schema to fit your application as you want to add new fields. It's a simple and beautiful system.

是的,您可以(并且应该)使用rails generate migration...,因为它不仅在许多常见情况下会生成正确的代码,而且还可以跟踪哪些迁移已在数据库的不同版本中运行.请参见 http://guides.rubyonrails.org/migrations.html#creating-a-迁移

So yes, you can (and should) use rails generate migration... as not only does this generate the proper code in many common cases, it also keeps track of which migrations have been run in different versions of the database. See http://guides.rubyonrails.org/migrations.html#creating-a-migration

常见的工作流程可能是这样的:

A common workflow might be something like this:

  • 创建一个新模型,例如,用户具有first_name,last_name,user_name之类的字段
  • 这将创建一个关联的迁移,您可以使用bundle exec rake db:migrate运行它-数据库架构将被更新
  • 您确定需要其他信息,例如生日,因此请运行rails generate migration AddBirthdateToUser birthdate:date.对于一些简单的操作(例如添加列,索引等),将生成完整的迁移代码.在其他情况下,您需要编写迁移.完成后,运行迁移.
  • 如果发现开发中存在问题,例如字段类型应该为浮点数,而不是整数,或者忘记添加索引,则可以回滚迁移(bundle exec rake db:rollback),修复迁移并重新运行它.
  • 运行测试(将运行迁移),然后在本地全部运行时,检入文件(包括迁移)并部署到具有自己的数据库副本的QA或登台服务器.
  • 在登台服务器上运行rake db:migrate.如果您在团队中,并且其他开发人员已签入迁移,那么他们也将运行.现在,您的代码和数据架构已同步.
  • 重复:-)
  • create a new model, for example User with fields like first_name, last_name, user_name
  • this will create an associated migration, which you can run using bundle exec rake db:migrate -- your database schema will be updated
  • you decide you want additional information, such as birthdate, so run rails generate migration AddBirthdateToUser birthdate:date. For some simple operations like adding a column, index, etc., the full migration code will be generated; in other cases you'll need to write the migration. When done, run the migration.
  • If you find a problem in development, for example a field type should be float, not integer, or you forgot to add an index, you can roll back the migration (bundle exec rake db:rollback), fix the migration and re-run it.
  • run your tests (which will run the migrations), and when it all works for you locally, check in the files (including the migrations) and deploy to a QA or staging server, which has its own copy of the database.
  • run rake db:migrate on the staging server. If you're on a team and other developers have checked in migrations, their will run, too. Now your code and data schema are in sync.
  • repeat :-)

在生产部署期间运行迁移不会有任何危害(我谨在上面的评论表示不同意)-您应该接受这样的想法,即更改,即使是这样的更改(在其他环境中也很难实现)是正常的部分日常的Rails生活!

There's no harm whatsoever running migrations during a production deployment (I respectfully disagree with a comment above) -- you should embrace the idea that change, even changes like this (which can be incredibly difficult in other environments) are a normal part of everyday Rails life!

这篇关于Rails-从模型生成迁移脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆