我应该在迁移中更新数据库信息吗?

发布于 2024-09-11 11:29:15 字数 220 浏览 2 评论 0原文

我一直在从事一个 Rails 项目,在该项目中,我需要首先为数据库播种,然后定期更新数据。

不幸的问题是我似乎想多次更新同一组数据。因此我想使用相同的迁移名称...但 Rails 生成器似乎抱怨命名。

这让我想到几个问题。

我应该使用迁移来更新数据库中的数据还是有其他选择? 我如何解决命名问题,我的假设是在迁移名称之前添加的时间戳将是一个足够的区分因素?

谢谢

I've been working on a rails project, in which I need to firstly seed the database and then update the data periodically.

The unfortunate problem is that I seem to want to update the same set of data multiple times. Hence I would like to use the same migration name...but the rails generator seems to complain about the naming.

This leads me to a couple of questions.

Should I even be using migrations to update data in my db or is there an alternative?
How do I get around the naming problems, my assumption was that the timestamp that prepends itself to the migration name would be an adequate differentiating factor?

Thanks

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

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

发布评论

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

评论(1

不久前,开发人员使用迁移来播种数据。由于这种需求作为核心功能的重要性以及这种方法的不清洁性,Rails 2.3.4 引入了一个专门的 rake 任务来完成此任务。

这是现在的最佳实践。请参阅此帖子 以及此截屏了解更多信息。

对于定期数据库更新,您可以将自定义 rake 任务添加到 lib/tasks 中,根据我的经验,这效果很好。

A while back developers used the migration for seeding data. Such was the importance of this need as a core feature and the perceived uncleanliness of this approach, Rails 2.3.4 introduced a dedicated rake task for doing this.

This is now best practice. See this post and this screencast for more info.

For periodic db updating you can add a custom rake task to lib/tasks which works well in my experience.

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