def initiate_db(filepath, db_type: str, num_of_dummy_inserts: int = 10) -> DB_general: # make sure we can access filepath if not filepath.parent.exists(): filepath.parent.mkdir(parents=True, exist_ok=True) db = DB_general(filepath) for name, table_dict in table_data.items(): if name == db_type: tables = table_dict type_tuple = type_tuples[name] break else: raise Exception('Invalid db type') # create tables for table, column_data in tables.items(): db.create_table(table, column_data) # insert dummy data to tables ph = cs.PasswordHasher() if 'hash' in type_tuple else None f = cs.Fernet(cs.Fernet.generate_key()) if 'key' in type_tuple else None for table, column_data in tables.items(): t_tuple = type_tuple[:len(column_data.keys())] dummy_data = generate_dummy_data(t_tuple, num_of_dummy_inserts, f, ph) db.insert_many(table, column_data.keys(), dummy_data) return db
def default_paths(paths): def_paths = get_paths_from_str(paths, 0) # make sure the files exist if not def_paths[0].exists(): generate_salt(def_paths[0]) for path in def_paths[1:]: if not path.exists(): DB_general(path) yield def_paths # remove files after some time for path in def_paths: if path.exists(): path.unlink()
def db_backup(): db_path = Path(__file__).parent / 'test_db_backup.db' yield DB_general(db_path) db_path.unlink()