如何将所有迁移文件转换为 Rails 中的单个文件?
How can I convert all migration files into a single file in Rails?
我一直在 Rails Ruby 开发一个项目。在开发过程中,我为我的项目生成了大量的迁移文件。有时我在不同的表中添加和删除列。
有没有一种方法可以将多个文件的所有迁移合并到一个文件中?
您必须手动进行合并。
但是如果您只想要一个文件,那么db/schema.rb
。它包含当前数据库模式的快照。如果需要,您可以直接将其加载到数据库中。
TL;DR
您需要的不是一组统一的迁移;它是一个单一的模式文件和一个可选的 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
而不是 运行ning 迁移,或者 运行 旧版本 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 任务)加载,而不是在迁移中维护数据。
有一个 gem 声称完全符合您在问题中描述的内容 - 查看 Squasher。
来自自述文件:
"Squasher compresses old migrations in a Rails application. ... Squasher removes all the migrations and creates a single migration with the final database state of the specified date (the new migration will look like a schema)."
我一直在 Rails Ruby 开发一个项目。在开发过程中,我为我的项目生成了大量的迁移文件。有时我在不同的表中添加和删除列。
有没有一种方法可以将多个文件的所有迁移合并到一个文件中?
您必须手动进行合并。
但是如果您只想要一个文件,那么db/schema.rb
。它包含当前数据库模式的快照。如果需要,您可以直接将其加载到数据库中。
TL;DR
您需要的不是一组统一的迁移;它是一个单一的模式文件和一个可选的 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
而不是 运行ning 迁移,或者 运行 旧版本 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 任务)加载,而不是在迁移中维护数据。
有一个 gem 声称完全符合您在问题中描述的内容 - 查看 Squasher。
来自自述文件: "Squasher compresses old migrations in a Rails application. ... Squasher removes all the migrations and creates a single migration with the final database state of the specified date (the new migration will look like a schema)."