查看 Rails Migration 输出的方法

发布于 2024-08-28 23:34:18 字数 203 浏览 2 评论 0原文

有没有一种简单的方法可以查看 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 技术交流群。

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

发布评论

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

评论(3

太阳哥哥 2024-09-04 23:34:18

查看日志文件:本地 log/development.log 与服务器上的 log/development.log。

Look at the log files: log/development.log locally vs log/production.log on your server.

善良天后 2024-09-04 23:34:18

我做了一些挖掘,发现了另一种方法也可以实现这一点...(这种方法只提供 SQL,所以对我来说更容易阅读)

如果您将此行放入配置中,Postgresql 将记录所有执行的查询文件:(有一个示例已在配置文件的“记录内容”部分中注释掉)

log_statement = 'all'

然后我回滚并在本地重新运行迁移以查找我正在查找的 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)

log_statement = 'all'

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.

浅紫色的梦幻 2024-09-04 23:34:18

您可以在迁移的 changeupdown 方法顶部将记录器设置为 STDOUT。示例:

class SomMigration < ActiveRecord::Migration
  def change
    ActiveRecord::Base.logger = Logger.new(STDOUT)

    # ...
  end
end

或者参阅此答案,向所有 rake 任务添加 SQL 日志记录

You could set the logger to STDOUT at the top of your migration's change, up, or down methods. Example:

class SomMigration < ActiveRecord::Migration
  def change
    ActiveRecord::Base.logger = Logger.new(STDOUT)

    # ...
  end
end

Or see this answer for adding SQL logging to all rake tasks

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