xfstestsdb/xfstestsdb/xunit/gc.py

30 lines
1.2 KiB
Python

# Copyright 2023 (c) Anna Schumaker.
"""The `xfstestsdb xunit gc` command."""
import argparse
from .. import commands
from .. import sqlite
class Command(commands.Command):
"""The `xfstestsdb xunit gc` command."""
def __init__(self, subparser: argparse.Action,
sql: sqlite.Connection) -> None:
"""Set up the xunit gc command."""
super().__init__(subparser, sql, "gc",
help="garbage collect xunit entries")
self.parser.add_argument("--dry-run", action="store_true",
help="do not remove the xunit entries")
def do_command(self, args: argparse.Namespace) -> None:
"""Clean up the xunit table."""
how = "would be" if args.dry_run else "has been"
cur = self.sql("""SELECT xunitid, runid, name FROM xunits
WHERE NOT EXISTS (SELECT 1 FROM testcases
WHERE xunitid = xunits.xunitid)""")
for row in cur.fetchall():
if not args.dry_run:
self.sql("DELETE FROM xunits WHERE xunitid=?", row['xunitid'])
print(f"run #{row['runid']} xunit '{row['name']}' {how} deleted")