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 init_db(): _connect_db(_db) sqlite_version = ".".join([str(num) for num in _db.server_version]) log.info("SQLite version: {}".format(sqlite_version)) if Settings.table_exists(): update_db() else: _db.create_tables([ Track, Book, Settings, ArtworkCache, Storage, StorageBlackList, OfflineCache ]) _db.stop() _db.start() while not _db.table_exists("settings"): time.sleep(0.01) _db.bind([ Book, Track, Settings, ArtworkCache, StorageBlackList, OfflineCache, Storage ], bind_refs=False, bind_backrefs=False) if (Settings.select().count() == 0): Settings.create(path="", last_played_book=None) # TODO: Properly handle errors within the database # Remove this later. It prevents empty book objects in the database clean_books()
def init_db(): tmp_db = None _connect_db(_db) sqlite_version = ".".join([str(num) for num in _db.server_version]) log.info("SQLite version: {}, APSW version: {}".format( sqlite_version, apswversion())) if Settings.table_exists(): update_db() else: tmp_db = PooledSqliteDatabase(os.path.join(get_data_dir(), "cozy.db")) if PeeweeVersion[0] == '2': tmp_db.create_tables([ Track, Book, Settings, ArtworkCache, Storage, StorageBlackList, OfflineCache ], True) else: with tmp_db.connection_context(): tmp_db.create_tables([ Track, Book, Settings, ArtworkCache, Storage, StorageBlackList, OfflineCache ]) # this is necessary to ensure that the tables have indeed been created if tmp_db: if PeeweeVersion[0] == '2': while not Settings.table_exists(): time.sleep(0.01) else: while not tmp_db.table_exists("settings"): time.sleep(0.01) _connect_db(_db) if PeeweeVersion[0] == '3': _db.bind([ Book, Track, Settings, ArtworkCache, StorageBlackList, OfflineCache, Storage ], bind_refs=False, bind_backrefs=False) if (Settings.select().count() == 0): Settings.create(path="", last_played_book=None) # TODO: Properly handle errors within the database # Remove this later. It prevents empty book objects in the database clean_books()
def peewee_database_storage(): from cozy.db.storage import Storage from cozy.db.settings import Settings from cozy.db.storage_blacklist import StorageBlackList db_path, models, test_db = prepare_db() path_of_test_folder = os.path.dirname(os.path.realpath(__file__)) + '/' 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=None) 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)