diff --git a/redash/models/__init__.py b/redash/models/__init__.py index eeb8746691..dd3c21c655 100644 --- a/redash/models/__init__.py +++ b/redash/models/__init__.py @@ -214,7 +214,8 @@ def get_schema(self, refresh=False): logging.exception("Error sorting schema columns for data_source {}".format(self.id)) out_schema = schema finally: - redis_connection.set(self._schema_key, json_dumps(out_schema)) + ttl = int(datetime.timedelta(minutes=settings.SCHEMAS_REFRESH_SCHEDULE, days=7).total_seconds()) + redis_connection.set(self._schema_key, json_dumps(out_schema), ex=ttl) return out_schema diff --git a/tests/models/test_data_sources.py b/tests/models/test_data_sources.py index dc53444cdd..7db4d463a3 100644 --- a/tests/models/test_data_sources.py +++ b/tests/models/test_data_sources.py @@ -86,6 +86,17 @@ def test_model_uses_schema_sorter(self): self.assertEqual(out_schema, sorted_schema) + @patch("redash.redis_connection.set") + def test_expires_schema(self, mock_redis): + # default of 30min + 7 days + expected_ttl = 606600 + + with mock.patch("redash.query_runner.pg.PostgreSQL.get_schema") as patched_get_schema: + patched_get_schema.return_value = None + self.factory.data_source.get_schema(refresh=True) + + mock_redis.assert_called_with("data_source:schema:1", "null", ex=expected_ttl) + class TestDataSourceCreate(BaseTestCase): def test_adds_data_source_to_default_group(self):