Skip to content

Commit 2d11715

Browse files
authored
Merge pull request #90 from filips123/cleanup-database
Added command for cleaning up the database
2 parents ecc2cdd + a847083 commit 2d11715

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

API/gimvicurnik/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
create_database_command,
2525
update_eclassroom_command,
2626
update_menu_command,
27+
cleanup_database_command,
2728
update_timetable_command,
2829
)
2930
from .config import Config
@@ -245,6 +246,7 @@ def register_commands(self) -> None:
245246
self.app.cli.add_command(update_timetable_command)
246247
self.app.cli.add_command(update_eclassroom_command)
247248
self.app.cli.add_command(update_menu_command)
249+
self.app.cli.add_command(cleanup_database_command)
248250
self.app.cli.add_command(create_database_command)
249251

250252
def register_routes(self) -> None:

API/gimvicurnik/commands/__init__.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import click
55
from flask import current_app
66

7-
from ..database import Base, SessionFactory
7+
from datetime import datetime, timedelta
8+
from sqlalchemy import and_, or_
9+
10+
from ..database import Base, SessionFactory, Document, DocumentType
811
from ..updaters import EClassroomUpdater, MenuUpdater, TimetableUpdater
912
from ..utils.sentry import with_transaction
1013

@@ -51,6 +54,26 @@ def update_menu_command() -> None:
5154
updater.update()
5255

5356

57+
@click.command("cleanup-database", help="Clean up the database.")
58+
@with_transaction(name="cleanup-database", op="command")
59+
def cleanup_database_command() -> None:
60+
"""Remove lunch schedules, snack menus and lunch menus older than 2 weeks from the database."""
61+
62+
logging.getLogger(__name__).info("Cleaning up the database")
63+
64+
with SessionFactory.begin() as session:
65+
session.query(Document).filter(
66+
and_(
67+
or_(
68+
Document.type == DocumentType.LUNCH_SCHEDULE,
69+
Document.type == DocumentType.SNACK_MENU,
70+
Document.type == DocumentType.LUNCH_MENU,
71+
),
72+
Document.effective < datetime.now().date() - timedelta(weeks=2),
73+
)
74+
).delete()
75+
76+
5477
@click.command("create-database", help="Create the database.")
5578
@click.option("--recreate", help="Remove existing tables before creating new ones.", is_flag=True)
5679
@click.pass_context

0 commit comments

Comments
 (0)