rake db:schema:load 与迁移 [英] rake db:schema:load vs. migrations

查看:29
本文介绍了rake db:schema:load 与迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个非常简单的问题 - 如果迁移会随着应用程序变得更加复杂而变得缓慢和繁琐,并且如果我们有更干净的 rake db:schema:load 来调用,为什么迁移存在于全部?

Very simple question here - if migrations can get slow and cumbersome as an app gets more complex and if we have the much cleaner rake db:schema:load to call instead, why do migrations exist at all?

如果上述问题的答案是迁移用于版本控制(数据库更改的逐步记录),那么随着应用程序变得更加复杂,rake db:schema:load 是取而代之的是,它们是否继续保持其主要功能?

If the answer to the above is that migrations are used for version control (a stepwise record of changes to the database), then as an app gets more complex and rake db:schema:load is used more instead, do they continue to maintain their primary function?

来自这个问题的答案:rake db:schema:load 会删除生产服务器上的数据,所以使用时要小心.

From the answers to this question: rake db:schema:load will delete data on a production server so be careful when using it.

推荐答案

迁移提供对数据库的向前和向后步进更改.在生产环境中,必须在部署期间对数据库进行增量更改:迁移提供此功能和回滚故障保护.如果您在生产服务器上运行 rake db:schema:load,您最终会删除所有生产数据.这是一个危险的习惯.

Migrations provide forward and backward step changes to the database. In a production environment, incremental changes must be made to the database during deploys: migrations provide this functionality with a rollback failsafe. If you run rake db:schema:load on a production server, you'll end up deleting all your production data. This is a dangerous habit to get into.

话虽如此,我认为偶尔崩溃"迁移是一种体面的做法.这需要删除旧的迁移,将它们替换为单个迁移(与您的 schema.rb 文件非常相似)并更新 schema_migrations 表以反映此更改.这样做时要非常小心!如果您不小心,您可以轻松删除您的生产数据.

That being said, I believe it is a decent practice to occasionally "collapse" migrations. This entails deleting old migrations, replacing them with a single migration (very similar to your schema.rb file) and updating the schema_migrations table to reflect this change. Be very careful when doing this! You can easily delete your production data if you aren't careful.

顺便提一下,我坚信您永远不应该将数据创建放在迁移文件中.seed.rb 文件可用于此目的,或自定义 rake 或部署任务.将其放入迁移文件会将您的数据库架构规范与数据规范混合在一起,并可能导致运行迁移文件时发生冲突.

As a side note, I strongly believe that you should never put data creation in the migration files. The seed.rb file can be used for this, or custom rake or deploy tasks. Putting this into migration files mixes your database schema specification with your data specification and can lead to conflicts when running migration files.

这篇关于rake db:schema:load 与迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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