1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-26 16:58:28 +00:00

Enable SQL foreign key support for SQLite

This commit is contained in:
grossmj 2021-05-06 17:21:45 +09:30
parent a1f3f6472a
commit 10164e262d
3 changed files with 13 additions and 3 deletions

View File

@ -17,7 +17,6 @@
import os import os
import sys import sys
import json
import uuid import uuid
import socket import socket
import shutil import shutil
@ -30,7 +29,6 @@ from .appliance_manager import ApplianceManager
from .compute import Compute, ComputeError from .compute import Compute, ComputeError
from .notification import Notification from .notification import Notification
from .symbols import Symbols from .symbols import Symbols
from ..version import __version__
from .topology import load_topology from .topology import load_topology
from .gns3vm import GNS3VM from .gns3vm import GNS3VM
from ..utils.get_resource import get_resource from ..utils.get_resource import get_resource

View File

@ -22,6 +22,8 @@ from fastapi.encoders import jsonable_encoder
from pydantic import ValidationError from pydantic import ValidationError
from typing import List from typing import List
from sqlalchemy import event
from sqlalchemy.engine import Engine
from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from gns3server.db.repositories.computes import ComputesRepository from gns3server.db.repositories.computes import ComputesRepository
@ -49,6 +51,16 @@ async def connect_to_db(app: FastAPI) -> None:
log.error(f"Error while connecting to database '{db_url}: {e}") log.error(f"Error while connecting to database '{db_url}: {e}")
@event.listens_for(Engine, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
# Enable SQL foreign key support for SQLite
# https://docs.sqlalchemy.org/en/14/dialects/sqlite.html#foreign-key-support
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA foreign_keys=ON")
cursor.close()
async def get_computes(app: FastAPI) -> List[dict]: async def get_computes(app: FastAPI) -> List[dict]:
computes = [] computes = []

View File

@ -97,7 +97,7 @@ class TestTemplateRoutes:
assert response.status_code == status.HTTP_200_OK assert response.status_code == status.HTTP_200_OK
assert response.json()["name"] == "VPCS_TEST_RENAMED" assert response.json()["name"] == "VPCS_TEST_RENAMED"
async def test_template_delete(self, app: FastAPI, client: AsyncClient, controller: Controller) -> None: async def test_template_delete(self, app: FastAPI, client: AsyncClient) -> None:
template_id = str(uuid.uuid4()) template_id = str(uuid.uuid4())
params = {"template_id": template_id, params = {"template_id": template_id,