def write_tracks_to_db(tracks): """ """ if tracks is None or len(tracks) < 1: return if PeeweeVersion[0] == '2': data = list({ "name": t.name, "number": t.track_number, "disk": t.disk, "position": t.position, "book": t.book, "file": t.file, "length": t.length, "modified": t.modified } for t in tracks) Track.insert_many(data).execute() else: fields = [ Track.name, Track.number, Track.disk, Track.position, Track.book, Track.file, Track.length, Track.modified ] data = list((t.name, t.track_number, t.disk, t.position, t.book, t.file, t.length, t.modified) for t in tracks) Track.insert_many(data, fields=fields).execute()
def peewee_database(): from cozy.db.track import Track from cozy.db.book import Book from cozy.db.settings import Settings from cozy.db.storage_blacklist import StorageBlackList from cozy.db.storage import Storage db_path, models, test_db = prepare_db() path_of_test_folder = os.path.dirname(os.path.realpath(__file__)) + '/' with open(path_of_test_folder + 'books.json') as json_file: book_data = json.load(json_file) with open(path_of_test_folder + 'tracks.json') as json_file: track_data = json.load(json_file) Book.insert_many(book_data).execute() for chunk in chunks(track_data, 25): Track.insert_many(chunk).execute() with open(path_of_test_folder + 'storages.json') as json_file: storage_data = json.load(json_file) Storage.insert_many(storage_data).execute() Settings.create(path="", last_played_book=Book.get()) StorageBlackList.create(path="/path/to/replace/test1.mp3") StorageBlackList.create(path="/path/to/not/replace/test2.mp3") print("Provide database...") yield test_db teardown_db(db_path, models, test_db)
def peewee_database(): from playhouse.apsw_ext import APSWDatabase from cozy.db.artwork_cache import ArtworkCache from cozy.db.book import Book from cozy.db.offline_cache import OfflineCache from cozy.db.settings import Settings from cozy.db.storage import Storage from cozy.db.storage_blacklist import StorageBlackList from cozy.db.track import Track models = [ Track, Book, Settings, ArtworkCache, Storage, StorageBlackList, OfflineCache ] print("Setup database...") db_path = '/tmp/cozy_test.db' test_db = APSWDatabase(db_path, pragmas=[('journal_mode', 'wal')]) test_db.bind(models, bind_refs=False, bind_backrefs=False) test_db.connect() test_db.create_tables(models) path_of_test_folder = os.path.dirname(os.path.realpath(__file__)) + '/' with open(path_of_test_folder + 'books.json') as json_file: book_data = json.load(json_file) with open(path_of_test_folder + 'tracks.json') as json_file: track_data = json.load(json_file) Book.insert_many(book_data).execute() for chunk in chunks(track_data, 25): Track.insert_many(chunk).execute() print("Provide database...") yield test_db print("Teardown database...") test_db.drop_tables(models) test_db.close() os.remove(db_path)
def insert_many(self, media_files: Set[MediaFile]): tracks = self._prepare_db_objects(media_files) with self._db: Track.insert_many(tracks).execute()