gpt4 book ai didi

ruby-on-rails - 如何在 Rails 中将所有迁移文件转换为单个文件?

转载 作者:行者123 更新时间:2023-12-04 22:19:42 25 4
gpt4 key购买 nike

我一直在用 Ruby on Rails 开发一个项目。在开发过程中,我为我的项目生成了大量迁移文件。有时我从不同的表中添加和删除列。

有没有办法可以将多个文件中的所有迁移合并到一个文件中?

最佳答案

TL; 博士

您需要的不是一组合并的迁移;它是一个单一的模式文件和一个可选的seeds.rb 文件。 rails 一般 maintains the schema automagically when you run migrations ,因此您已经拥有了大部分您应该需要的东西,但种子数据可能除外,如下所述。

使用架构,而不是迁移

通常,您不应该维护大量迁移。相反,您应该定期清除迁移,并使用 schema.rb 或 schema.sql 来(重新)创建数据库。 Rails guides specifically state :

There is no need (and it is error prone) to deploy a new instance of an app by replaying the entire migration history. It is much simpler and faster to just load into the database a description of the current schema...Because schema dumps are the authoritative source for your database schema, it is strongly recommended that you check them into source control.



因此,您应该使用 bin/rails db:schema:load而不是运行迁移,或者在旧版本的 Rails 上运行相关的 Rake 任务。

数据迁移

虽然您可以使用迁移来修复或处理与最近的架构更改相关的数据,但数据迁移(如果使用的话)应该是临时工件。数据迁移几乎从来都不是幂等的,因此您不应该长期维护数据迁移。 The guide says :

Some people use migrations to add data to the database...However, Rails has a 'seeds' feature that should be used for seeding a database with initial data. It's a really simple feature: just fill up db/seeds.rb with some Ruby code, and run rake db:seed...This is generally a much cleaner way to set up the database of a blank application.



数据库种子数据应加载 bin/rails db:seed (或相关的 Rake 任务)而不是在迁移中维护数据。

关于ruby-on-rails - 如何在 Rails 中将所有迁移文件转换为单个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36116485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com