def update_user(user_id, username): with INSERTION_LOCK: user = SESSION.query(Users).get(user_id) if not user: user = Users(user_id, username) SESSION.add(user) SESSION.flush() else: user.username = username SESSION.commit()
def update_creds(user_id, spotify_id=None, spotify_access_token=None, spotify_refresh_token=None): with SPT_INSERTION_LOCK: sptcreds = SESSION.query(SpotifyCreds).get(user_id) if not sptcreds: sptcreds = SpotifyCreds(user_id, spotify_id, spotify_access_token, spotify_refresh_token) SESSION.add(sptcreds) SESSION.flush() else: sptcreds.spotify_id = spotify_id sptcreds.spotify_access_token = spotify_access_token sptcreds.spotify_refresh_token = spotify_refresh_token SESSION.commit()
def get_sptuser(user_id): try: return SESSION.query(SpotifyCreds).get(user_id) finally: SESSION.close()
def users_count(): try: return SESSION.query(Users).count() finally: SESSION.close()
def get_all_users(): try: return SESSION.query(Users).all() finally: SESSION.close()
def ensure_bot_in_db(): with INSERTION_LOCK: bot = Users(dp.bot.id, dp.bot.username) SESSION.merge(bot) SESSION.commit()