Skip to content

Commit 28d57c6

Browse files
committed
admin: spears: Remove unused entries when updating from config
1 parent 500dfc9 commit 28d57c6

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/lkadmin/spears.py

+24-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def spears():
2727
'''Configure automatic package migration.'''
2828

2929

30-
def _add_migration_task(repo_name: str, source_suites: T.List[str], target_suite: str, delays: dict[str, int]):
30+
def _add_migration_task(repo_name: str, source_suites: T.List[str], target_suite: str, delays: dict[str, int]) -> str:
3131
"""Add or update a migration task."""
3232

3333
from laniakea.db import ChangesUrgency, SpearsMigrationTask
@@ -58,6 +58,8 @@ def _add_migration_task(repo_name: str, source_suites: T.List[str], target_suite
5858
raise ValueError('The priority value "{}" is unknown!'.format(prio_name))
5959
stask.delays[prio_name] = days
6060

61+
return stask.make_migration_unique_name()
62+
6163

6264
@spears.command()
6365
def configure_all():
@@ -133,13 +135,31 @@ def remove_hint(source_suite, target_suite, hint):
133135

134136
@spears.command()
135137
@click.argument('config_fname', nargs=1)
136-
def add_from_config(config_fname):
138+
def update_from_config(config_fname):
137139
"""Add/update migration tasks from a TOML config file."""
140+
from laniakea.db import SpearsHint, SpearsExcuse, SpearsMigrationTask
141+
138142
try:
139143
with open(config_fname, 'r', encoding='utf-8') as f:
140144
conf = tomlkit.load(f)
141145
except Exception as e:
142146
print_error_exit('Unable to load data from configuration file: {}'.format(str(e)))
143147

144-
for task_d in conf.get('MigrationTasks', []):
145-
_add_migration_task(**task_d)
148+
with session_scope() as session:
149+
entries = session.query(SpearsMigrationTask).all()
150+
known_tasks = {}
151+
for e in entries:
152+
known_tasks[e.make_migration_unique_name()] = e
153+
154+
for task_d in conf.get('MigrationTasks', []):
155+
unq_name = _add_migration_task(**task_d)
156+
known_tasks.pop(unq_name, None)
157+
158+
# delete the remaining, orphaned entries
159+
for task in known_tasks.values():
160+
hints = session.query(SpearsHint).filer(SpearsHint.migration_id == task.id).all()
161+
excuses = session.query(SpearsExcuse).filer(SpearsExcuse.migration_id == task.id).all()
162+
163+
session.delete(hints)
164+
session.delete(excuses)
165+
session.delete(task)

0 commit comments

Comments
 (0)