Ruby-on-rails rake db:schema:load 与迁移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5905287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:05:19  来源:igfitidea点击:

rake db:schema:load vs. migrations

ruby-on-railsruby-on-rails-3migration

提问by sscirrus

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:loadto 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:loadis used more instead, do they continue to maintain their primary function?

如果上面的答案是迁移用于版本控制(对数据库更改的逐步记录),那么随着应用程序变得越来越复杂并被更多地rake db:schema:load使用,它们是否会继续保持其主要功能?



Caution:

警告:

From the answers to this question: rake db:schema:loadwill delete dataon a production server so be careful when using it.

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

回答by jesse reiss

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:loadon a production server, you'll end up deleting all your production data. This is a dangerous habit to get into.

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

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.rbfile) and updating the schema_migrationstable to reflect this change. Be very careful when doing this! You can easily delete your production data if you aren't careful.

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

As a side note, I strongly believe that you should never put data creation in the migration files. The seed.rbfile 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.

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

回答by ereslibre

Just stumbled across this post, that was long ago and didn't see the answer I was expecting.

刚刚偶然发现这篇文章,那是很久以前的事了,没有看到我期待的答案。

rake db:schema:loadis great for the first time you put a system in production. After that you should run migrations normally.

rake db:schema:load非常适合您第一次将系统投入生产。之后,您应该正常运行迁移。

This also helps you cleaning your migrations whenever you like, since the schema has all the information to put other machines in production even when you cleaned up your migrations.

这也有助于您随时清理迁移,因为即使在清理迁移时,架构也拥有将其他机器投入生产的所有信息。

回答by Shaunak

Migrations lets you add data to the db too. but db:schema:load only loads the schema .

迁移也允许您将数据添加到数据库中。但是 db:schema:load 只加载 schema 。

回答by Jamie Penney

Because migrations can be rolled back, and provide additional functionality. For example, if you need to modify some data as part of a schema change then you'll need to do that as a migration.

因为迁移可以回滚,并提供额外的功能。例如,如果您需要修改某些数据作为架构更改的一部分,那么您需要将其作为迁移进行。

回答by Dan James

As a user of other ORM's, it always seemed strange to me that Rails didn't have a 'sync and update' feature. ie, by using the schema file (which represents the entire, up-to-date schema), go through the existing DB structure and add/remove tables, columns, indexes as required.

作为其他 ORM 的用户,Rails 没有“同步和更新”功能对我来说总是很奇怪。即,通过使用架构文件(代表整个最新架构),浏览现有的数据库结构并根据需要添加/删除表、列、索引。

To me this would be a lot more robust, even if possibly a little slower.

对我来说,这会更加健壮,即使可能会慢一点。

回答by user1251840

I have already posted as a comment, but feels it is better to put the comments of the db/schema.rb file here:

我已经作为评论发布了,但我觉得把 db/schema.rb 文件的评论放在这里会更好:

# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

Actually, my experience is that it is better to put the migration files in git and not the schema.rb file...

实际上,我的经验是最好将迁移文件放在 git 中而不是 schema.rb 文件中...

回答by Nesha Zoric

rake db:migratesetup the tables in the database. When you run the migration command, it will look in db/migrate/ for any ruby files and execute them starting with the oldest. There is a timestamp at the beginning of each migration filename.

rake db:migrate设置数据库中的表。当您运行迁移命令时,它将在 db/migrate/ 中查找任何 ruby​​ 文件并从最旧的开始执行它们。每个迁移文件名的开头都有一个时间戳。

Unlike rake db:migratethat runs migrations that have not run yet, rake db:schema:loadloads the schema that is already generated in db/schema.rbinto the database.

rake db:migrate运行尚未运行的迁移不同,将rake db:schema:load已生成的模式加载db/schema.rb到数据库中。

You can find out more about rake database commands here.

您可以在此处找到有关rake 数据库命令的更多信息。