是否可以在 Rails 迁移中使用外部 SQL 文件?
我必须创建一个 Rails 迁移,它会创建许多触发器和存储过程。
通常,人们会使用 execute
方法来执行此操作,但由于语句的大小,我宁愿将它们保存在外部文件中并从迁移中引用它。
我怎样才能做到这一点? 有可能吗?
I have to create a Rails migration which creates many triggers and stored procedures.
Normally one would do that using the execute
method, but because of the size of the statements, I'd rather keep them in an external file and reference it from the migration.
How can I do that? Is it even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将它们存储在文本文件中并通过 File 对象读取它们。
它很丑,但是有效。 我强烈建议将存储过程/触发器保留在迁移中,以便于回滚。
如果您使用“外部文件”方法,则每次迁移都需要维护两个额外的文件,一个用于添加所有内容,另一个用于在出现以下情况时放入:
You can just store them in a text file and read them in via a File object.
It's ugly, but works. I'd strongly recommend keeping the stored procedures/triggers inside migrations for easy rollbacks.
If you do the "external file" method, you'll need to maintain two additional files per migration, one for adding all the stuff, and one for dropping in in case of a:
我在需要的地方做了以下操作:
I did the following where we needed:
如果文件中只有一条语句,Mike 的答案毫无问题,但如果有更多语句(例如多次插入和更新),ActiveRecord 将失败,因为默认情况下它不支持一次调用多个语句。
一种解决方案是修改 ActiveRecord 以支持多个语句,如此处所述。
另一种解决方案是确保您的 SQL 文件每行仅包含一个语句,并使用类似的循环
Mike’s answer works without problems if you have only one statement in the file, but if there is more statements (multiple inserts and updates, for example), ActiveRecord will fail as it doesn’t support multiple statements with one call by default.
One solution would be to modify ActiveRecord to support multiple statements, as instructed here.
Other solution would be to ensure that your SQL file contains only one statement per row and use a loop like