Skip to content

Commit

Permalink
Allow same remote_resource_uuid on different sites and improve unitte…
Browse files Browse the repository at this point in the history
…sting
  • Loading branch information
giffels committed Nov 29, 2021
1 parent 5bd8a11 commit 7ebb447
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 72 deletions.
4 changes: 2 additions & 2 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
.. Created by changelog.py at 2021-11-25, command
.. Created by changelog.py at 2021-11-29, command
'/Users/giffler/.cache/pre-commit/repor6pnmwlm/py_env-default/bin/changelog docs/source/changes compile --output=docs/source/changelog.rst'
based on the format of 'https://keepachangelog.com/'
#########
CHANGELOG
#########

[Unreleased] - 2021-11-25
[Unreleased] - 2021-11-29
=========================

Changed
Expand Down
53 changes: 39 additions & 14 deletions tardis/plugins/sqliteregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
from typing import List, Dict
import asyncio
import logging
import sqlite3
Expand Down Expand Up @@ -33,25 +34,48 @@ def __init__(self):
for machine_type in getattr(configuration, site.name).MachineTypes:
self.add_machine_types(site.name, machine_type)

def add_machine_types(self, site_name: str, machine_type: str):
def add_machine_types(self, site_name: str, machine_type: str) -> None:
if self.get_machine_type(site_name, machine_type):
logger.debug(
f"{machine_type} is already present for {site_name} in database! Skipping insertion!" # noqa B950
)
return
sql_query = """
INSERT OR IGNORE INTO MachineTypes(machine_type, site_id)
INSERT OR ROLLBACK INTO MachineTypes(machine_type, site_id)
SELECT :machine_type, Sites.site_id FROM Sites
WHERE Sites.site_name = :site_name"""
self.execute(sql_query, dict(site_name=site_name, machine_type=machine_type))

def add_site(self, site_name: str):
sql_query = "INSERT OR IGNORE INTO Sites(site_name) VALUES (:site_name)"
def get_machine_type(self, site_name: str, machine_type: str) -> List[Dict]:
sql_query = """
SELECT * FROM MachineTypes MT
JOIN Sites S ON MT.site_id = S.site_id
WHERE MT.machine_type = :machine_type AND S.site_name = :site_name"""
return self.execute(
sql_query, dict(site_name=site_name, machine_type=machine_type)
)

def add_site(self, site_name: str) -> None:
if self.get_site(site_name):
logger.debug(
f"{site_name} already present in database! Skipping insertion!"
)
return
sql_query = "INSERT OR ROLLBACK INTO Sites(site_name) VALUES (:site_name)"
self.execute(sql_query, dict(site_name=site_name))

async def async_execute(self, sql_query: str, bind_parameters: dict):
def get_site(self, site_name: str) -> List[Dict]:
sql_query = "SELECT * FROM Sites WHERE site_name = :site_name"
return self.execute(sql_query, dict(site_name=site_name))

async def async_execute(self, sql_query: str, bind_parameters: dict) -> List[Dict]:
loop = asyncio.get_event_loop()
return await loop.run_in_executor(
self.thread_pool_executor, self.execute, sql_query, bind_parameters
)

@contextmanager
def connect(self):
def connect(self) -> None:
con = sqlite3.connect(
self._db_file, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES
)
Expand All @@ -61,7 +85,7 @@ def connect(self):
finally:
con.close()

def _deploy_db_schema(self):
def _deploy_db_schema(self) -> None:
tables = {
"MachineTypes": [
"machine_type_id INTEGER PRIMARY KEY AUTOINCREMENT",
Expand All @@ -72,7 +96,7 @@ def _deploy_db_schema(self):
],
"Resources": [
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"remote_resource_uuid VARCHAR(255) UNIQUE",
"remote_resource_uuid VARCHAR(255)",
"drone_uuid VARCHAR(255) UNIQUE",
"state_id INTEGER",
"site_id INTEGER",
Expand All @@ -82,6 +106,7 @@ def _deploy_db_schema(self):
"FOREIGN KEY(state_id) REFERENCES ResourceState(state_id)",
"FOREIGN KEY(site_id) REFERENCES Sites(site_id)",
"FOREIGN KEY(machine_type_id) REFERENCES MachineTypes(machine_type_id)",
"CONSTRAINT unique_remote_resource_uuid_per_site UNIQUE (site_id, remote_resource_uuid)", # noqa B950
],
"ResourceStates": [
"state_id INTEGER PRIMARY KEY AUTOINCREMENT",
Expand Down Expand Up @@ -109,13 +134,13 @@ def _deploy_db_schema(self):
(state,),
)

async def delete_resource(self, bind_parameters: dict):
async def delete_resource(self, bind_parameters: dict) -> None:
sql_query = """DELETE FROM Resources
WHERE drone_uuid = :drone_uuid
AND site_id = (SELECT site_id from Sites WHERE site_name = :site_name)"""
await self.async_execute(sql_query, bind_parameters)

def execute(self, sql_query: str, bind_parameters: dict):
def execute(self, sql_query: str, bind_parameters: dict) -> List[Dict]:
with self.connect() as connection:
connection.row_factory = lambda cur, row: {
col[0]: row[idx] for idx, col in enumerate(cur.description)
Expand All @@ -125,7 +150,7 @@ def execute(self, sql_query: str, bind_parameters: dict):
logger.debug(f"{sql_query},{bind_parameters} executed")
return cursor.fetchall()

def get_resources(self, site_name: str, machine_type: str):
def get_resources(self, site_name: str, machine_type: str) -> List[Dict]:
sql_query = """
SELECT R.remote_resource_uuid, R.drone_uuid, RS.state, R.created, R.updated
FROM Resources R
Expand All @@ -137,9 +162,9 @@ def get_resources(self, site_name: str, machine_type: str):
sql_query, dict(site_name=site_name, machine_type=machine_type)
)

async def insert_resource(self, bind_parameters: dict):
async def insert_resource(self, bind_parameters: dict) -> None:
sql_query = """
INSERT OR IGNORE INTO
INSERT OR ROLLBACK INTO
Resources(remote_resource_uuid, drone_uuid, state_id, site_id, machine_type_id,
created, updated)
SELECT :remote_resource_uuid, :drone_uuid, RS.state_id, S.site_id,
Expand All @@ -158,7 +183,7 @@ async def notify(self, state: State, resource_attributes: AttributeDict) -> None
bind_parameters.update(resource_attributes)
await self._dispatch_on_state.get(state, self.update_resource)(bind_parameters)

async def update_resource(self, bind_parameters: dict):
async def update_resource(self, bind_parameters: dict) -> None:
sql_query = """UPDATE Resources SET updated = :updated,
state_id = (SELECT state_id FROM ResourceStates WHERE state = :state)
WHERE drone_uuid = :drone_uuid
Expand Down
Loading

0 comments on commit 7ebb447

Please sign in to comment.