()
Drops all partition tables containing data that's older than 63 days (9 weeks)
Drops all partition tables containing data that's older than 63 days (9 weeks)
Drops all partition tables containing data that s older than 63 days ( 9 weeks )
def prune_synclogs():
    """
    Drops all partition tables containing data that's older than 63 days (9 weeks)
    """
    db = router.db_for_write(SyncLogSQL)
    oldest_date = SyncLogSQL.objects.aggregate(Min('date'))['date__min']
    while oldest_date and (datetime.today() - oldest_date).days > SYNCLOG_RETENTION_DAYS:
        year, week, _ = oldest_date.isocalendar()
        table_name = "{base_name}_y{year}w{week}".format(
            base_name=SyncLogSQL._meta.db_table,
            year=year,
            week="%02d" % week
        )
        drop_query = "DROP TABLE IF EXISTS {}".format(table_name)
        with connections[db].cursor() as cursor:
            cursor.execute(drop_query)
        oldest_date += timedelta(weeks=1)

    # find and log synclogs for which the trigger did not function properly
    with connections[db].cursor() as cursor:
        cursor.execute("select count(*) from only phone_synclogsql")
        orphaned_synclogs = cursor.fetchone()[0]
        metrics_gauge('commcare.orphaned_synclogs', orphaned_synclogs)
def prune_synclogs ( ) : db = router . db_for_write ( SyncLogSQL ) oldest_date = SyncLogSQL . objects . aggregate ( Min ( 'date' ) ) [ 'date__min' ] while oldest_date and ( datetime . today ( ) - oldest_date ) . days > SYNCLOG_RETENTION_DAYS : year , week , _ = oldest_date . isocalendar ( ) table_name = "{base_name}_y{year}w{week}" . format ( base_name = SyncLogSQL . _meta . db_table , year = year , week = "%02d" % week ) drop_query = "DROP TABLE IF EXISTS {}" . format ( table_name ) with connections [ db ] . cursor ( ) as cursor : cursor . execute ( drop_query ) oldest_date += timedelta ( weeks = 1 ) # find and log synclogs for which the trigger did not function properly with connections [ db ] . cursor ( ) as cursor : cursor . execute ( "select count(*) from only phone_synclogsql" ) orphaned_synclogs = cursor . fetchone ( ) [ 0 ] metrics_gauge ( 'commcare.orphaned_synclogs' , orphaned_synclogs )