Skip to content

Commit 8063d33

Browse files
committed
fix: Improve warnings, prompt, and SQL style.
* Send dry-run warning to stderr * Shorten confirmation prompt now that counts are already printed * Document PRUNABLE_TABLES parent-before-child order * Quote table identifiers in prune SQL Signed-off-by: Alan Peixinho <alan.peixinho@profusion.mobi>
1 parent e275881 commit 8063d33

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

backend/kernelCI_app/management/commands/prune_db.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from kernelCI_app.management.commands.helpers.intervals import parse_interval
2121

22+
# Strict parent-before-child order: a checkout owns builds, a build owns tests.
2223
PRUNABLE_TABLES = ("checkouts", "builds", "tests")
2324

2425

@@ -138,7 +139,7 @@ def handle(self, *args, **options):
138139
return
139140

140141
if dry_run:
141-
self.stdout.write(
142+
self.stderr.write(
142143
self.style.WARNING(
143144
"[DRY RUN] No rows deleted. Run without --dry-run to "
144145
"execute."
@@ -147,13 +148,8 @@ def handle(self, *args, **options):
147148
return
148149

149150
if not options["yes"]:
150-
summary = ", ".join(f"{counts[t]} {t}" for t in selected_tables)
151151
try:
152-
answer = (
153-
input(f"Delete {summary} ({total} rows total)? [y/N] ")
154-
.strip()
155-
.lower()
156-
)
152+
answer = input("Delete these rows? [y/N] ").strip().lower()
157153
except EOFError:
158154
answer = ""
159155
if answer not in ("y", "yes"):
@@ -175,7 +171,7 @@ def handle(self, *args, **options):
175171
)
176172
finally:
177173
for temp_table in temp_tables.values():
178-
cursor.execute(f"DROP TABLE IF EXISTS {temp_table}")
174+
cursor.execute(f'DROP TABLE IF EXISTS "{temp_table}"')
179175

180176
def _build_where_clauses(
181177
self, origins_condition, protect_incidents, selected_tables
@@ -231,22 +227,23 @@ def _build_where_clauses(
231227
def _materialize(self, cursor, table, temp_table, where, params):
232228
"""Snapshot the doomed ids into a temp table so the nested predicate runs
233229
once instead of per batch."""
234-
cursor.execute(f"DROP TABLE IF EXISTS {temp_table}")
230+
cursor.execute(f'DROP TABLE IF EXISTS "{temp_table}"')
235231
cursor.execute(
236-
f"CREATE TEMP TABLE {temp_table} AS SELECT id FROM {table} WHERE {where}",
232+
f'CREATE TEMP TABLE "{temp_table}" AS '
233+
f'SELECT id FROM "{table}" WHERE {where}',
237234
params,
238235
)
239236

240237
def _count(self, cursor, temp_table):
241-
cursor.execute(f"SELECT COUNT(*) FROM {temp_table}")
238+
cursor.execute(f'SELECT COUNT(*) FROM "{temp_table}"')
242239
return cursor.fetchone()[0]
243240

244241
def _batch_delete(self, cursor, table, temp_table, batch_size):
245242
sql = (
246243
f"WITH batch AS ("
247-
f"DELETE FROM {temp_table} WHERE id IN "
248-
f"(SELECT id FROM {temp_table} LIMIT %(batch_size)s) RETURNING id"
249-
f") DELETE FROM {table} WHERE id IN (SELECT id FROM batch)"
244+
f'DELETE FROM "{temp_table}" WHERE id IN '
245+
f'(SELECT id FROM "{temp_table}" LIMIT %(batch_size)s) RETURNING id'
246+
f') DELETE FROM "{table}" WHERE id IN (SELECT id FROM batch)'
250247
)
251248
deleted_total = 0
252249
while True:

0 commit comments

Comments
 (0)