Skip to content

Commit c70b303

Browse files
authored
Merge pull request #1956 from Andry925/feat/sqlite-storage-config-validation-and-init-guardrails
feat:sqlite storage config validation and init guardrails
2 parents 65a443b + cd5c479 commit c70b303

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

tests/unit/service/test_storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def test_sqlite_storage(self):
144144
class TestSQLiteEventStorageRotation(TestCase):
145145

146146
def setUp(self):
147-
self.directory = path.join("storage", "data")
147+
self.directory = "./data/"
148148
self.db_path = path.join(self.directory, "data.db")
149149
if not path.exists(self.directory):
150150
try:
@@ -153,7 +153,7 @@ def setUp(self):
153153
except Exception as e:
154154
LOG.warning(f"Failed to create directory {self.directory}: {e}")
155155
self.config = {
156-
"data_file_path": self.db_path,
156+
"data_file_path": self.directory,
157157
"messages_ttl_check_in_hours": 1,
158158
"messages_ttl_in_days": 7,
159159
"max_read_records_count": 1000,

thingsboard_gateway/storage/sqlite/database.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def init_table(self):
105105
self.__log.exception("Failed to create table or migrate data! Error: %s", e)
106106

107107
def run(self):
108-
self.__log.info("Database thread started %r", id(self))
108+
self.__log.debug("Database thread started %r", id(self))
109109
interval = self.settings.oversize_check_period * 60
110110
sleep_time = 0.2
111111

@@ -131,7 +131,7 @@ def run(self):
131131

132132
except Exception as e:
133133
self.__log.exception("Error in database thread: %s", exc_info=e)
134-
self.__log.info("Database thread stopped %r", id(self))
134+
self.__log.debug("Database thread stopped %r", id(self))
135135
self.db.close()
136136

137137
def process_file_limit(self):

thingsboard_gateway/storage/sqlite/sqlite_event_storage.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ def __init__(self, config, logger, main_stop_event):
3636
self.__log.info("Sqlite Storage initializing...")
3737
self.write_queue = Queue(-1)
3838
self.stopped = Event()
39-
if isinstance(config, StorageSettings):
40-
self.__settings = config
41-
else:
42-
self.__settings = StorageSettings(config)
39+
40+
self.__settings = config if isinstance(config, StorageSettings) else StorageSettings(config)
41+
if self.__settings.warnings:
42+
for warning in self.__settings.warnings:
43+
self.__log.warning("%s", str(warning))
44+
4345
self.__ensure_data_folder_exists()
4446
self.__pointer = Pointer(self.__settings.data_file_path, log=self.__log)
4547
self.__default_database_name = self.__settings.db_file_name

thingsboard_gateway/storage/sqlite/storage_settings.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,25 @@ def __init__(self, config, enable_validation=True):
2626
self.max_read_records_count = config.get("max_read_records_count", 1000)
2727
self.batch_size = config.get("writing_batch_size", 1000)
2828
self.directory_path = path.dirname(self.data_file_path)
29-
self.db_file_name = path.basename(self.data_file_path)
29+
self.db_file_name = "data.db"
3030
self.size_limit = config.get("size_limit", 1024)
3131
self.max_db_amount = config.get("max_db_amount", 10)
3232
self.oversize_check_period = config.get("oversize_check_period", 1)
33-
self.validate_settings()
33+
self.warnings = self.validate_settings()
3434

3535
def validate_settings(self):
36-
if not self.db_file_name:
37-
self.db_file_name = "data.db"
36+
warnings = []
37+
if path.basename(self.data_file_path):
38+
warnings.append(
39+
"You can not specify the file name, using default file name: data.db or make sure you set slash at the end of the path;")
40+
3841
if self.size_limit < 1 and self.enable_validation:
3942
self.size_limit = 1
43+
warnings.append("The size limit is too small - using the minimum value 1 MB;")
44+
4045
if self.oversize_check_period < 1 and self.enable_validation:
4146
self.oversize_check_period = 1
47+
warnings.append("The oversize check period is too small - using the minimum value 1 minute;")
48+
49+
return warnings
4250

0 commit comments

Comments
 (0)