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

Remove schema after deleting data source #3894

Merged
merged 2 commits into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 14 additions & 8 deletions redash/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,41 +140,47 @@ def delete(self):
QueryResult.query.filter(QueryResult.data_source == self).delete()
res = db.session.delete(self)
db.session.commit()

redis_connection.delete(self._schema_key)

return res

def get_schema(self, refresh=False):
key = "data_source:schema:{}".format(self.id)

cache = None
if not refresh:
cache = redis_connection.get(key)
cache = redis_connection.get(self._schema_key)

if cache is None:
query_runner = self.query_runner
schema = sorted(query_runner.get_schema(get_stats=refresh), key=lambda t: t['name'])

redis_connection.set(key, json_dumps(schema))
redis_connection.set(self._schema_key, json_dumps(schema))
else:
schema = json_loads(cache)

return schema

@property
def _schema_key(self):
return "data_source:schema:{}".format(self.id)

@property
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR, but the similarity forced me.

def _pause_key(self):
return 'ds:{}:pause'.format(self.id)

@property
def paused(self):
return redis_connection.exists(self._pause_key())
return redis_connection.exists(self._pause_key)

@property
def pause_reason(self):
return redis_connection.get(self._pause_key())
return redis_connection.get(self._pause_key)

def pause(self, reason=None):
redis_connection.set(self._pause_key(), reason or '')
redis_connection.set(self._pause_key, reason or '')

def resume(self):
redis_connection.delete(self._pause_key())
redis_connection.delete(self._pause_key)

def add_group(self, group, view_only=False):
dsg = DataSourceGroup(group=group, data_source=self, view_only=view_only)
Expand Down
8 changes: 8 additions & 0 deletions tests/models/test_data_sources.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock
from mock import patch
from tests import BaseTestCase

from redash.models import DataSource, Query, QueryResult
Expand Down Expand Up @@ -96,3 +97,10 @@ def test_deletes_child_models(self):
data_source.delete()
self.assertIsNone(DataSource.query.get(data_source.id))
self.assertEqual(0, QueryResult.query.filter(QueryResult.data_source == data_source).count())

@patch('redash.redis_connection.delete')
def test_deletes_schema(self, mock_redis):
data_source = self.factory.create_data_source()
data_source.delete()

mock_redis.assert_called_with(data_source._schema_key)