|
4 | 4 |
|
5 | 5 | from fastapi import HTTPException
|
6 | 6 | from pydantic import ValidationError
|
7 |
| -from sqlalchemy import and_, or_ |
| 7 | +from sqlalchemy import and_, func, or_ |
8 | 8 | from sqlalchemy.exc import OperationalError, PendingRollbackError
|
9 | 9 | from sqlalchemy.orm import Query, Session
|
10 | 10 | from sqlalchemy.orm.session import object_session
|
@@ -158,25 +158,49 @@ def clean_database(*, log: Logger = log) -> None:
|
158 | 158 | db.delete(episode)
|
159 | 159 | db.commit()
|
160 | 160 |
|
161 |
| - # Delete duplicate Cards |
162 |
| - for series in db.query(Series).all(): |
163 |
| - episode_ids = db.query(Episode.id)\ |
164 |
| - .filter_by(series_id=series.id)\ |
165 |
| - .all() |
166 |
| - |
167 |
| - for episode_id in episode_ids: |
168 |
| - cards = db.query(Card)\ |
169 |
| - .filter_by(episode_id=episode_id[0])\ |
170 |
| - .all() |
171 |
| - |
172 |
| - if get_preferences().library_unique_cards: |
173 |
| - ... |
174 |
| - else: |
175 |
| - for card in cards[::-1][1:]: # All but the latest Card |
176 |
| - log.debug(f'Deleting redundant {card}') |
177 |
| - card.file.unlink(missing_ok=True) |
178 |
| - db.delete(card) |
| 161 | + # Delete Episodes which are duplicates |
| 162 | + subquery = db\ |
| 163 | + .query( |
| 164 | + Episode.series_id, |
| 165 | + Episode.season_number, |
| 166 | + Episode.episode_number, |
| 167 | + func.min(Episode.id).label('min_id') |
| 168 | + )\ |
| 169 | + .group_by( |
| 170 | + Episode.series_id, |
| 171 | + Episode.season_number, |
| 172 | + Episode.episode_number |
| 173 | + )\ |
| 174 | + .subquery() |
| 175 | + to_delete = db.query(Episode).filter( |
| 176 | + Episode.series_id == subquery.c.series_id, |
| 177 | + Episode.season_number == subquery.c.season_number, |
| 178 | + Episode.episode_number == subquery.c.episode_number, |
| 179 | + Episode.id != subquery.c.min_id, |
| 180 | + ) |
| 181 | + for episode in to_delete.all(): |
| 182 | + log.trace(f'Deleting duplicate Episode {episode}') |
| 183 | + db.delete(episode) |
179 | 184 | db.commit()
|
| 185 | + |
| 186 | + # Delete duplicate Cards |
| 187 | + if not get_preferences().library_unique_cards: |
| 188 | + subquery = db\ |
| 189 | + .query( |
| 190 | + Card.episode_id, |
| 191 | + func.max(Card.id).label('max_id'), |
| 192 | + )\ |
| 193 | + .group_by(Card.episode_id)\ |
| 194 | + .subquery() |
| 195 | + to_delete = db.query(Card).filter( |
| 196 | + Card.episode_id == subquery.c.episode_id, |
| 197 | + Card.id != subquery.c.max_id, |
| 198 | + ) |
| 199 | + for card in to_delete.all(): |
| 200 | + log.debug(f'Deleting redundant {card}') |
| 201 | + card.file.unlink(missing_ok=True) |
| 202 | + db.delete(card) |
| 203 | + db.commit() |
180 | 204 | except Exception:
|
181 | 205 | log.exception('Failed to clean the database')
|
182 | 206 |
|
|
0 commit comments