From: Elrond Date: Tue, 24 Jan 2012 22:03:33 +0000 (+0100) Subject: SQL: Tool to cleanup unused tag slugs. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c65cbf9514278fd624ff6f1ccbac93cca8e31764;p=mediagoblin.git SQL: Tool to cleanup unused tag slugs. The current SQL layout/sqlalchemy strucuture can't detect whether a slug isn't needed any more and delete it. So provide a tool function to cleanup unused slugs. It's currently not hooked to any gmg function! --- diff --git a/mediagoblin/db/sql/util.py b/mediagoblin/db/sql/util.py index 13bc97e1..737e4e59 100644 --- a/mediagoblin/db/sql/util.py +++ b/mediagoblin/db/sql/util.py @@ -17,6 +17,7 @@ import sys from mediagoblin.db.sql.base import Session +from mediagoblin.db.sql.models import Tag, MediaTag def _simple_printer(string): @@ -282,3 +283,24 @@ def atomic_update(table, query_dict, update_values): table.find(query_dict).update(update_values, synchronize_session=False) Session.commit() + + +def clean_orphan_tags(): + q1 = Session.query(Tag).outerjoin(MediaTag).filter(MediaTag.id==None) + for t in q1: + Session.delete(t) + + # The "let the db do all the work" version: + # q1 = Session.query(Tag.id).outerjoin(MediaTag).filter(MediaTag.id==None) + # q2 = Session.query(Tag).filter(Tag.id.in_(q1)) + # q2.delete(synchronize_session = False) + + Session.commit() + + +if __name__ == '__main__': + from mediagoblin.db.sql.open import setup_connection_and_db_from_config + + conn,db = setup_connection_and_db_from_config({'sql_engine':'sqlite:///mediagoblin.db'}) + + clean_orphan_tags()