Skip to content

Commit

Permalink
Expire schemas after 7 days (getredash#5112)
Browse files Browse the repository at this point in the history
* expire schemas after 7 days

* expire schemas 7 days after the last refresh_schemas scheduled time

* format files

* add expire schema test

* patch schema away

* return nothing on schema get

* fix redis key name

* finally run tests locally and fix tests

---------

Co-authored-by: Omer Lachish <[email protected]>
Co-authored-by: Justin Clift <[email protected]>
Co-authored-by: konnectr <[email protected]>
Co-authored-by: Konstantin Smirnov <[email protected]>
Co-authored-by: Guido Petri <[email protected]>
  • Loading branch information
6 people authored and harveyrendell committed Jan 8, 2025
1 parent a360373 commit 423659a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion redash/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions tests/models/test_data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 423659a

Please sign in to comment.