gda
gda copied to clipboard
The parser cannot parse update query correctly with `order by`
Sometimes, we call update_all on an object of ActiveRelation (which also orders the record by id), but the parser cannot get the correct information from the sql if it includes order by in the query. E.g
User.where("created_at < ?", Time.now.utc + 1.day).order(:id).update_all(created_at: Time.now.utc).
How to reproduce:
[7] pry(main)> parser = GDA::SQL::Parser.new
=> #<GDA::SQL::Parser:0x0000000124ad5168>
[8] pry(main)> sql = "update users set users.created_at = '2023-01-09 20:19:21.793637' where users.is_not_deleted = true and (created_at < '2023-01-10 20:19:21.723448') order by users.id asc"
=> "update users set users.created_at = '2023-01-09 20:19:21.793637' where users.is_not_deleted = true and (created_at < '2023-01-10 20:19:21.723448') order by users.id asc"
[9] pry(main)> parser.parse(sql).ast
=> #<GDA::Nodes::Unknown:0x0000000124afc060>
This returns Update node type
If I remove order by users.id asc, it works:
[11] pry(main)> sql = "update users set users.created_at = '2023-01-09 20:19:21.793637' where users.is_not_deleted = true and (created_at < '2023-01-10 20:19:21.723448')"
=> "update users set users.created_at = '2023-01-09 20:19:21.793637' where users.is_not_deleted = true and (created_at < '2023-01-10 20:19:21.723448')"
[12] pry(main)> parser.parse(sql).ast
=> #<GDA::Nodes::Update:0x0000000124ee5758>
I don't think order by is supported for update statements on SQLite3. What database are you using, and does this statement work on that database? That said, I'm not sure what order by is supposed to do on an update statement.