Skip to content

Commit

Permalink
Remove schema after deleting data source (#3894)
Browse files Browse the repository at this point in the history
* remove schema from redis after deleting data sources

* switch to _pause_key to property
  • Loading branch information
Omer Lachish authored and arikfr committed Jun 10, 2019
1 parent dda75cc commit ed2ac40
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
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
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)

0 comments on commit ed2ac40

Please sign in to comment.