aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'buildbot_gentoo_ci/db/packages.py')
-rw-r--r--buildbot_gentoo_ci/db/packages.py113
1 files changed, 74 insertions, 39 deletions
diff --git a/buildbot_gentoo_ci/db/packages.py b/buildbot_gentoo_ci/db/packages.py
index ad4a244..2e29c02 100644
--- a/buildbot_gentoo_ci/db/packages.py
+++ b/buildbot_gentoo_ci/db/packages.py
@@ -15,7 +15,11 @@
# Copyright Buildbot Team Members
# Origins: buildbot.db.*
# Modifyed by Gentoo Authors.
-# Copyright 2021 Gentoo Authors
+# Copyright 2024 Gentoo Authors
+
+from __future__ import annotations
+from dataclasses import dataclass
+from typing import TYPE_CHECKING
import uuid
import sqlalchemy as sa
@@ -23,12 +27,42 @@ import sqlalchemy as sa
from twisted.internet import defer
from buildbot.db import base
+from buildbot.warnings import warn_deprecated
+
+if TYPE_CHECKING:
+ import datetime
+
+@dataclass
+class PackageModel:
+ uuid : str
+ name : str
+ category_uuid : str
+ repository_uuid : str
+ deleted : bool
+ deleted_at : datetime.datetime | None
+
+ # For backward compatibility
+ def __getitem__(self, key: str):
+ if hasattr(self, key):
+ return getattr(self, key)
+ raise KeyError(key)
+
+def _db2data_Package(model: PackageModel):
+ if model is None:
+ return None
+ return {
+ 'uuid' : model.uuid,
+ 'name' : model.name,
+ 'category_uuid' : model.category_uuid,
+ 'repository_uuid' : model.repository_uuid,
+ 'deleted' : model.deleted,
+ 'deleted_at' : model.deleted_at,
+ }
class PackagesConnectorComponent(base.DBConnectorComponent):
- @defer.inlineCallbacks
- def getPackageByName(self, name, c_uuid, repo_uuid, deleted=False):
- def thd(conn):
+ def getPackageByName(self, name: str, c_uuid: str, repo_uuid: str, deleted: bool | None = False) -> defer.Deferred[PackageModel | None]:
+ def thd(conn) -> PackageModel | None:
tbl = self.db.model.packages
q = tbl.select()
q = q.where(tbl.c.name == name)
@@ -37,52 +71,54 @@ class PackagesConnectorComponent(base.DBConnectorComponent):
q = q.where(tbl.c.repository_uuid == repo_uuid)
res = conn.execute(q)
row = res.fetchone()
- if not row:
- return None
- return self._row2dict(conn, row)
- res = yield self.db.pool.do(thd)
- return res
+ rv = None
+ if row:
+ rv = self._model_from_row_PackageModel(row)
+ res.close()
+ return rv
+ return self.db.pool.do(thd)
- @defer.inlineCallbacks
- def getPackageByUuid(self, uuid):
- def thd(conn):
+ def getPackageByUuid(self, uuid: str) -> defer.Deferred[PackageModel | None]:
+ def thd(conn) -> PackageModel | None:
tbl = self.db.model.packages
q = tbl.select()
q = q.where(tbl.c.uuid == uuid)
res = conn.execute(q)
row = res.fetchone()
- if not row:
- return None
- return self._row2dict(conn, row)
- res = yield self.db.pool.do(thd)
- return res
+ rv = None
+ if row:
+ rv = self._model_from_row_PackageModel(row)
+ res.close()
+ return rv
+ return self.db.pool.do(thd)
- @defer.inlineCallbacks
- def addPackage(self, name, repository_uuid, category_uuid):
- def thd(conn, no_recurse=False):
+ def addPackage(self, name: str, repository_uuid: str, category_uuid: str) -> defer.Deferred[str]:
+ def thd(conn) -> str:
+ insert_row = {
+ 'name' : name,
+ 'repository_uuid' : repository_uuid,
+ 'category_uuid' : category_uuid,
+ 'deleted_at' : 0,
+ 'deleted' : False,
+ }
try:
- tbl = self.db.model.packages
- q = tbl.insert()
- r = conn.execute(q, dict(name=name,
- repository_uuid=repository_uuid,
- category_uuid=category_uuid))
- except Exception as e:
- print(type(e))
- print(e.args)
- print(e)
- uuid = None
+ r = conn.execute(self.db.model.packages.insert(), insert_row)
+ conn.commit()
+ except (sa.exc.IntegrityError, sa.exc.ProgrammingError):
+ conn.rollback()
+ return None
else:
- uuid = r.inserted_primary_key[0]
- return uuid
- res = yield self.db.pool.do(thd)
- return res
+ return r.inserted_primary_key[0]
+ return self.db.pool.do(thd)
- def _row2dict(self, conn, row):
- return dict(
+ def _model_from_row_PackageModel(self, row):
+ return PackageModel(
uuid=row.uuid,
name=row.name,
repository_uuid=row.repository_uuid,
- category_uuid=row.category_uuid
+ category_uuid=row.category_uuid,
+ deleted=row.deleted,
+ deleted_at=row.deleted_at,
)
@defer.inlineCallbacks
@@ -209,8 +245,7 @@ class PackagesConnectorComponent(base.DBConnectorComponent):
def delPackageEmail(self, package_uuid):
def thd(conn, no_recurse=False):
tbl = self.db.model.packages_emails
- conn.execute(tbl.delete(
- whereclause=((tbl.c.package_uuid == package_uuid))))
+ conn.execute(tbl.delete().where(tbl.c.package_uuid == package_uuid))
res = yield self.db.pool.do(thd)
return res