diff --git a/redash/models.py b/redash/models.py index b95f02a3..14f435b2 100644 --- a/redash/models.py +++ b/redash/models.py @@ -857,9 +857,10 @@ class Query(ChangeTrackingMixin, ModelTimestampsMixin, BaseVersionedModel, Belon forked_v['query'] = forked_query forked_v.pop('id') forked_visualizations.append(forked_v) - - with db.database.atomic(): - Visualization.insert_many(forked_visualizations).execute() + + if len(forked_visualizations) > 0: + with db.database.atomic(): + Visualization.insert_many(forked_visualizations).execute() return forked_query def pre_save(self, created): diff --git a/tests/models/test_queries.py b/tests/models/test_queries.py index 2f941c0c..085bf7db 100644 --- a/tests/models/test_queries.py +++ b/tests/models/test_queries.py @@ -12,7 +12,7 @@ class TestApiKeyGetByObject(BaseTestCase): self.assertEqual(forked_q.id, forked_v.query.id) - def test_returns_none_if_not_exists(self): + def test_fork_with_visualizations(self): # prepare original query and visualizations data_source = self.factory.create_data_source(group=self.factory.create_group()) query = self.factory.create_query(data_source=data_source, description="this is description") @@ -20,10 +20,8 @@ class TestApiKeyGetByObject(BaseTestCase): visualization_box = self.factory.create_visualization(query=query, description="box vis", type="BOXPLOT", options="{}") fork_user = self.factory.create_user() - forked_query = Query.fork(query.id, fork_user, self.factory.org) - forked_visualization_chart = None forked_visualization_box = None forked_table = None @@ -48,8 +46,29 @@ class TestApiKeyGetByObject(BaseTestCase): self.assertEqual(forked_query.user, fork_user) self.assertEqual(forked_query.description, query.description) self.assertTrue(forked_query.name.startswith('Copy')) - self.assertEqual(count_table, 1) # num of TABLE must be 1. default table only + self.assertEqual(count_table, 1) self.assertEqual(forked_table.name, "Table") self.assertEqual(forked_table.description, "") self.assertEqual(forked_table.options, "{}") +<<<<<<< HEAD +======= + + def test_fork_from_query_that_has_no_visualization(self): + # prepare original query and visualizations + data_source = self.factory.create_data_source(group=self.factory.create_group()) + query = self.factory.create_query(data_source=data_source, description="this is description") + fork_user = self.factory.create_user() + + forked_query = Query.fork(query.id, fork_user, self.factory.org) + + count_table = 0 + count_vis = 0 + for v in forked_query.visualizations: + count_vis += 1 + if v.type == "TABLE": + count_table += 1 + + self.assertEqual(count_table, 1) + self.assertEqual(count_vis, 1) +>>>>>>> fix bugs