Skip to content

Commit

Permalink
Add support for cleaning up data from releases_json/release_assets
Browse files Browse the repository at this point in the history
  • Loading branch information
bhearsum committed Oct 13, 2023
1 parent 2181f92 commit 4fef9db
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions scripts/manage-db.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@
sys.path.append(path.join(path.dirname(__file__), path.join("..", "vendor", "lib", "python")))


RELEASES_CLEANUP_CONDITION = """
LEFT JOIN rules rules_mapping ON (name=rules_mapping.mapping)
WHERE name LIKE '%%nightly%%'
AND name NOT LIKE '%%latest'
AND rules_mapping.mapping IS NULL
AND (STR_TO_DATE(RIGHT(name, 14), "%%Y%%m%%d%%H%%i%%S") < NOW() - INTERVAL {nightly_age} DAY);
"""


def cleanup_releases(trans, nightly_age, dryrun=True):
# This and the subsequent queries use "%%%%%" because we end up going
# through two levels of Python string formatting. The first is here,
# and the second happens at a low level of SQLAlchemy when the transaction
# is being executed.
query = (
"""
LEFT JOIN rules rules_mapping ON (name=rules_mapping.mapping)
WHERE name LIKE '%%%%nightly%%%%'
AND name NOT LIKE '%%%%latest'
AND rules_mapping.mapping IS NULL
AND (STR_TO_DATE(RIGHT(name, 14), "%%%%Y%%%%m%%%%d%%%%H%%%%i%%%%S") < NOW() - INTERVAL %s DAY);
"""
% nightly_age
)
query = RELEASES_CLEANUP_CONDITION.format(nightly_age=nightly_age)
if dryrun:
todelete = trans.execute("SELECT name FROM releases" + query).fetchall()
print("Releases rows to be deleted:")
Expand All @@ -45,6 +45,20 @@ def cleanup_releases(trans, nightly_age, dryrun=True):
trans.execute("DELETE releases FROM releases" + query)


def cleanup_releases_json(trans, nightly_age, dryrun=True):
query = RELEASES_CLEANUP_CONDITION.format(nightly_age=nightly_age)
if dryrun:
todelete = trans.execute("SELECT name FROM releases_json" + query).fetchall()
print("Releases JSON rows to be deleted:")
if todelete:
print("\n".join(itertools.chain(*todelete)))
else:
print(" - None")
else:
trans.execute("DELETE releases_json FROM releases_json" + query)
trans.execute("DELETE release_assets FROM release_assets" + query)


def chunk_list(list_object, n):
"""
Yield successive n-sized chunks from list_object.
Expand Down Expand Up @@ -203,5 +217,7 @@ def _strip_multiple_spaces(string):
with db.begin() as trans:
if action == "cleanup":
cleanup_releases(trans, nightly_age, dryrun=False)
cleanup_releases_json(trans, nightly_age, dryrun=False)
else:
cleanup_releases(trans, nightly_age, dryrun=True)
cleanup_releases_json(trans, nightly_age, dryrun=True)

0 comments on commit 4fef9db

Please sign in to comment.