查看 Rails Migration 输出的方法
有没有一种简单的方法可以查看 Rails 迁移生成的实际 SQL?
我遇到过这样的情况:更改列类型的迁移在我的本地开发计算机上有效,但在生产服务器上部分失败。
我的 postgreSQL 版本在本地和生产之间有所不同(生产中为 7 个,本地为 8 个),因此我希望通过查看本地成功迁移时生成的 SQL,我可以制定出在生产中运行的 SQL 语句来修复问题。 ..
Is there an easy way to see the actual SQL generated by a rails migration?
I have a situation where a migration to change a column type worked on my local development machine by partially failed on the production server.
My postgreSQL versions are different between local and production (7 on production, 8 on local) so I'm hoping by looking at the SQL generated on the successful migration locally I can work out a SQL statement to run on production to fix things....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看日志文件:本地 log/development.log 与服务器上的 log/development.log。
Look at the log files: log/development.log locally vs log/production.log on your server.
我做了一些挖掘,发现了另一种方法也可以实现这一点...(这种方法只提供 SQL,所以对我来说更容易阅读)
如果您将此行放入配置中,Postgresql 将记录所有执行的查询文件:(有一个示例已在配置文件的“记录内容”部分中注释掉)
然后我回滚并在本地重新运行迁移以查找我正在查找的 SQL。
此方法还为您提供了一种格式的 SQL,您可以轻松地将其粘贴到诸如 PGAdmin 的查询构建器之类的东西中并进行处理。
I did some digging and found another way this can be achieved too... (This way only gives you the SQL so it was a bit easier for me to read)
Postgresql will log all the queries executed if you put this line in your config file: (there's an example which has been commented out in the "What to log" section of the config file)
Then I rolled back and re-ran my migration locally to find the SQL I was looking for.
This method also gives you the SQL in a format where you can easily paste it into something like PGAdmin's query builder and mess around with it.
您可以在迁移的
change
、up
或down
方法顶部将记录器设置为 STDOUT。示例:或者参阅此答案,向所有 rake 任务添加 SQL 日志记录
You could set the logger to STDOUT at the top of your migration's
change
,up
, ordown
methods. Example:Or see this answer for adding SQL logging to all rake tasks