Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fork query does not fork tables but instead adds default table #3580

Merged
merged 3 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions redash/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,15 +631,18 @@ 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),

cls = type(self)
# Query.create will add default TABLE visualization, so use constructor to create bare copy of query
forked_query = cls(name=u'Copy of (#{}) {}'.format(self.id, self.name),
kravets-levko marked this conversation as resolved.
Show resolved Hide resolved
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