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) cls.query.filter(cls.id.in_(ids)).delete(synchronize_session=False)
state.db.session.expire_all() 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): def add_user(self, user):
if not user in self.users: if not user in self.users:
self.users.append(user) self.users.append(user)

View file

@ -326,7 +326,6 @@ def sync_db():
cleanup_lists() cleanup_lists()
def cleanup_lists(): def cleanup_lists():
from sqlalchemy.orm import load_only
import item.models import item.models
import user.models import user.models
with db.session(): with db.session():
@ -344,10 +343,5 @@ def cleanup_lists():
state.db.session.commit() state.db.session.commit()
q = item.models.user_items.select() item.models.Item.remove_without_user()
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)
state.db.session.commit() state.db.session.commit()

View file

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