Work around MariaDB bug in replace_files.py

This commit is contained in:
Keith Edmunds 2022-08-22 14:39:18 +01:00
parent 5f8d8572ad
commit 62c5fa178c
2 changed files with 29 additions and 17 deletions

View File

@ -572,12 +572,15 @@ class Tracks(Base):
Return track with passed path, or None.
"""
return (
session.execute(
select(Tracks)
.where(Tracks.path == path)
).scalar_one()
)
try:
return (
session.execute(
select(Tracks)
.where(Tracks.path == path)
).scalar_one()
)
except NoResultFound:
return None
def rescan(self, session: Session) -> None:
"""

View File

@ -27,15 +27,14 @@ from helpers import (
from models import Tracks
from dbconfig import Session
from thefuzz import process
from string import ascii_lowercase as letters
from thefuzz import process # type: ignore
from sqlalchemy.exc import IntegrityError
from typing import List
# ###################### SETTINGS #########################
process_multiple_matches = True
process_multiple_matches = False
do_processing = True
process_no_matches = True
process_no_matches = False
# #########################################################
@ -202,7 +201,10 @@ def main():
def process_track(src, dst, title, artist):
new_path = os.path.join(os.path.dirname(dst), os.path.basename(src))
print(f"process_track:\n {src=}\n {new_path=}\n {dst=}\n {title=}, {artist=}\n")
print(
f"process_track:\n {src=}\n {new_path=}\n "
f"{dst=}\n {title=}, {artist=}\n"
)
if not do_processing:
return
@ -213,17 +215,24 @@ def process_track(src, dst, title, artist):
track.title = title
track.artist = artist
track.path = new_path
session.commit()
try:
session.commit()
except IntegrityError:
# https://jira.mariadb.org/browse/MDEV-29345 workaround
session.rollback()
track.title = title
track.artist = artist
track.path = "DUMMY"
session.commit()
track.path = new_path
session.commit()
print(f"os.unlink({dst}")
print(f"shutil.move({src}, {new_path}")
os.unlink(dst)
shutil.move(src, new_path)
try:
track = Tracks.get_by_path(session, new_path)
except:
import ipdb; ipdb.set_trace()
track = Tracks.get_by_path(session, new_path)
if track:
track.rescan(session)
else: