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): def files_list(self, db):
if self._files: if self._files:
return self._files return self._files
cur = db.cursor() with db.cursor() as cur:
cur.execute("SELECT * from files where revision_id=%s", (self.dbid,)) cur.execute("SELECT * from files where revision_id=%s", (self.dbid,))
self._files = [] self._files = []
for row in cur.fetchall(): for row in cur.fetchall():
(_, _, name, md5, size, mtime) = row (_, _, 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"]) self._files.sort(key=lambda x: x["name"])
return self._files return self._files

View File

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

View File

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