From 6828d33a025d319d1cd6b954fb94e3f8261cce36 Mon Sep 17 00:00:00 2001 From: Omer Lachish Date: Mon, 10 Jun 2019 20:48:24 +0300 Subject: [PATCH 1/2] remove schema from redis after deleting data sources --- redash/models/__init__.py | 13 +++++++++---- tests/models/test_data_sources.py | 8 ++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/redash/models/__init__.py b/redash/models/__init__.py index 2c81d25284..f2db1ece13 100644 --- a/redash/models/__init__.py +++ b/redash/models/__init__.py @@ -140,25 +140,30 @@ 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) + def _pause_key(self): return 'ds:{}:pause'.format(self.id) diff --git a/tests/models/test_data_sources.py b/tests/models/test_data_sources.py index 037ff77a05..37d4af663b 100644 --- a/tests/models/test_data_sources.py +++ b/tests/models/test_data_sources.py @@ -1,4 +1,5 @@ import mock +from mock import patch from tests import BaseTestCase from redash.models import DataSource, Query, QueryResult @@ -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) From ffc180c1f3385ea994012816d672f95ebc97632b Mon Sep 17 00:00:00 2001 From: Omer Lachish Date: Mon, 10 Jun 2019 20:50:11 +0300 Subject: [PATCH 2/2] switch to _pause_key to property --- redash/models/__init__.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/redash/models/__init__.py b/redash/models/__init__.py index f2db1ece13..dcfc4b5bfd 100644 --- a/redash/models/__init__.py +++ b/redash/models/__init__.py @@ -164,22 +164,23 @@ def get_schema(self, refresh=False): 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)