add prune-woodpecker.py

This commit is contained in:
Martin Matous 2025-03-18 19:47:44 +01:00
parent 02e59ce7e2
commit 2cef97f92b
Signed by: mmatous
GPG key ID: 8BED4CD352953224
2 changed files with 51 additions and 0 deletions

View file

@ -107,6 +107,15 @@ Dependencies: imagemagick
Usage: Run next to `logo.png` and `workdir` directory with photos
## prune-woodpecker.py
Prune Woodpecker CI's DB. Expects `woodpecker.sqlite` in the same directory. Useful for getting rid of a string
of failed pipelines while setting things up.
Tested with Woodpecker 2.8.x and sqlite backend.
Status: one-off
Usage: `./prune-woodpecker.py <start> <stop>`
## sync-apparmor.py
Scan source directory of profiles and binaries in the system.

42
prune-woodpecker.py Normal file
View file

@ -0,0 +1,42 @@
#!/usr/bin/env python3
import sqlite3
import sys
from contextlib import closing
start = int(sys.argv[1]) # starting id to delete (inclusive)
stop = int(sys.argv[2]) # final id to delete (inclusive)
with closing(sqlite3.connect('./woodpecker.sqlite', autocommit=False)) as con:
del_ids = con.execute('SELECT id FROM pipelines WHERE number BETWEEN ? AND ?;', (start, stop)).fetchall()
del_ids = tuple(i[0] for i in del_ids)
n_deleted = len(del_ids)
max_id = max(del_ids)
placeholders = ', '.join('?' for i in range(n_deleted))
try:
con.execute(f'DELETE FROM pipelines WHERE id IN ({placeholders});', del_ids)
con.execute(f'DELETE FROM pipeline_configs WHERE pipeline_id IN ({placeholders});', del_ids)
con.execute(f'DELETE FROM steps WHERE pipeline_id IN ({placeholders});', del_ids)
con.execute(f'DELETE FROM workflows WHERE pipeline_id IN ({placeholders});', del_ids)
con.execute('DELETE FROM configs WHERE id NOT IN (SELECT config_id FROM pipeline_configs);')
except sqlite3.Error as e:
e.add_note(f'with {del_ids=}, {n_deleted=}, {placeholders=}')
raise
try:
res = con.execute(
'UPDATE pipelines SET number = number - ?, id = id - ? WHERE id > ?;', (n_deleted, n_deleted, max_id)
)
res = con.execute(
'UPDATE pipeline_configs SET pipeline_id = pipeline_id - ? WHERE pipeline_id > ?;', (n_deleted, max_id)
)
res = con.execute('UPDATE steps SET pipeline_id = pipeline_id - ? WHERE pipeline_id > ?;', (n_deleted, max_id))
res = con.execute(
'UPDATE workflows SET pipeline_id = pipeline_id - ? WHERE pipeline_id > ?;', (n_deleted, max_id)
)
except sqlite3.Error as e:
e.add_note(f'with {n_deleted=}, {max_id=}')
raise
con.commit()