aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'buildbot_gentoo_ci/db/versions.py')
-rw-r--r--buildbot_gentoo_ci/db/versions.py174
1 files changed, 111 insertions, 63 deletions
diff --git a/buildbot_gentoo_ci/db/versions.py b/buildbot_gentoo_ci/db/versions.py
index afb9e52..60946c5 100644
--- a/buildbot_gentoo_ci/db/versions.py
+++ b/buildbot_gentoo_ci/db/versions.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,11 +27,48 @@ 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 VersionModel:
+ uuid: str
+ name: str
+ package_uuid: str
+ file_hash: str
+ commit_id: str
+ change_id: int
+ created_at: datetime.datetime | None
+ 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_Version(model: VersionModel):
+ if model is None:
+ return None
+ return {
+ 'uuid' : model.uuid,
+ 'name' : model.name,
+ 'package_uuid' : model.package_uuid,
+ 'file_hash' : model.file_hash,
+ 'commit_id' : model.commit_id,
+ 'change_id' : model.change_id,
+ 'deleted' : model.deleted,
+ 'deleted_at' : model.deleted_at,
+ 'created_at' : model.created_at,
+ }
+
class VersionsConnectorComponent(base.DBConnectorComponent):
- @defer.inlineCallbacks
- def getVersionByName(self, name, p_uuid, deleted=False):
- def thd(conn):
+ def getVersionByName(self, name: str, p_uuid: str, deleted: bool | None = False) -> defer.Deferred[VersionModel | None]:
+ def thd(conn) -> VersionModel | None:
tbl = self.db.model.versions
q = tbl.select()
q = q.where(tbl.c.name == name)
@@ -35,56 +76,57 @@ class VersionsConnectorComponent(base.DBConnectorComponent):
q = q.where(tbl.c.deleted == deleted)
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
-
- @defer.inlineCallbacks
- def getVersionByUuid(self, uuid):
- def thd(conn):
+ rv = None
+ if row:
+ rv = self._model_from_row_VersionModel(row)
+ res.close()
+ return rv
+ return self.db.pool.do(thd)
+
+ def getVersionByUuid(self, uuid: str) -> defer.Deferred[VersionModel | None]:
+ def thd(conn) -> VersionModel | None:
tbl = self.db.model.versions
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
-
- @defer.inlineCallbacks
- def addVersion(self, name, package_uuid, file_hash, commit_id, change_id):
- def thd(conn, no_recurse=False):
+ rv = None
+ if row:
+ rv = self._model_from_row_VersionModel(row)
+ res.close()
+ return rv
+ return self.db.pool.do(thd)
+
+ def addVersion(self, name: str, package_uuid: str, file_hash: str, commit_id: int, change_id: int) -> defer.Deferred[str]:
+ def thd(conn) -> str:
+ insert_row = {
+ 'name': name,
+ 'package_uuid' : package_uuid,
+ 'file_hash' : file_hash,
+ 'commit_id' : commit_id,
+ 'change_id' : change_id,
+ 'created_at' : int(self.master.reactor.seconds()),
+ 'deleted_at' : 0,
+ 'deleted' : False,
+ }
try:
- tbl = self.db.model.versions
- q = tbl.insert()
- r = conn.execute(q, dict(name=name,
- package_uuid=package_uuid,
- file_hash=file_hash,
- commit_id=commit_id,
- change_id=change_id))
+ r = conn.execute(self.db.model.versions.insert(), insert_row)
+ conn.commit()
except (sa.exc.IntegrityError, sa.exc.ProgrammingError):
- uuid = None
+ 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)
- @defer.inlineCallbacks
def delVersion(self, uuid):
- deleted_at = int(self.master.reactor.seconds())
- def thd(conn, no_recurse=False):
-
- tbl = self.db.model.versions
- q = tbl.update()
- q = q.where(tbl.c.uuid == uuid)
- conn.execute(q, deleted=True,
- deleted_at=deleted_at)
- yield self.db.pool.do(thd)
+ def thd(conn):
+ tbl = self.db.model.versions
+ q = tbl.update().where(tbl.c.uuid == uuid)
+ res = conn.execute(q.values(deleted=True, deleted_at=int(self.master.reactor.seconds())))
+ #conn.commit()
+ res.close()
+ return self.db.pool.do(thd)
@defer.inlineCallbacks
def addKeyword(self, version_uuid, keyword_id, status):
@@ -95,6 +137,7 @@ class VersionsConnectorComponent(base.DBConnectorComponent):
r = conn.execute(q, dict(version_uuid=version_uuid,
keyword_id=keyword_id,
status=status))
+ conn.commit()
except (sa.exc.IntegrityError, sa.exc.ProgrammingError):
uuid = None
else:
@@ -112,6 +155,7 @@ class VersionsConnectorComponent(base.DBConnectorComponent):
r = conn.execute(q, dict(version_uuid=version_uuid,
metadata=metadata,
value=value))
+ #conn.commit()
except (sa.exc.IntegrityError, sa.exc.ProgrammingError):
id = None
else:
@@ -140,41 +184,44 @@ class VersionsConnectorComponent(base.DBConnectorComponent):
q = tbl.select()
q = q.where(tbl.c.deleted == deleted)
q = q.where(tbl.c.package_uuid == p_uuid)
- return [self._row2dict(conn, row)
+ return [self._model_from_row_VersionModel(row)
for row in conn.execute(q).fetchall()]
res = yield self.db.pool.do(thd)
return res
- @defer.inlineCallbacks
- def removeVersion(self, uuid):
- def thd(conn, no_recurse=False):
+ def removeVersion(self, uuid: str) -> defer.Deferred[None]:
+ def thd(conn) -> None:
tbl = self.db.model.versions
q = tbl.delete()
q = q.where(tbl.c.uuid == uuid)
- conn.execute(q)
- yield self.db.pool.do(thd)
+ res = conn.execute(q)
+ conn.commit()
+ res.close()
+ return self.db.pool.do(thd)
- @defer.inlineCallbacks
- def removeVersionMetadata(self, version_uuid):
- def thd(conn, no_recurse=False):
+ def removeVersionMetadata(self, version_uuid: str) -> defer.Deferred[None]:
+ def thd(conn) -> None:
tbl = self.db.model.versions_metadata
q = tbl.delete()
q = q.where(tbl.c.version_uuid == version_uuid)
- conn.execute(q)
- yield self.db.pool.do(thd)
+ res = conn.execute(q)
+ conn.commit()
+ res.close()
+ return self.db.pool.do(thd)
- @defer.inlineCallbacks
- def removeVersionKeyword(self, version_uuid):
- def thd(conn, no_recurse=False):
+ def removeVersionKeyword(self, version_uuid: str) -> defer.Deferred[None]:
+ def thd(conn) -> None:
tbl = self.db.model.versions_keywords
q = tbl.delete()
q = q.where(tbl.c.version_uuid == version_uuid)
- conn.execute(q)
- yield self.db.pool.do(thd)
+ res = conn.execute(q)
+ conn.commit()
+ res.close()
+ return self.db.pool.do(thd)
- def _row2dict(self, conn, row):
- return dict(
+ def _model_from_row_VersionModel(self, row):
+ return VersionModel(
uuid=row.uuid,
name=row.name,
package_uuid=row.package_uuid,
@@ -182,7 +229,8 @@ class VersionsConnectorComponent(base.DBConnectorComponent):
commit_id=row.commit_id,
change_id = row.change_id,
deleted=row.deleted,
- deleted_at=row.deleted_at
+ deleted_at=row.deleted_at,
+ created_at=row.created_at,
)
def _row2dict_version_metadata(self, conn, row):