Skip to content

Commit

Permalink
Merge pull request #90 from filips123/cleanup-database
Browse files Browse the repository at this point in the history
Added command for cleaning up the database
  • Loading branch information
PetJer authored Oct 29, 2023
2 parents ecc2cdd + a847083 commit 2d11715
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions API/gimvicurnik/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
create_database_command,
update_eclassroom_command,
update_menu_command,
cleanup_database_command,
update_timetable_command,
)
from .config import Config
Expand Down Expand Up @@ -245,6 +246,7 @@ def register_commands(self) -> None:
self.app.cli.add_command(update_timetable_command)
self.app.cli.add_command(update_eclassroom_command)
self.app.cli.add_command(update_menu_command)
self.app.cli.add_command(cleanup_database_command)
self.app.cli.add_command(create_database_command)

def register_routes(self) -> None:
Expand Down
25 changes: 24 additions & 1 deletion API/gimvicurnik/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import click
from flask import current_app

from ..database import Base, SessionFactory
from datetime import datetime, timedelta
from sqlalchemy import and_, or_

from ..database import Base, SessionFactory, Document, DocumentType
from ..updaters import EClassroomUpdater, MenuUpdater, TimetableUpdater
from ..utils.sentry import with_transaction

Expand Down Expand Up @@ -51,6 +54,26 @@ def update_menu_command() -> None:
updater.update()


@click.command("cleanup-database", help="Clean up the database.")
@with_transaction(name="cleanup-database", op="command")
def cleanup_database_command() -> None:
"""Remove lunch schedules, snack menus and lunch menus older than 2 weeks from the database."""

logging.getLogger(__name__).info("Cleaning up the database")

with SessionFactory.begin() as session:
session.query(Document).filter(
and_(
or_(
Document.type == DocumentType.LUNCH_SCHEDULE,
Document.type == DocumentType.SNACK_MENU,
Document.type == DocumentType.LUNCH_MENU,
),
Document.effective < datetime.now().date() - timedelta(weeks=2),
)
).delete()


@click.command("create-database", help="Create the database.")
@click.option("--recreate", help="Remove existing tables before creating new ones.", is_flag=True)
@click.pass_context
Expand Down

0 comments on commit 2d11715

Please sign in to comment.