Commit 0e1017b6 authored by Gerion Entrup's avatar Gerion Entrup
Browse files

Plumber: implement missing functions

(except mark_outdated)
parent 0700d328
......@@ -8,7 +8,7 @@ import threading
from collector import Collector
from model import Recording
from mbdata.models import Release, ReleaseGroup
from mbdata.models import Artist, ArtistCreditName, Medium, Release, ReleaseGroup, Track
from utils import get_path
......@@ -16,6 +16,7 @@ class Action(enum.Enum):
check_recordings = 0 # check if all files of the recordings are accessible
clean_cache = 1 # clean the fetcher cache
clean_database = 2 # check all database tables for outdated tables
mark_outdated = 3 # mark outdated files
Actions = queue.Queue(maxsize=2)
......@@ -39,6 +40,8 @@ class Plumber(threading.Thread):
fetcher.clean_cache()
if action == Action.clean_database:
self.clean_database()
if action == Action.mark_outdated:
self.mark_outdated()
self._session.close()
def check_recordings(self):
......@@ -58,7 +61,14 @@ class Plumber(threading.Thread):
def clean_recording(self, recording):
self._logger.info("Delete recording " + recording.path)
self.clean_artist_credit(recording.artist_credit, max_references=1)
# clean tracks
tracks = self._session.query(Track).filter_by(recording=recording)
for track in tracks:
self._session.delete(track)
# clean recording
self._session.delete(recording)
# check rest of the database for consistency
self.clean_database()
def clean_artist_credit(self, artist_credit, max_references=0):
count = self._session.query(Recording).filter_by(
......@@ -69,10 +79,51 @@ class Plumber(threading.Thread):
artist_credit=artist_credit).count()
if count <= max_references:
# delete dependent artist_credit_names
acns = self.session.query(ArtistCreditName).filter_by(
artist_credit=artist_credit)
for acn in acns:
self._session.delete(acn)
# delete artist_credit
self._session.delete(artist_credit)
def clean_database():
pass
def clean_medium(self, medium, max_references=0):
count = self._session.query(Track).filter_by(
medium=medium).count()
if count <= max_references:
self._session.delete(medium)
def clean_release(self, release, max_references=0):
count = self._session.query(Medium).filter_by(
release=release).count()
if count <= max_references:
self._session.delete(release)
def clean_release_group(self, release_group, max_references=0):
count = self._session.query(Release).filter_by(
release_group=release_group).count()
if count <= max_references:
self._session.delete(release_group)
def clean_artist(self, artist, max_references=0):
count = self._session.query(ArtistCreditName).filter_by(
artist=artist).count()
if count <= max_references:
self._session.delete(artist)
def clean_database(self):
for medium in self.session.query(Medium):
self.clean_medium(medium)
for release in self.session.query(Release):
self.clean_release(release)
for release_group in self.session.query(ReleaseGroup):
self.clean_release_group(release_group)
for artist in self.session.query(Artist):
self.clean_artist(artist)
def mark_outdated(self):
for recording in self._session.query(Recording):
self.check_update_time(recording)
def check_update_time(self, entity):
print("last_updated:")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment