我应该将用于执行数据迁移的 CSV 文件放在哪里?

发布于 2024-09-17 23:43:45 字数 368 浏览 2 评论 0原文

这很可能是重复的,但我找不到任何人问这个问题。

我的理解*是,如果我想将数据从外部源迁移到我的 Rails 应用程序的数据库,我应该使用迁移来实现。从我的初步研究看来,我可以做的是使用像 FasterCSV 这样的工具来解析 CSV 文件(例如)就在迁移 (.rb) 文件本身中。

这是正确的方法吗?如果是这样,我实际上应该将 CSV 文件放在哪里 - 看来,如果迁移毕竟是可逆/可重复的,那么 CSV 数据应该保存在稳定的位置。

*如果我对如何解决这个问题完全错误,请告诉我,因为我对 RoR 还很陌生。

This may well be a duplicate, but I couldn't find anyone asking quite this question.

My understanding* is that if I want to migrate data from an outside source to my Rails app's database, I should do so using a migration. It seems from my preliminary research that what I could do is use a tool like FasterCSV to parse a CSV file (for example) right in the migration (.rb) file itself.

Is this the correct approach? And if so, where should I actually put that CSV file -- it seems that if migrations are, after all, meant to be reversible/repeatable, that CSV data ought to be kept in a stable location.

*Let me know if I am completely mistaken about how to even go about this as I am still new to RoR.

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

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

发布评论

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

评论(2

狂之美人 2024-09-24 23:43:45

你可以在没有 FasterCSV 的情况下将其写入 rake 作业,尽管我两者都使用。

Write rows to 'csvout' file.

outfile = File.open('csvout', 'wb')
CSV::Writer.generate(outfile) do |csv|
  csv << ['c1', nil, '', '"', "\r\n", 'c2']
  ...
end

outfile.close

该文件将输出 rake 文件的写入位置。对于您的情况,您可以将其放在单独的 CSV 文件夹中。我个人会将其排除在应用程序结构的其余部分之外。

You can write this to a rake job without FasterCSV, though I use both.

Write rows to 'csvout' file.

outfile = File.open('csvout', 'wb')
CSV::Writer.generate(outfile) do |csv|
  csv << ['c1', nil, '', '"', "\r\n", 'c2']
  ...
end

outfile.close

This file will output where the rake file is written. In your case, you can put it in a seperate folder for CSV's. I would personally keep it out of the rest of the app structure.

反话 2024-09-24 23:43:45

您可能需要查看 seed_fu 来管理它。它的优点是能够轻松更新数据库中已有的数据。您可以将 CSV 转换为种子文件,该文件只是 Ruby 代码(此处提供了示例代码)。

You may want to look into seed_fu to manage it. It has the benefit of being able to easily update the data already in database. You can convert the CSV into a seed file, which is just a Ruby code (example code is provided there).

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