简体   繁体   中英

Convert Rails SQL output to valid psql statements

Active Job creates the following SQL on placing a job in its queue.

INSERT INTO "delayed_jobs" ("queue", "handler", "run_at", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["queue", "mailers"], ["handler", "--- !ruby/object:ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper\njob_data:\n  job_class: ActionMailer::DeliveryJob\n  job_id: 189ce295-1da8-47f5-9933-1b5657554731\n  queue_name: mailers\n  arguments:\n  - UserMailer\n  - register_success\n  - deliver_now\n  - _aj_globalid: gid://hub/User/1\n  - _aj_globalid: gid://hub/Order/294\n  - token: 561e6309deb2b9.72817606\n    number: 123\n    expiration: '2025-10-14'\n"], ["run_at", "2015-10-14 14:13:30.140078"], ["created_at", "2015-10-14 14:13:30.140408"], ["updated_at", "2015-10-14 14:13:30.140408"]]

How would I modify this to work as a raw SQL query? I thought that the rails output was supposed to be valid sql, but I get an error

ERROR: syntax error at or near "[" LINE 1: ...at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["queue", ...

it is using placeholders. so you would need to replace the $x with the values so

INSERT INTO "delayed_jobs" ("queue", "handler", "run_at", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["queue", "mailers"], ["handler", "--- !ruby/object:ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper\njob_data:\n  job_class: ActionMailer::DeliveryJob\n  job_id: 189ce295-1da8-47f5-9933-1b5657554731\n  queue_name: mailers\n  arguments:\n  - UserMailer\n  - register_success\n  - deliver_now\n  - _aj_globalid: gid://hub/User/1\n  - _aj_globalid: gid://hub/Order/294\n  - token: 561e6309deb2b9.72817606\n    number: 123\n    expiration: '2025-10-14'\n"], ["run_at", "2015-10-14 14:13:30.140078"], ["created_at", "2015-10-14 14:13:30.140408"], ["updated_at", "2015-10-14 14:13:30.140408"]]

would be come

INSERT INTO "delayed_jobs" ("queue", "handler", "run_at", "created_at", "updated_at")
 VALUES (
 'mailers',
 '--- !ruby/object:ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper\njob_data:\n  job_class: ActionMailer::DeliveryJob\n  job_id: 189ce295-1da8-47f5-9933-1b5657554731\n  queue_name: mailers\n  arguments:\n  - UserMailer\n  - register_success\n  - deliver_now\n  - _aj_globalid: gid://hub/User/1\n  - _aj_globalid: gid://hub/Order/294\n  - token: 561e6309deb2b9.72817606\n    number: 123\n    expiration: '2025-10-14'\n',
 '2015-10-14 14:13:30.140078', 
 '2015-10-14 14:13:30.140408',
 '2015-10-14 14:13:30.140408'
) RETURNING "id"

如果您有权访问生成该数据库调用的ActiveRecord对象,则可以使用to_sql方法获取SQL查询。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM