Merge pull request #2774 from cristian-ciobanu/3.1

fix(templates): Database error detected when saving a template with a disk image change
This commit is contained in:
Jeremy Grossmann 2026-06-06 16:40:52 +02:00 committed by GitHub
commit 8e50d3f31b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 2 deletions

View File

@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import logging
from uuid import UUID
from typing import List, Union, Optional
@ -29,6 +30,8 @@ from .base import BaseRepository
import gns3server.db.models as models
from gns3server import schemas
log = logging.getLogger(__name__)
TEMPLATE_TYPE_TO_MODEL = {
"cloud": models.CloudTemplate,
"docker": models.DockerTemplate,
@ -123,8 +126,16 @@ class TemplatesRepository(BaseRepository):
where(models.Image.filename == image_name, models.Image.path.endswith(image_path))
else:
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)
return result.scalars().one_or_none()
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 add_image_to_template(
self,

View File

@ -267,9 +267,13 @@ class TemplatesService:
raise ControllerNotFoundError(f"Template '{template_id}' not found")
return template
async def _remove_image(self, template_id: UUID, image_path:str) -> None:
async def _remove_image(self, template_id: UUID, image_path: str) -> None:
if not image_path:
return
image = await self._templates_repo.get_image(image_path)
if image is None:
return
await self._templates_repo.remove_image_from_template(template_id, image)
async def update_template(self, template_id: UUID, template_update: schemas.TemplateUpdate) -> dict: