mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-09-27 22:30:11 +03:00
fix (templates): Add ordering to handle the duplicate cases gracefully
This commit is contained in:
parent
da3a34144b
commit
1e40fa1b70
@ -47,8 +47,16 @@ class ImagesRepository(BaseRepository):
|
|||||||
where(models.Image.filename == image_name, models.Image.path.endswith(image_path))
|
where(models.Image.filename == image_name, models.Image.path.endswith(image_path))
|
||||||
else:
|
else:
|
||||||
query = select(models.Image).where(models.Image.filename == image_name)
|
query = select(models.Image).where(models.Image.filename == image_name)
|
||||||
|
query = query.order_by(models.Image.image_id)
|
||||||
result = await self._db_session.execute(query)
|
result = await self._db_session.execute(query)
|
||||||
return result.scalars().first()
|
images = result.scalars().all()
|
||||||
|
if len(images) > 1:
|
||||||
|
log.warning(
|
||||||
|
f"Multiple DB entries found for image '{image_path}' "
|
||||||
|
f"({len(images)} rows). This indicates a data integrity issue. "
|
||||||
|
f"Using the entry with the lowest image_id ({images[0].image_id})."
|
||||||
|
)
|
||||||
|
return images[0] if images else None
|
||||||
|
|
||||||
async def get_image_by_checksum(self, checksum: str, image_dir: str = None) -> Optional[models.Image]:
|
async def get_image_by_checksum(self, checksum: str, image_dir: str = None) -> Optional[models.Image]:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
|
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
from typing import List, Union, Optional
|
from typing import List, Union, Optional
|
||||||
@ -29,6 +30,8 @@ from .base import BaseRepository
|
|||||||
import gns3server.db.models as models
|
import gns3server.db.models as models
|
||||||
from gns3server import schemas
|
from gns3server import schemas
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
TEMPLATE_TYPE_TO_MODEL = {
|
TEMPLATE_TYPE_TO_MODEL = {
|
||||||
"cloud": models.CloudTemplate,
|
"cloud": models.CloudTemplate,
|
||||||
"docker": models.DockerTemplate,
|
"docker": models.DockerTemplate,
|
||||||
@ -123,10 +126,16 @@ class TemplatesRepository(BaseRepository):
|
|||||||
where(models.Image.filename == image_name, models.Image.path.endswith(image_path))
|
where(models.Image.filename == image_name, models.Image.path.endswith(image_path))
|
||||||
else:
|
else:
|
||||||
query = select(models.Image).where(models.Image.filename == image_name)
|
query = select(models.Image).where(models.Image.filename == image_name)
|
||||||
|
query = query.order_by(models.Image.image_id)
|
||||||
result = await self._db_session.execute(query)
|
result = await self._db_session.execute(query)
|
||||||
# Use first() instead of one_or_none() to handle cases where multiple
|
images = result.scalars().all()
|
||||||
# DB rows share the same filename (e.g. image discovered in multiple paths)
|
if len(images) > 1:
|
||||||
return result.scalars().first()
|
log.warning(
|
||||||
|
f"Multiple DB entries found for image '{image_path}' "
|
||||||
|
f"({len(images)} rows). This indicates a data integrity issue. "
|
||||||
|
f"Using the entry with the lowest image_id ({images[0].image_id})."
|
||||||
|
)
|
||||||
|
return images[0] if images else None
|
||||||
|
|
||||||
async def add_image_to_template(
|
async def add_image_to_template(
|
||||||
self,
|
self,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user