mirror of
https://github.com/krateng/maloja.git
synced 2023-08-10 21:12:55 +03:00
Added more connection passing
This commit is contained in:
parent
8845f931df
commit
cf04583122
|
@ -442,7 +442,7 @@ def merge_tracks(target_id,source_ids,dbconn=None):
|
||||||
track_id=target_id
|
track_id=target_id
|
||||||
)
|
)
|
||||||
result = dbconn.execute(op)
|
result = dbconn.execute(op)
|
||||||
clean_db()
|
clean_db(dbconn=dbconn)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -491,8 +491,8 @@ def merge_artists(target_id,source_ids,dbconn=None):
|
||||||
# result = dbconn.execute(op)
|
# result = dbconn.execute(op)
|
||||||
|
|
||||||
# this could have created duplicate tracks
|
# this could have created duplicate tracks
|
||||||
merge_duplicate_tracks(artist_id=target_id)
|
merge_duplicate_tracks(artist_id=target_id,dbconn=dbconn)
|
||||||
clean_db()
|
clean_db(dbconn=dbconn)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -871,38 +871,37 @@ def search_track(searchterm,dbconn=None):
|
||||||
##### MAINTENANCE
|
##### MAINTENANCE
|
||||||
|
|
||||||
@runhourly
|
@runhourly
|
||||||
def clean_db():
|
@connection_provider
|
||||||
|
def clean_db(dbconn=None):
|
||||||
|
|
||||||
with SCROBBLE_LOCK:
|
log(f"Database Cleanup...")
|
||||||
with engine.begin() as conn:
|
|
||||||
log(f"Database Cleanup...")
|
|
||||||
|
|
||||||
to_delete = [
|
to_delete = [
|
||||||
# tracks with no scrobbles (trackartist entries first)
|
# tracks with no scrobbles (trackartist entries first)
|
||||||
"from trackartists where track_id in (select id from tracks where id not in (select track_id from scrobbles))",
|
"from trackartists where track_id in (select id from tracks where id not in (select track_id from scrobbles))",
|
||||||
"from tracks where id not in (select track_id from scrobbles)",
|
"from tracks where id not in (select track_id from scrobbles)",
|
||||||
# artists with no tracks
|
# artists with no tracks
|
||||||
"from artists where id not in (select artist_id from trackartists) and id not in (select target_artist from associated_artists)",
|
"from artists where id not in (select artist_id from trackartists) and id not in (select target_artist from associated_artists)",
|
||||||
# tracks with no artists (scrobbles first)
|
# tracks with no artists (scrobbles first)
|
||||||
"from scrobbles where track_id in (select id from tracks where id not in (select track_id from trackartists))",
|
"from scrobbles where track_id in (select id from tracks where id not in (select track_id from trackartists))",
|
||||||
"from tracks where id not in (select track_id from trackartists)"
|
"from tracks where id not in (select track_id from trackartists)"
|
||||||
]
|
]
|
||||||
|
|
||||||
for d in to_delete:
|
for d in to_delete:
|
||||||
selection = conn.execute(sql.text(f"select * {d}"))
|
selection = dbconn.execute(sql.text(f"select * {d}"))
|
||||||
for row in selection.all():
|
for row in selection.all():
|
||||||
log(f"Deleting {row}")
|
log(f"Deleting {row}")
|
||||||
deletion = conn.execute(sql.text(f"delete {d}"))
|
deletion = dbconn.execute(sql.text(f"delete {d}"))
|
||||||
|
|
||||||
log("Database Cleanup complete!")
|
log("Database Cleanup complete!")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if a2+a1>0: log(f"Deleted {a2} tracks without scrobbles ({a1} track artist entries)")
|
#if a2+a1>0: log(f"Deleted {a2} tracks without scrobbles ({a1} track artist entries)")
|
||||||
|
|
||||||
#if a3>0: log(f"Deleted {a3} artists without tracks")
|
#if a3>0: log(f"Deleted {a3} artists without tracks")
|
||||||
|
|
||||||
#if a5+a4>0: log(f"Deleted {a5} tracks without artists ({a4} scrobbles)")
|
#if a5+a4>0: log(f"Deleted {a5} tracks without artists ({a4} scrobbles)")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -923,40 +922,39 @@ def renormalize_names():
|
||||||
rows = conn.execute(DB['artists'].update().where(DB['artists'].c.id == id).values(name_normalized=norm_target))
|
rows = conn.execute(DB['artists'].update().where(DB['artists'].c.id == id).values(name_normalized=norm_target))
|
||||||
|
|
||||||
|
|
||||||
|
@connection_provider
|
||||||
def merge_duplicate_tracks(artist_id):
|
def merge_duplicate_tracks(artist_id,dbconn=None):
|
||||||
with engine.begin() as conn:
|
rows = dbconn.execute(
|
||||||
rows = conn.execute(
|
DB['trackartists'].select().where(
|
||||||
DB['trackartists'].select().where(
|
DB['trackartists'].c.artist_id == artist_id
|
||||||
DB['trackartists'].c.artist_id == artist_id
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
affected_tracks = [r.track_id for r in rows]
|
)
|
||||||
|
affected_tracks = [r.track_id for r in rows]
|
||||||
|
|
||||||
track_artists = {}
|
track_artists = {}
|
||||||
rows = conn.execute(
|
rows = dbconn.execute(
|
||||||
DB['trackartists'].select().where(
|
DB['trackartists'].select().where(
|
||||||
DB['trackartists'].c.track_id.in_(affected_tracks)
|
DB['trackartists'].c.track_id.in_(affected_tracks)
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
for row in rows:
|
for row in rows:
|
||||||
track_artists.setdefault(row.track_id,[]).append(row.artist_id)
|
track_artists.setdefault(row.track_id,[]).append(row.artist_id)
|
||||||
|
|
||||||
artist_combos = {}
|
artist_combos = {}
|
||||||
for track_id in track_artists:
|
for track_id in track_artists:
|
||||||
artist_combos.setdefault(tuple(sorted(track_artists[track_id])),[]).append(track_id)
|
artist_combos.setdefault(tuple(sorted(track_artists[track_id])),[]).append(track_id)
|
||||||
|
|
||||||
for c in artist_combos:
|
for c in artist_combos:
|
||||||
if len(artist_combos[c]) > 1:
|
if len(artist_combos[c]) > 1:
|
||||||
track_identifiers = {}
|
track_identifiers = {}
|
||||||
for track_id in artist_combos[c]:
|
for track_id in artist_combos[c]:
|
||||||
track_identifiers.setdefault(normalize_name(get_track(track_id)['title']),[]).append(track_id)
|
track_identifiers.setdefault(normalize_name(get_track(track_id)['title']),[]).append(track_id)
|
||||||
for track in track_identifiers:
|
for track in track_identifiers:
|
||||||
if len(track_identifiers[track]) > 1:
|
if len(track_identifiers[track]) > 1:
|
||||||
target,*src = track_identifiers[track]
|
target,*src = track_identifiers[track]
|
||||||
merge_tracks(target,src)
|
merge_tracks(target,src)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user