Skip to content

Commit e7eb1c7

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

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/lkadmin/spears.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -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.name
62+
6163

6264
@spears.command()
6365
def configure_all():
@@ -133,13 +135,26 @@ 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 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.name] = e
153+
154+
for task_d in conf.get('MigrationTasks', []):
155+
task_name = _add_migration_task(**task_d)
156+
known_tasks.pop(task_name)
157+
158+
# delete the remaining, orphaned entries
159+
for task in known_tasks.values():
160+
session.delete(task)

0 commit comments

Comments
 (0)