From 1c4201d6f7fefdc66b51fb2c5a33593f4ab465c0 Mon Sep 17 00:00:00 2001 From: "saivineeth.v" Date: Tue, 8 Aug 2023 00:37:06 +0530 Subject: [PATCH 1/3] Custom Database Tables --- README.md | 8 ++++++++ project/project/settings.py | 2 ++ silk/config.py | 4 +++- silk/models.py | 13 +++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 673dc524..4c668864 100644 --- a/README.md +++ b/README.md @@ -520,6 +520,14 @@ But sometimes, you might want to have your own sensitive keywords, then above co SILKY_SENSITIVE_KEYS = {'custom-password'} ``` +### Custom Database Tables + +By default, Silk is using below-mentioned table name (they are case insensitive) + +```python +SILKY_DATABASE_TABLES = {'REQUEST': 'silk_request', 'RESPONSE': 'silk_response', + 'SQLQUERY': 'silk_sqlquery', 'PROFILE': 'silk_profile'} +``` ### Clearing logged data diff --git a/project/project/settings.py b/project/project/settings.py index e3586db7..0a6c39d1 100644 --- a/project/project/settings.py +++ b/project/project/settings.py @@ -126,3 +126,5 @@ SILKY_MAX_RECORDED_REQUESTS_CHECK_PERCENT = 0 # SILKY_AUTHENTICATION = True # SILKY_AUTHORISATION = True +SILKY_DB_TABLE = {'REQUEST': 'test_silk_request', 'RESPONSE': 'silk_response', + 'SQLQUERY': 'silk_sqlquery', 'PROFILE': 'silk_profile'} diff --git a/silk/config.py b/silk/config.py index fddbf9e2..83f26db4 100644 --- a/silk/config.py +++ b/silk/config.py @@ -33,7 +33,9 @@ class SilkyConfig(metaclass=Singleton): 'SILKY_JSON_ENSURE_ASCII': True, 'SILKY_ANALYZE_QUERIES': False, 'SILKY_EXPLAIN_FLAGS': None, - 'SILKY_SENSITIVE_KEYS': {'username', 'api', 'token', 'key', 'secret', 'password', 'signature'} + 'SILKY_SENSITIVE_KEYS': {'username', 'api', 'token', 'key', 'secret', 'password', 'signature'}, + 'SILKY_DATABASE_TABLES': {'REQUEST': 'silk_request', 'RESPONSE': 'silk_response', + 'SQLQUERY': 'silk_sqlquery', 'PROFILE': 'silk_profile'} } def _setup(self): diff --git a/silk/models.py b/silk/models.py index b8d1ce17..464db41b 100644 --- a/silk/models.py +++ b/silk/models.py @@ -26,6 +26,7 @@ from silk.utils.profile_parser import parse_profile silk_storage = get_storage_class(SilkyConfig().SILKY_STORAGE_CLASS)() +silk_db_tables = SilkyConfig().SILKY_DATABASE_TABLES # Seperated out so can use in tests w/o models @@ -190,6 +191,9 @@ def save(self, *args, **kwargs): super().save(*args, **kwargs) Request.garbage_collect(force=False) + class Meta: + db_table = silk_db_tables['REQUEST'] + class Response(models.Model): id = CharField(max_length=36, default=uuid4, primary_key=True) @@ -218,6 +222,9 @@ def headers(self): def raw_body_decoded(self): return base64.b64decode(self.raw_body) + class Meta: + db_table = silk_db_tables['RESPONSE'] + # TODO rewrite docstring class SQLQueryManager(models.Manager): @@ -323,6 +330,9 @@ def delete(self, *args, **kwargs): self.request.save() super().delete(*args, **kwargs) + class Meta: + db_table = silk_db_tables['SQLQUERY'] + class BaseProfile(models.Model): name = CharField(max_length=300, blank=True, default='') @@ -364,3 +374,6 @@ def is_context_profile(self): @property def time_spent_on_sql_queries(self): return sum(x.time_taken for x in self.queries.all()) + + class Meta: + db_table = silk_db_tables['PROFILE'] From 7e1a17550305be974e7ffdd1ba26656a4002d982 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 7 Aug 2023 19:13:48 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project/project/settings.py | 2 +- silk/config.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/project/project/settings.py b/project/project/settings.py index 0a6c39d1..c72f3b62 100644 --- a/project/project/settings.py +++ b/project/project/settings.py @@ -127,4 +127,4 @@ # SILKY_AUTHENTICATION = True # SILKY_AUTHORISATION = True SILKY_DB_TABLE = {'REQUEST': 'test_silk_request', 'RESPONSE': 'silk_response', - 'SQLQUERY': 'silk_sqlquery', 'PROFILE': 'silk_profile'} + 'SQLQUERY': 'silk_sqlquery', 'PROFILE': 'silk_profile'} diff --git a/silk/config.py b/silk/config.py index 83f26db4..f1ac88d8 100644 --- a/silk/config.py +++ b/silk/config.py @@ -35,7 +35,7 @@ class SilkyConfig(metaclass=Singleton): 'SILKY_EXPLAIN_FLAGS': None, 'SILKY_SENSITIVE_KEYS': {'username', 'api', 'token', 'key', 'secret', 'password', 'signature'}, 'SILKY_DATABASE_TABLES': {'REQUEST': 'silk_request', 'RESPONSE': 'silk_response', - 'SQLQUERY': 'silk_sqlquery', 'PROFILE': 'silk_profile'} + 'SQLQUERY': 'silk_sqlquery', 'PROFILE': 'silk_profile'} } def _setup(self): From a317cd8a73a50a652a9a1eed252d6d91684e6969 Mon Sep 17 00:00:00 2001 From: saivineeth181 <30948769+saivineeth181@users.noreply.github.com> Date: Tue, 8 Aug 2023 00:50:11 +0530 Subject: [PATCH 3/3] Update settings.py --- project/project/settings.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project/project/settings.py b/project/project/settings.py index c72f3b62..e017a0ac 100644 --- a/project/project/settings.py +++ b/project/project/settings.py @@ -126,5 +126,5 @@ SILKY_MAX_RECORDED_REQUESTS_CHECK_PERCENT = 0 # SILKY_AUTHENTICATION = True # SILKY_AUTHORISATION = True -SILKY_DB_TABLE = {'REQUEST': 'test_silk_request', 'RESPONSE': 'silk_response', - 'SQLQUERY': 'silk_sqlquery', 'PROFILE': 'silk_profile'} +# SILKY_DATABASE_TABLES = {'REQUEST': 'test_silk_request', 'RESPONSE': 'silk_response', +# 'SQLQUERY': 'silk_sqlquery', 'PROFILE': 'silk_profile'}