Skip to content

SQLEditor: Improper index management leads to copied queries on move/edit #172

@gelicia

Description

@gelicia

The SQLEditor component is not properly getting index updates to the queries when queries are moved, and changing the SQL query after will cause duplicate queries/refIDs and delete the previous query.

Replication Steps:
Start with this panel https://play.grafana.org/d/feavrpzb4wu0wa/kristina-demos?orgId=1&from=now-30d&to=now&timezone=utc&var-fruit=apple&var-fuelType=Battery%20storage&var-stand=ice-king&editPanel=24

(For future reference, the panel is under a mixed scenario, where query A is a postgres query with some sort of query. Query B is a prometheus query with some sort of query. The actual contents of either query do not matter).

  1. Note the queries are as follows: Index 0, refID A, postgres. Index 1, refID B, prometheus.
  2. Move the prometheus query, index 1, refID B, to be before the postgres query.
  3. Note the queries are as follows: Index 0, refID B, prometheus. Index 1, refID A, postgres.
  4. In "code" mode for postgres, edit the SQL. You can just put a cursor in there and hit the spacebar.
  5. Note both queries are now Postgres, with the same RefID.

@Develer and I had originally investigated this as a problem for SQL Expressions, and what we found was onChangeQuery in QueryEditorRows is not getting the right index - it doesn't get the index update after the queries are moved. So for the example above, when you perform the edit to the postgres query in step 3, when this function is hit https://github.com/grafana/grafana/blob/main/public/app/features/query/components/QueryEditorRows.tsx#L50

The function is passed the index of 0, the index from step 1 before the move. It then updates that query, resulting in the prometheus query being overwritten.

I have a patch file with some helpful console logs and a fix, where it finds the index based on refID instead of trusting the index thats passed in, but since this doesn't seem to be a problem with other queries, its probably something to fix here.

0001-asfsfsdf.patch

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions