如何修复损坏的 Rails 数据库迁移
由于我自己的愚蠢,我设法以某种方式使我的数据库处于损坏状态,并且不知道如何修复它。
问题始于我的一个数据库迁移中的拼写错误。我正在添加一列,但拼写错误了要添加该列的表的名称。我运行了“rake db:migrate”但失败了。所以我去修正了拼写错误并再次运行“rake db:migrate”,但什么也没发生。因此,我认为也许 Rails 认为它已经完成了迁移,所以我运行了“rake db:rollback”,这似乎最终搞砸了事情,因为它删除了我试图添加列的表。
所有这一切的最终结果是我的数据库版本现在与rails认为的不同步。我尝试过使用“rake db:migrate:up”和“rake db:migrate:down”(带有版本号),但这没有帮助。即使当我尝试显式运行创建问题表的迁移时,也不会发生任何事情(如果我运行“向下”版本,它会告诉我它无法删除该表”)。我知道我可以直接访问数据库并修补问题表,但我认为最好找出这个问题是否有“正确”的解决方案。
非常感谢
As a direct result of my own stupidity I have managed to somehow get my database in a broken state and can't figure out how to fix it.
The problem started with a typo in one of my db migrations. I was adding a column and mis-spelled the name of the table that I wanted to add the column to. I ran 'rake db:migrate' and it failed. So I went and fixed the typo and ran 'rake db:migrate' again but nothing happened. So thinking that perhaps rails thought it had already done the migration, I ran 'rake db:rollback', which appeared to finally knacker things as it dropped the table I was trying to add the column to.
The end result of all this is that the version of my db is now out of sync with what rails thinks it is. I've tried using 'rake db:migrate:up' and 'rake db:migrate:down' (with version numbers) but this hasn't helped. Even when I try and explicitly run the migration that created the problem table, nothing happens (tho if I run the 'down' version it tells me it can't drop the table'). I know I could just access the db directly and patch up the problem table but I figured it would be good to find out if there is a 'proper' solution to this problem.
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会在这里使用解决方案:
Rails:在早期开发阶段改变迁移
I'd use the solution here:
Rails: Alter migrations during early development phases
rake db:reset
出于某种原因,发布答案至少需要 30 个字符:-)
rake db:reset
and for some reason there's a 30-char minimum for posting an answer :-)