generate longer file_ids and retry if they is a collison in the database
Build and Push Docker Container / build-and-push (push) Successful in 1m31s
Build and Push Docker Container / build-and-push (push) Successful in 1m31s
This commit is contained in:
+31
-24
@@ -1,3 +1,4 @@
|
||||
from my_modules.file_helper_functions import generate_short_id
|
||||
from my_modules.app.logger import logger
|
||||
|
||||
import asyncio, gel
|
||||
@@ -108,30 +109,36 @@ class EdgeDB:
|
||||
"expires_at": i.expires_at if i.expires_at else '',
|
||||
} for i in data]
|
||||
|
||||
async def add_file(self, file_id, file_name, file_size, note, content_type, uploaded_at, expires_at, user_id:str):
|
||||
return await self.run_query_with_reconnection(
|
||||
self.client.query,
|
||||
"""
|
||||
insert files {
|
||||
file_id := <str>$file_id,
|
||||
file_name := <str>$file_name,
|
||||
file_size := <str>$file_size,
|
||||
note := <str>$note,
|
||||
content_type := <str>$content_type,
|
||||
uploaded_at := <datetime>$uploaded_at,
|
||||
expires_at := <optional datetime>$expires_at,
|
||||
user_id := <str>$user_id
|
||||
};
|
||||
""",
|
||||
file_id=file_id,
|
||||
file_name=file_name,
|
||||
file_size=file_size,
|
||||
note=note,
|
||||
content_type=content_type,
|
||||
uploaded_at=uploaded_at,
|
||||
expires_at=expires_at,
|
||||
user_id=user_id,
|
||||
)
|
||||
async def add_file(self, file_name:str, file_size:str, note:str, content_type:str, uploaded_at, expires_at, user_id:str):
|
||||
for attempt in range(10):
|
||||
try:
|
||||
return await self.run_query_with_reconnection(
|
||||
self.client.query_single,
|
||||
"""
|
||||
insert files {
|
||||
file_id := <str>$file_id,
|
||||
file_name := <str>$file_name,
|
||||
file_size := <str>$file_size,
|
||||
note := <str>$note,
|
||||
content_type := <str>$content_type,
|
||||
uploaded_at := <datetime>$uploaded_at,
|
||||
expires_at := <optional datetime>$expires_at,
|
||||
user_id := <str>$user_id
|
||||
};
|
||||
""",
|
||||
file_id=generate_short_id(),
|
||||
file_name=file_name,
|
||||
file_size=file_size,
|
||||
note=note,
|
||||
content_type=content_type,
|
||||
uploaded_at=uploaded_at,
|
||||
expires_at=expires_at,
|
||||
user_id=user_id,
|
||||
)
|
||||
except gel.errors.ConstraintViolationError as e:
|
||||
await logger.warning(f'file_id collision on attempt {attempt+1}, regenerating…')
|
||||
continue
|
||||
raise RuntimeError("Could not allocate unique file_id after multiple retries")
|
||||
|
||||
async def update_file(self, file_id:str, file_name:str, note:str, expires_at, user_id:str):
|
||||
return await self.run_query_with_reconnection(
|
||||
|
||||
Reference in New Issue
Block a user