def __create_emoji(emoji:Union[discord.Emoji, discord.PartialEmoji, str], guild:discord.Guild) -> Karma: if not GuildRepo.guild_exist(guild.id): GuildRepo.create_guild(guild) emoji_id = general_util.emoji_id_from_emoji(emoji) if not EmojiDataRepo.emoji_data_exist(emoji_id): EmojiDataRepo.create_emoji_data(emoji) with emoji_access_lock: item = Karma_emoji(emoji_ID=emoji_id, guild_id=str(guild.id)) session.add(item) session.commit() return item
def set_emoji_value(self, emoji_id: str, value: int): emoji = Karma_emoji(emoji_ID=utils.str_emoji_id(emoji_id), value=str(value)) # Merge == 'insert on duplicate key update' session.merge(emoji) session.commit()
def load_dump(filename: str): init_db(False) session.query(Karma).delete() session.query(Karma_emoji).delete() session.query(Permit).delete() session.query(Valid_person).delete() session.commit() print(f'Loading dump from {filename}') data = database.base.metadata.tables.keys() for row in data: print(row) with open(filename, "r", encoding='utf-8') as backup_file: data = backup_file.readlines() inserts = [line for line in data if line.startswith("INSERT")] karma_values = [] for insert in inserts: values = insert.split("VALUES", 1)[1] if insert.startswith("INSERT INTO `bot_karma`"): values = values[1:-2].replace('\'', '') values = values.replace('(', '').replace(')', '') values = values.split(',') for i in range(0, len(values), 3): karma_values.append( Karma(member_ID=values[i], karma=values[i + 1])) elif insert.startswith("INSERT INTO `bot_karma_giving`"): values = values[1:-2].replace('\'', '') values = values.replace('(', '').replace(')', '') values = values.split(',') for i in range(0, len(values), 4): karma_values.append( Karma(member_ID=values[i], positive=values[i + 1], negative=values[i + 2])) elif insert.startswith("INSERT INTO `bot_karma_emoji`"): values = values[1:-2].replace('\'', '') values = values.replace('(', '').replace(')', '') values = values.split(',') for i in range(0, len(values), 2): session.add( Karma_emoji(emoji_ID=values[i], value=values[i + 1])) elif insert.startswith("INSERT INTO `bot_permit`"): values = values[1:-2] values = values.replace('(', '').replace(')', '') values = re.split(r',(?=\')', values) values = [value.replace('\'', '') for value in values] for i in range(0, len(values), 3): session.add(Permit(login=values[i], discord_ID=values[i + 2])) elif insert.startswith("INSERT INTO `bot_valid_persons`"): values = values[1:-2].replace('\'', '') values = values.replace('(', '').replace(')', '') values = values.split(',') for i in range(0, len(values), 5): session.add( Valid_person( login=values[i], name=values[i + 1], year=values[i + 2], code=values[i + 3] if values[i + 3] != "NULL" else None, status=values[i + 4])) for karma in karma_values: session.merge(karma) session.commit()