1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 17:06:17 +00:00

Set Meta to use the same connection as other tables, and cache generated/retrieved secret keys

This commit is contained in:
Alban Féron 2020-01-19 16:45:31 +01:00
parent aa6ca54dc1
commit acb6b773a9
No known key found for this signature in database
GPG Key ID: 8CE0313646D16165
3 changed files with 27 additions and 24 deletions

View File

@ -3,7 +3,7 @@
# This file is part of Supysonic. # This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API. # Supysonic is a Python implementation of the Subsonic server API.
# #
# Copyright (C) 2013-2019 Alban 'spl0k' Féron # Copyright (C) 2013-2020 Alban 'spl0k' Féron
# #
# Distributed under terms of the GNU AGPLv3 license. # Distributed under terms of the GNU AGPLv3 license.
@ -645,9 +645,12 @@ def init_database(database_uri):
else: else:
metadb.disconnect() metadb.disconnect()
db.bind(**settings) db.bind(**settings)
# Force requests to Meta to use the same connection as other tables
metadb.provider = db.provider
db.generate_mapping(check_tables=False) db.generate_mapping(check_tables=False)
def release_database(): def release_database():
metadb.disconnect() metadb.disconnect()
db.disconnect() db.disconnect()

View File

@ -3,20 +3,25 @@
# This file is part of Supysonic. # This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API. # Supysonic is a Python implementation of the Subsonic server API.
# #
# Copyright (C) 2019 Alban 'spl0k' Féron # Copyright (C) 2019-2020 Alban 'spl0k' Féron
# #
# Distributed under terms of the GNU AGPLv3 license. # Distributed under terms of the GNU AGPLv3 license.
import uuid
from base64 import b64encode, b64decode from base64 import b64encode, b64decode
from os import urandom from os import urandom
from pony.orm import db_session, commit, ObjectNotFound from pony.orm import db_session, commit, ObjectNotFound
from supysonic.db import Folder, Meta from supysonic.db import Meta
__key_cache = {}
@db_session
def get_secret_key(keyname): def get_secret_key(keyname):
if keyname in __key_cache:
return __key_cache[keyname]
with db_session():
# Commit both at enter and exit. The metadb/db split (from supysonic.db) # Commit both at enter and exit. The metadb/db split (from supysonic.db)
# confuses Pony which can either error or hang when this method is called # confuses Pony which can either error or hang when this method is called
commit() commit()
@ -26,4 +31,6 @@ def get_secret_key(keyname):
key = urandom(128) key = urandom(128)
Meta(key=keyname, value=b64encode(key).decode()) Meta(key=keyname, value=b64encode(key).decode())
commit() commit()
__key_cache[keyname] = key
return key return key

View File

@ -29,8 +29,8 @@ class ScannerTestCase(unittest.TestCase):
with db_session: with db_session:
folder = FolderManager.add("folder", os.path.abspath("tests/assets/folder")) folder = FolderManager.add("folder", os.path.abspath("tests/assets/folder"))
self.assertIsNotNone(folder) self.assertIsNotNone(folder)
self.folderid = folder.id
self.folderid = folder.id
self.__scan() self.__scan()
def tearDown(self): def tearDown(self):
@ -48,6 +48,7 @@ class ScannerTestCase(unittest.TestCase):
self.scanner = Scanner(force=force) self.scanner = Scanner(force=force)
self.scanner.queue_folder("folder") self.scanner.queue_folder("folder")
self.scanner.run() self.scanner.run()
commit()
@db_session @db_session
def test_scan(self): def test_scan(self):
@ -61,13 +62,11 @@ class ScannerTestCase(unittest.TestCase):
@db_session @db_session
def test_rescan(self): def test_rescan(self):
self.__scan() self.__scan()
commit()
self.assertEqual(db.Track.select().count(), 1) self.assertEqual(db.Track.select().count(), 1)
@db_session @db_session
def test_force_rescan(self): def test_force_rescan(self):
self.__scan(True) self.__scan(True)
commit()
self.assertEqual(db.Track.select().count(), 1) self.assertEqual(db.Track.select().count(), 1)
@db_session @db_session
@ -115,7 +114,6 @@ class ScannerTestCase(unittest.TestCase):
with self.__temporary_track_copy() as tf: with self.__temporary_track_copy() as tf:
self.__scan() self.__scan()
commit()
self.assertEqual(db.Track.select().count(), 2) self.assertEqual(db.Track.select().count(), 2)
self.scanner.move_file(tf.name, track.path) self.scanner.move_file(tf.name, track.path)
commit() commit()
@ -134,7 +132,6 @@ class ScannerTestCase(unittest.TestCase):
with self.__temporary_track_copy() as tf: with self.__temporary_track_copy() as tf:
self.__scan() self.__scan()
commit()
self.assertEqual(db.Track.select().count(), 2) self.assertEqual(db.Track.select().count(), 2)
tf.seek(0, 0) tf.seek(0, 0)
@ -142,7 +139,6 @@ class ScannerTestCase(unittest.TestCase):
tf.truncate() tf.truncate()
self.__scan(True) self.__scan(True)
commit()
self.assertEqual(db.Track.select().count(), 1) self.assertEqual(db.Track.select().count(), 1)
@db_session @db_session
@ -151,11 +147,9 @@ class ScannerTestCase(unittest.TestCase):
with self.__temporary_track_copy() as tf: with self.__temporary_track_copy() as tf:
self.__scan() self.__scan()
commit()
self.assertEqual(db.Track.select().count(), 2) self.assertEqual(db.Track.select().count(), 2)
self.__scan() self.__scan()
commit()
self.assertEqual(db.Track.select().count(), 1) self.assertEqual(db.Track.select().count(), 1)
@db_session @db_session
@ -164,7 +158,6 @@ class ScannerTestCase(unittest.TestCase):
with self.__temporary_track_copy() as tf: with self.__temporary_track_copy() as tf:
self.__scan() self.__scan()
commit()
copy = db.Track.get(path=tf.name) copy = db.Track.get(path=tf.name)
self.assertEqual(copy.artist.name, "Some artist") self.assertEqual(copy.artist.name, "Some artist")
self.assertEqual(copy.album.name, "Awesome album") self.assertEqual(copy.album.name, "Awesome album")