Skip to content

Commit

Permalink
Merge pull request #4191 from whisperity/feat/show-zero-checkers/fix-…
Browse files Browse the repository at this point in the history
…migration

fix(migration): Migrate reports with appropriate default checker ID
  • Loading branch information
vodorok authored Mar 20, 2024
2 parents 9a85f26 + 3cfb1c2 commit 51bffbf
Showing 1 changed file with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,13 @@ def add_checkers():
LOG.info("Done filling 'checkers', %d unique entries.", checker_count)

def upgrade_reports():
Base = automap_base()
Base.prepare(conn, reflect=True)
Report = Base.classes.reports # 'reports' is the table name!
Checker = Base.classes.checkers

db = Session(bind=conn)
report_count = db.execute("SELECT COUNT(id) FROM reports;").scalar()
report_count = db.query(Report.id).count()
if not report_count:
return

Expand All @@ -214,6 +219,10 @@ def _print_progress(batch: int):
if batch < num_batches else report_count,
float(batch) / num_batches * 100)

fake_chk_id = db.query(Checker.id) \
.filter(Checker.analyzer_name == FakeChecker[0],
Checker.checker_name == FakeChecker[1]) \
.scalar()
for i in range(0, num_batches):
# FIXME: "UPDATE ... SET ... FROM ..." is only supported starting
# with SQLite version 3.33.0 (2020-08-14). Until this version
Expand All @@ -233,7 +242,9 @@ def _print_progress(batch: int):
WHERE reports.id IN (
SELECT reports.id
FROM reports
WHERE reports.checker_id = 0
WHERE reports.checker_id = {fake_chk_id}
AND reports.analyzer_name != '{FakeChecker[0]}'
AND reports.checker_name != '{FakeChecker[1]}'
LIMIT {REPORT_UPDATE_CHUNK_SIZE}
)
;
Expand All @@ -249,7 +260,9 @@ def _print_progress(batch: int):
AND reports.id IN (
SELECT reports.id
FROM reports
WHERE reports.checker_id = 0
WHERE reports.checker_id = {fake_chk_id}
AND reports.analyzer_name != '{FakeChecker[0]}'
AND reports.checker_name != '{FakeChecker[1]}'
LIMIT {REPORT_UPDATE_CHUNK_SIZE}
)
;
Expand Down Expand Up @@ -455,8 +468,13 @@ def restore_report_columns():
"this is a technical note.")

def downgrade_reports():
Base = automap_base()
Base.prepare(conn, reflect=True)
Report = Base.classes.reports # 'reports' is the table name!
Checker = Base.classes.checkers

db = Session(bind=conn)
report_count = db.execute("SELECT COUNT(id) FROM reports;").scalar()
report_count = db.query(Report.id).count()
if not report_count:
return

Expand All @@ -473,6 +491,10 @@ def _print_progress(batch: int):
if batch < num_batches else report_count,
float(batch) / num_batches * 100)

fake_chk_id = db.query(Checker.id) \
.filter(Checker.analyzer_name == FakeChecker[0],
Checker.checker_name == FakeChecker[1]) \
.scalar()
for i in range(0, num_batches):
# FIXME: "UPDATE ... SET ... FROM ..." is only supported starting
# with SQLite version 3.33.0 (2020-08-14). Until this version
Expand All @@ -483,14 +505,16 @@ def _print_progress(batch: int):
db.execute(f"""
UPDATE reports
SET
(analyzer_name, checker_id, severity, checker_id_lookup) =
(SELECT analyzer_name, checker_name, severity, '0'
(analyzer_name, checker_id, severity,
checker_id_lookup) =
(SELECT analyzer_name, checker_name, severity,
'{fake_chk_id}'
FROM checkers
WHERE checkers.id = reports.checker_id_lookup)
WHERE reports.id IN (
SELECT reports.id
FROM reports
WHERE reports.checker_id_lookup != 0
WHERE reports.checker_id_lookup != {fake_chk_id}
LIMIT {REPORT_UPDATE_CHUNK_SIZE}
)
;
Expand All @@ -502,13 +526,13 @@ def _print_progress(batch: int):
analyzer_name = checkers.analyzer_name,
checker_id = checkers.checker_name,
severity = checkers.severity,
checker_id_lookup = 0
checker_id_lookup = {fake_chk_id}
FROM checkers
WHERE checkers.id = reports.checker_id_lookup
AND reports.id IN (
SELECT reports.id
FROM reports
WHERE reports.checker_id_lookup != 0
WHERE reports.checker_id_lookup != {fake_chk_id}
LIMIT {REPORT_UPDATE_CHUNK_SIZE}
)
;
Expand Down

0 comments on commit 51bffbf

Please sign in to comment.