Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions aptracker/database/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import logging
import datetime as dt

import sqlalchemy as sa

from sqlalchemy.ext.asyncio import (
AsyncSession,
async_sessionmaker,
Expand Down Expand Up @@ -88,6 +90,24 @@ async def disconnect(self) -> None:
disconnected. To reconnect, call :meth:`connect` again.
"""

# ! once .disconnect() is invoked always flag decomissioned
async with self._session_factory() as db_session:
_current = (
await db_session.execute(
sa.select(SessionRecord).where(
SessionRecord.session_id == self.session_id
)
)
).scalar_one_or_none()

if _current:
_current.decommissioned_on = dt.datetime.now(
tz = dt.timezone.utc
)
_current.decommissioned_by = self.session.CREATED_BY

await db_session.commit()

await self.engine.dispose()
self._status = False

Expand All @@ -110,14 +130,30 @@ async def create(self, job_name : str) -> str:
now = dt.datetime.now(tz = dt.timezone.utc)
retvalue = f"ID: {self.job_id} Job Name: {job_name}"

# ? check if the job already exists; unique constraint
async with self._session_factory() as db_session:
db_session.add(ProjectRecord(
job_id = self.job_id,
job_name = job_name,
created_on = now
))

await db_session.commit()
_exists = (
await db_session.execute(
sa.select(ProjectRecord).where(
ProjectRecord.job_name == job_name
)
)
).scalar_one_or_none()

if _exists:
print(
f"WARN:: JOB : {job_name} Alread Exists. "
f"Use the Existing ID: {_exists.job_id}. "
f"Current ID: {self.job_id} is discarded."
)
else:
db_session.add(ProjectRecord(
job_id = self.job_id,
job_name = job_name,
created_on = now
))

await db_session.commit()

return retvalue

Expand Down
2 changes: 1 addition & 1 deletion example/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from aptracker.database.sqlalchemy import SQLAlchemyDB

async def main(session : SessionConfig) -> None:
job_name = f"[{str(uuid.uuid4()).upper()[:3]}] Example Project"
job_name = f"Example Project"
session_name = f"[{str(uuid.uuid4()).upper()[:3]}] Example Session"

logging.basicConfig(level = logging.INFO)
Expand Down