faster remove

This commit is contained in:
j 2016-02-11 23:25:46 +05:30
parent 83ceb5b63f
commit a71dab1c91
3 changed files with 15 additions and 13 deletions

View file

@ -104,6 +104,15 @@ class Item(db.Model):
cls.query.filter(cls.id.in_(ids)).delete(synchronize_session=False)
state.db.session.expire_all()
@classmethod
def remove_without_user(cls):
q = user_items.select()
owned_ids = {i['item_id'] for i in state.db.session.execute(q)}
ids = {i.id for i in cls.query.options(load_only('id'))}
remove = ids - owned_ids
if remove:
cls.remove_many(remove)
def add_user(self, user):
if not user in self.users:
self.users.append(user)

View file

@ -326,7 +326,6 @@ def sync_db():
cleanup_lists()
def cleanup_lists():
from sqlalchemy.orm import load_only
import item.models
import user.models
with db.session():
@ -344,10 +343,5 @@ def cleanup_lists():
state.db.session.commit()
q = item.models.user_items.select()
user_items = {i['item_id'] for i in state.db.session.execute(q)}
ids = {i.id for i in item.models.Item.query.options(load_only('id'))}
remove = ids - user_items
if remove:
item.models.Item.remove_many(remove)
item.models.Item.remove_without_user()
state.db.session.commit()

View file

@ -180,13 +180,12 @@ class User(db.Model):
self.save()
def cleanup(self):
from item.models import user_items, Item
List.query.filter_by(user_id=self.id).delete()
for i in self.items:
i.users.remove(self)
if not i.users:
i.delete()
Changelog.query.filter_by(user_id=self.id).delete()
Metadata.query.filter_by(user_id=self.id).delete()
c_user_id = user_items.columns['user_id']
q = user_items.delete().where(c_user_id.is_(self.id))
state.db.session.execute(q)
Item.remove_without_user()
self.save()
if self.id in state.peers:
state.peers[self.id].remove()