Make use of db.cursor() in with blocks

This commit is contained in:
Stephan Kulow 2022-10-26 15:49:14 +02:00
parent 217bbcadb5
commit 4dc7e972a2
3 changed files with 103 additions and 95 deletions

View File

@ -188,12 +188,14 @@ class DBRevision:
def files_list(self, db):
if self._files:
return self._files
cur = db.cursor()
with db.cursor() as cur:
cur.execute("SELECT * from files where revision_id=%s", (self.dbid,))
self._files = []
for row in cur.fetchall():
(_, _, name, md5, size, mtime) = row
self._files.append({"md5": md5, "size": size, "mtime": mtime, "name": name})
self._files.append(
{"md5": md5, "size": size, "mtime": mtime, "name": name}
)
self._files.sort(key=lambda x: x["name"])
return self._files

View File

@ -12,7 +12,7 @@ class Exporter:
def run(self):
db = DB()
cur = db.cursor()
with db.cursor() as cur:
cur.execute(
"SELECT * from revisions where package=%s ORDER BY project,rev",
(self.package,),

View File

@ -149,7 +149,7 @@ class Importer:
db.conn.commit()
def find_linked_revs(self, db):
cur = db.cursor()
with db.cursor() as cur:
cur.execute(
"""SELECT * from revisions WHERE id in (SELECT l.revision_id FROM links l
LEFT JOIN linked_revs lrevs ON lrevs.revision_id=l.revision_id
@ -168,7 +168,7 @@ class Importer:
)
def calculate_file_hashes(self, db):
cur = db.cursor()
with db.cursor() as cur:
cur.execute(
"SELECT * from revisions where files_hash IS NULL AND broken is FALSE"
)
@ -180,7 +180,7 @@ class Importer:
)
def fetch_all_linked_packages(self, db, project, package):
cur = db.cursor()
with db.cursor() as cur:
cur.execute(
"""SELECT DISTINCT l.project, l.package from links l JOIN revisions r
on r.id=l.revision_id WHERE r.project=%s AND r.package=%s""",
@ -191,30 +191,33 @@ class Importer:
self.update_db_package(db, lproject, lpackage)
def find_fake_revisions(self, db):
cur = db.cursor()
with db.cursor() as cur:
cur.execute(
"""SELECT * from revisions WHERE
id in (SELECT revision_id from linked_revs WHERE considered=FALSE) AND
id not in (SELECT revision_id FROM fake_revs) ORDER by project,package,rev"""
)
for row in cur.fetchall():
rev = DBRevision(row)
DBRevision(row)
def _find_fake_revision(self, db, rev):
prev = rev.previous_commit(db)
if not prev:
with db.cursor() as cur:
cur.execute(
"UPDATE linked_revs SET considered=TRUE where revision_id=%s",
(rev.dbid,),
)
continue
cur2 = db.cursor()
cur2.execute(
return
with db.cursor() as cur:
cur.execute(
"""SELECT * from revisions where id in
(SELECT revision_id from linked_revs WHERE linked_id=%s)
AND commit_time <= %s ORDER BY commit_time""",
(prev.dbid, rev.commit_time),
)
last_linked = None
for linked in cur2.fetchall():
for linked in cur.fetchall():
linked = DBRevision(linked)
nextrev = linked.next_commit(db)
if nextrev and nextrev.commit_time < rev.commit_time:
@ -224,21 +227,24 @@ class Importer:
"UPDATE linked_revs SET considered=TRUE where revision_id=%s",
(rev.dbid,),
)
if last_linked:
if not last_linked:
return
with db.cursor() as cur:
linked = last_linked
cur2.execute(
cur.execute(
"SELECT 1 FROM fake_revs where revision_id=%s AND linked_id=%s",
(rev.dbid, linked.dbid),
)
if cur2.fetchone():
if cur.fetchone():
cur.execute(
"UPDATE linked_revs SET considered=TRUE where revision_id=%s",
(rev.dbid,),
)
continue
return
fake_rev = linked.rev + rev.rev / 1000.0
comment = f"Updating link to change in {rev.project}/{rev.package} revision {rev.rev}"
cur2.execute(
cur.execute(
"""INSERT INTO revisions (project,package,rev,unexpanded_srcmd5,
commit_time, userid, comment) VALUES(%s,%s,%s,%s,%s,%s,%s) RETURNING id""",
(
@ -251,12 +257,12 @@ class Importer:
comment,
),
)
new_id = cur2.fetchone()[0]
cur2.execute(
new_id = cur.fetchone()[0]
cur.execute(
"""INSERT INTO linked_revs (revision_id, linked_id) VALUES (%s,%s)""",
(new_id, rev.dbid),
)
cur2.execute(
cur.execute(
"""INSERT INTO fake_revs (revision_id, linked_id) VALUES (%s,%s)""",
(rev.dbid, linked.dbid),
)