在Rails中手动编辑schema.rb是否安全?

编程入门 行业动态 更新时间:2024-10-12 01:32:50
本文介绍了在Rails中手动编辑schema.rb是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到了一个问题,当时我在Rails项目的两个分支上工作,每个项目都有一个迁移以添加列。当时, rake db:migrate:reset 引起问题,我仅依靠 schema.rb 正确地工作代表我的数据库状态。有一次,我遇到了一个问题,其中分支A添加的列进入了分支B的架构。由于 migrate:reset 不是一个选择,因此我手动进行编辑架构文件。我所做的更改基本上删除了分支B的schema.rb中不需要的分支A中的列。

I came across a problem where I was working on two branches on a rails project and each project has a migration to add a column. At the time, rake db:migrate:reset cause a problem and I solely relied on my schema.rb to correctly represent the state of my database. At one point, I came across a problem where a column added by branch A got into the schema of branch B. Since migrate:reset was not an option, I resorted to manually editing the schema file to. I committed this change that basically deleted the column from branch A that I did not need in branch B's schema.rb.

问题是在将分支A合并到master之后出现的。当我尝试将分支B重新设置为master时,我仍然在B中拥有该提交以删除架构文件中的列(由于它位于master中,因此现在变得很重要)。 Git对此没有看到任何冲突,并自动将其合并。在重新定型结束时,我发现我的架构与我的母版中的架构不一致。

Problem came after I have merged branch A into master. When I tried to rebase branch B to master, I still had that commit in B to delete the column (which now has become relevant because it is in master) in the schema file. Git did not see a conflict for this and auto-merged it. At the end of my rebase, I found that my schema is inconsistent with what I have in master.

我的解决方法是再次编辑架构文件并手动添加先前的删除列回到架构文件。我的问题是:这被认为是非常规的吗?有危险吗hacky?

My fix is to edit the schema file again and manually add the previously deleted column back to the schema file. My question is: Is this considered unconventional? dangerous? hacky?

现在它涉及一列,但是如果涉及多个列的删除/添加,那么(危险的)解决方案可能会导致更多问题,并且db / schema.rb不一致。

Right now it involves one column but if this involved multiple column deletions/additions the (dangerous?) solution could lead to more problems and db/schema.rb inconsistency.

推荐答案

编辑 schema.rb 文件。

根据 Rails Guide on迁移:

迁移(尽管可能如此)并不是数据库架构的权威来源。该角色属于db / schema.rb或Active Record通过检查数据库生成的SQL文件。它们不是为了进行编辑而设计的,它们仅表示数据库的当前状态。

Migrations, mighty as they may be, are not the authoritative source for your database schema. That role falls to either db/schema.rb or an SQL file which Active Record generates by examining the database. They are not designed to be edited, they just represent the current state of the database.

schema.rb 在每次运行新迁移时都会更新

schema.rb gets updated every time you run a new migration:

请注意,运行db:migrate还会调用db:schema:dump任务,该任务将更新db / schema.rb文件以匹配数据库的结构。

Note that running the db:migrate also invokes the db:schema:dump task, which will update your db/schema.rb file to match the structure of your database.

我建议您花一些时间来整理问题,并使 schema.rb 文件恢复正常,并更正最新的迁移集。

I'd recommend just spending some time to sort things out and get the schema.rb file back on track and correct up to the latest set of migrations.

更多推荐

在Rails中手动编辑schema.rb是否安全?

本文发布于:2023-10-18 04:24:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1503095.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编辑   Rails   rb   schema

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!