Skip to content

Commit b66dd5f

Browse files
committed
Fix issue with remove_query_from_dashboard transaction
1 parent e665a41 commit b66dd5f

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

lib/sanbase/queries/dashboards.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ defmodule Sanbase.Dashboards do
717717
|> Ecto.Multi.run(:remove_dashboard_query_mapping, fn _repo, %{get_mapping: struct} ->
718718
Repo.delete(struct)
719719
end)
720-
|> Ecto.Multi.run(:add_preloads, fn _repo, %{add_query_to_dashboard: struct} ->
720+
|> Ecto.Multi.run(:add_preloads, fn _repo, %{remove_dashboard_query_mapping: struct} ->
721721
# Do not preload the dashboard as it will be added in the next step
722722
{:ok, Repo.preload(struct, [:query])}
723723
end)

lib/sanbase/run_examples.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,11 @@ defmodule Sanbase.RunExamples do
713713

714714
{:ok, mapping} = Sanbase.Dashboards.add_query_to_dashboard(dashboard.id, query.id, user.id)
715715

716+
# Add and remove the mapping to test the removal
717+
{:ok, mapping2} = Sanbase.Dashboards.add_query_to_dashboard(dashboard.id, query.id, user.id)
718+
719+
{:ok, _} = Sanbase.Dashboards.remove_query_from_dashboard(dashboard.id, mapping2.id, user.id)
720+
716721
{:ok, q} = Sanbase.Queries.get_dashboard_query(dashboard.id, mapping.id, user.id)
717722

718723
query_metadata = Sanbase.Queries.QueryMetadata.from_local_dev(user.id)

test/sanbase_web/graphql/queries/queries_api_test.exs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ defmodule SanbaseWeb.Graphql.QueriesApiTest do
457457

458458
describe "Run Queries" do
459459
test "run raw sql query", context do
460+
# In test env the storing runs not async and there's a 7500ms sleep
461+
Application.put_env(:__sanbase_queires__, :store_execution_details, false)
462+
on_exit(fn -> Application.delete_env(:__sanbase_queires__, :store_execution_details) end)
463+
460464
mock_fun =
461465
Sanbase.Mock.wrap_consecutives(
462466
[
@@ -498,6 +502,10 @@ defmodule SanbaseWeb.Graphql.QueriesApiTest do
498502
end
499503

500504
test "run sql query by id", context do
505+
# In test env the storing runs not async and there's a 7500ms sleep
506+
Application.put_env(:__sanbase_queires__, :store_execution_details, false)
507+
on_exit(fn -> Application.delete_env(:__sanbase_queires__, :store_execution_details) end)
508+
501509
{:ok, query} = create_query(context.user.id)
502510

503511
mock_fun =
@@ -535,7 +543,60 @@ defmodule SanbaseWeb.Graphql.QueriesApiTest do
535543
end)
536544
end
537545

546+
test "delete dashboard query", context do
547+
{:ok, query} = create_query(context.user.id)
548+
549+
{:ok, dashboard} =
550+
Sanbase.Dashboards.create_dashboard(%{name: "My Dashboard"}, context.user.id)
551+
552+
# Add a query to a dashboard
553+
mapping =
554+
execute_dashboard_query_mutation(context.conn, :create_dashboard_query, %{
555+
dashboard_id: dashboard.id,
556+
query_id: query.id,
557+
settings: %{layout: [0, 1, 2, 3, 4]}
558+
})
559+
|> get_in(["data", "createDashboardQuery"])
560+
561+
# Assert that the dashboard has exactly 1 query added
562+
assert {:ok, %{queries: [_]}} =
563+
Sanbase.Dashboards.get_dashboard(dashboard.id, context.user.id)
564+
565+
result =
566+
execute_dashboard_query_mutation(context.conn, :delete_dashboard_query, %{
567+
dashboard_id: dashboard.id,
568+
dashboard_query_mapping_id: mapping["id"]
569+
})
570+
|> get_in(["data", "deleteDashboardQuery"])
571+
572+
dashboard_query_mapping_id = mapping["id"]
573+
574+
query_id = query.id
575+
dashboard_id = dashboard.id
576+
577+
assert %{
578+
"dashboard" => %{"id" => ^dashboard_id, "parameters" => %{}},
579+
"id" => ^dashboard_query_mapping_id,
580+
"query" => %{
581+
"id" => ^query_id,
582+
"sqlQueryParameters" => %{"limit" => 10, "slug" => "bitcoin"},
583+
"sqlQueryText" =>
584+
"SELECT * FROM intraday_metrics WHERE asset_id = get_asset_id({{slug}}) LIMIT {{limit}}"
585+
},
586+
"settings" => %{"layout" => [0, 1, 2, 3, 4]}
587+
} = result
588+
589+
# Assert that the dashboard has no queries
590+
591+
assert {:ok, %{queries: []}} =
592+
Sanbase.Dashboards.get_dashboard(dashboard_id, context.user.id)
593+
end
594+
538595
test "run dashboard query (resolve global params)", context do
596+
# In test env the storing runs not async and there's a 7500ms sleep
597+
Application.put_env(:__sanbase_queires__, :store_execution_details, false)
598+
on_exit(fn -> Application.delete_env(:__sanbase_queires__, :store_execution_details) end)
599+
539600
{:ok, query} = create_query(context.user.id)
540601

541602
{:ok, dashboard} =
@@ -644,6 +705,7 @@ defmodule SanbaseWeb.Graphql.QueriesApiTest do
644705

645706
describe "Caching" do
646707
test "cache queries on a dashboard", context do
708+
# In test env the storing runs not async and there's a 7500ms sleep
647709
Application.put_env(:__sanbase_queires__, :store_execution_details, false)
648710
on_exit(fn -> Application.delete_env(:__sanbase_queires__, :store_execution_details) end)
649711

0 commit comments

Comments
 (0)