#!/usr/bin/env python3 """ Makes all existing filepaths in the database relative to `config.music_dir`. """ import os import asyncio import asyncpg import config async def fix_fpaths(): """ Makes all existing filepaths in the database relative to `config.music_dir`. """ db_pool = await asyncpg.create_pool(**config.db) async with db_pool.acquire() as conn: tracks = await conn.fetch("SELECT filepath FROM track") data = [] for track in tracks: fpath = track['filepath'] fpath = os.path.relpath(fpath, config.music_dir) entry = (fpath, track['filepath']) data.append(entry) stm = await conn.prepare("UPDATE track SET filepath = $1 WHERE filepath = $2") await stm.executemany(data) print("Done.") if __name__ == '__main__': asyncio.run(fix_fpaths())