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)
Calendars.create_table(True) Message.create_table(True) Locations.create_table(True) Call.create_table(True) Voicemail.create_table(True) Word.create_table(True) Jobs.create_table(True) Contacts.create_table(True) SocialMedia.create_table(True) Photos.create_table(True) Sleep.create_table(True) Activity.create_table(True) Heart.create_table(True) Document.create_table(True) database.close() # Have to do this because when the command is called from the import in any subfolder it cannot find the dbconfig if __name__ != "__main__": print(__name__) if __name__ == "databaseSetup": with open(os.path.join("..", "constants.yaml"), 'r') as ymlfile: config = yaml.load(ymlfile) else: with open(os.path.join("..", "constants.yaml"), 'r') as ymlfile: config = yaml.load(ymlfile) else: with open("constants.yaml", 'r') as ymlfile: config = yaml.load(ymlfile) ''' As of right now, this should create a database in each folder the script is run, to then combine them later Peewee seems to have a problem with connecting to a database that is not in the current folder