1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 08:56: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.
# 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.
@ -645,9 +645,12 @@ def init_database(database_uri):
else:
metadb.disconnect()
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)
def release_database():
metadb.disconnect()
db.disconnect()

View File

@ -3,20 +3,25 @@
# This file is part of Supysonic.
# 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.
import uuid
from base64 import b64encode, b64decode
from os import urandom
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):
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)
# confuses Pony which can either error or hang when this method is called
commit()
@ -26,4 +31,6 @@ def get_secret_key(keyname):
key = urandom(128)
Meta(key=keyname, value=b64encode(key).decode())
commit()
__key_cache[keyname] = key
return key

View File

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