Skip to content

Commit

Permalink
Fork query does not fork tables but instead adds default table (#3580)
Browse files Browse the repository at this point in the history
* #3572 Fork query does not fork tables but instead adds default table

* Fix code style

* CR1
  • Loading branch information
kravets-levko authored and arikfr committed Mar 13, 2019
1 parent ba62b46 commit 4a8d9a7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
11 changes: 6 additions & 5 deletions redash/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,15 +631,16 @@ def fork(self, user):
forked_list = ['org', 'data_source', 'latest_query_data', 'description',
'query_text', 'query_hash', 'options']
kwargs = {a: getattr(self, a) for a in forked_list}
forked_query = Query.create(name=u'Copy of (#{}) {}'.format(self.id, self.name),
user=user, **kwargs)

# Query.create will add default TABLE visualization, so use constructor to create bare copy of query
forked_query = Query(name=u'Copy of (#{}) {}'.format(self.id, self.name), user=user, **kwargs)

for v in self.visualizations:
if v.type == 'TABLE':
continue
forked_v = v.copy()
forked_v['query_rel'] = forked_query
forked_query.visualizations.append(Visualization(**forked_v))
fv = Visualization(**forked_v) # it will magically add it to `forked_query.visualizations`
db.session.add(fv)

db.session.add(forked_query)
return forked_query

Expand Down
11 changes: 11 additions & 0 deletions tests/models/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ def test_fork_with_visualizations(self):
group=self.factory.create_group())
query = self.factory.create_query(data_source=data_source,
description="this is description")

# create default TABLE - query factory does not create it
self.factory.create_visualization(
query_rel=query, name="Table", description='', type="TABLE", options="{}")

visualization_chart = self.factory.create_visualization(
query_rel=query, description="chart vis", type="CHART",
options="""{"yAxis": [{"type": "linear"}, {"type": "linear", "opposite": true}], "series": {"stacking": null}, "globalSeriesType": "line", "sortX": true, "seriesOptions": {"count": {"zIndex": 0, "index": 0, "type": "line", "yAxis": 0}}, "xAxis": {"labels": {"enabled": true}, "type": "datetime"}, "columnMapping": {"count": "y", "created_at": "x"}, "bottomMargin": 50, "legend": {"enabled": true}}""")
Expand All @@ -300,6 +305,7 @@ def test_fork_with_visualizations(self):
if v.type == "TABLE":
count_table += 1
forked_table = v

self.assert_visualizations(query, visualization_chart, forked_query,
forked_visualization_chart)
self.assert_visualizations(query, visualization_box, forked_query,
Expand Down Expand Up @@ -327,6 +333,11 @@ def test_fork_from_query_that_has_no_visualization(self):
group=self.factory.create_group())
query = self.factory.create_query(data_source=data_source,
description="this is description")

# create default TABLE - query factory does not create it
self.factory.create_visualization(
query_rel=query, name="Table", description='', type="TABLE", options="{}")

fork_user = self.factory.create_user()

forked_query = query.fork(fork_user)
Expand Down

0 comments on commit 4a8d9a7

Please sign in to comment.