如何在不先删除数据库的情况下将 Rails 数据库迁移到第一个修订版?

发布于 2024-07-29 15:41:44 字数 603 浏览 1 评论 0原文

我为 Rails 安装设置了一个数据库,并设置了一些迁移。 我希望能够将我的数据库重置为没有表/约束/等,但在不知道迁移次数或第一次迁移的时间戳的情况下找不到合理的方法来做到这一点。 以下是我看到的选项:

  • rake db:migrate:reset
  • rake db:migrate:down VERSION=20090701154839 其中 20090701154839 是与第一次迁移相关的时间戳
  • rake db:rollback STEP=15 其中已经进行了 15 次迁移

db:migrate:reset 的问题是它首先删除数据库(它确实 db:drop< /code>、db:create,然后 db:migrate)。

db:migrate:down 的问题是我不想对开头的版本进行编码。

db:rollback 的问题是我不知道它回到开头的步骤数。

我有什么选择?

I have a database set up for my Rails installation and some migrations set up. I would like to be able to reset my database back down to having no tables/constraints/etc., but can't find a reasonable way to do this without knowing the number of migrations or the timestamp of the first migration. Here are my options as I see them:

  • rake db:migrate:reset
  • rake db:migrate:down VERSION=20090701154839 where 20090701154839 is the timestamp associated with the first migration
  • rake db:rollback STEP=15 where there have been 15 migrations

The problem with db:migrate:reset is that it drops the database first (it does db:drop, db:create, then db:migrate).

The problem with db:migrate:down is that I don't want to encode the VERSION of the beginning.

The problem with db:rollback is that I don't know the number of steps it is back to the beginning.

What are my options?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愿得七秒忆 2024-08-05 15:42:46

它有点黑客,但您可以执行查询来查找 schema_migrations 中的第一个版本,然后调用 rake db:migrate:down 来获取该版本(我假设您想为您的应用程序打包一个“重置”脚本)。

当然,这需要所有迁移的 down 方法都能正常工作。

It's moderately hackish, but you could do a query to find the first VERSION in schema_migrations and then call rake db:migrate:down to get that VERSION (I'm assuming you want to package up a "reset" script for your app).

That will, of course, require that the down methods on all your migrations work properly.

时光瘦了 2024-08-05 15:42:35

除了上面 jdl 的(正确的)解决方案之外,实现此目的的另一种 hack-y 方法是执行 rake db:rollback STEP=1000000 ,其中 1000000 是一个很大的数字,大于您将拥有的迁移数量。 它将回滚最多 1000000 次,或者您在 STEP 中输入的任意次数。

In addition to jdl's (correct) solution above, another hack-y way to acheive this is to do rake db:rollback STEP=1000000, where 1000000 is a large number, larger than the number of migrations you'll ever have. It will rollback as far as it can up to 1000000 times, or however many times you put as the STEP.

來不及說愛妳 2024-08-05 15:42:24
rake db:migrate VERSION=0

即使您使用的是较新的带时间戳的迁移文件,它也可以工作。

更新:我刚刚在 Rails 3.2.1 上测试了这个,它仍然有效。 它运行 schema_migrations 已知的所有迁移的“向下”部分。 我不知道它是否适用于 3.1,但下面的评论表明该功能在那段时间被破坏了。

rake db:migrate VERSION=0

It works even if you're using the newer timestamped migration files.

Update: I just tested this on Rails 3.2.1, and it still works. It runs the "down" part of all the migrations known to schema_migrations. I have no idea if it worked on 3.1 or not, but the comment below indicates that this feature was broken during that time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文