diff --git a/discordbot/cleanup_db_messages.py b/discordbot/cleanup_db_messages.py index 284a89a..f9a28cf 100644 --- a/discordbot/cleanup_db_messages.py +++ b/discordbot/cleanup_db_messages.py @@ -80,10 +80,9 @@ class Titan(discord.Client): continue for channel in channelsjson: chanid = channel["id"] - msgs = session.query(Messages).filter(Messages.channel_id == chanid).order_by(Messages.timestamp.desc()).offset(50).all() - for msg in msgs: - session.delete(msg) - session.commit() + keep_these = session.query(Messages.id).filter(Messages.channel_id == chanid).order_by(Messages.timestamp.desc()).limit(50) + session.query(Messages).filter(Messages.channel_id == chanid, ~Messages.id.in_(keep_these)).delete(synchronize_session=False) + session.commit() print("done!") await self.logout() diff --git a/webapp/titanembeds/blueprints/api/api.py b/webapp/titanembeds/blueprints/api/api.py index 9a72272..a5f474d 100644 --- a/webapp/titanembeds/blueprints/api/api.py +++ b/webapp/titanembeds/blueprints/api/api.py @@ -436,18 +436,16 @@ def cleanup_keyval_db(): db.session.query(KeyValueProperties).filter(KeyValueProperties.expiration < datetime.datetime.now()).delete() db.session.commit() - # guilds = Guilds.query.all() - # for guild in guilds: - # try: - # channelsjson = json.loads(guild.channels) - # except: - # continue - # for channel in channelsjson: - # chanid = channel["id"] - # msgs = db.session.query(Messages).filter(Messages.channel_id == chanid).order_by(Messages.timestamp.desc()).offset(50).all() - # for msg in msgs: - # db.session.delete(msg) - # db.session.commit() - + guilds = Guilds.query.all() + for guild in guilds: + try: + channelsjson = json.loads(guild.channels) + except: + continue + for channel in channelsjson: + chanid = channel["id"] + keep_these = db.session.query(Messages.id).filter(Messages.channel_id == chanid).order_by(Messages.timestamp.desc()).limit(50) + db.session.query(Messages).filter(Messages.channel_id == chanid, ~Messages.id.in_(keep_these)).delete(synchronize_session=False) + db.session.commit() return ('', 204) abort(401)