|
4 | 4 | import click
|
5 | 5 | from flask import current_app
|
6 | 6 |
|
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 |
8 | 11 | from ..updaters import EClassroomUpdater, MenuUpdater, TimetableUpdater
|
9 | 12 | from ..utils.sentry import with_transaction
|
10 | 13 |
|
@@ -51,6 +54,26 @@ def update_menu_command() -> None:
|
51 | 54 | updater.update()
|
52 | 55 |
|
53 | 56 |
|
| 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 | + |
54 | 77 | @click.command("create-database", help="Create the database.")
|
55 | 78 | @click.option("--recreate", help="Remove existing tables before creating new ones.", is_flag=True)
|
56 | 79 | @click.pass_context
|
|
0 commit comments