Cleanup unused media_data tables and migration info.
authorElrond <elrond+mediagoblin.org@samba-tng.org>
Mon, 2 Apr 2012 11:42:02 +0000 (13:42 +0200)
committerElrond <elrond+mediagoblin.org@samba-tng.org>
Mon, 2 Apr 2012 11:51:52 +0000 (13:51 +0200)
After converting everything, check what is actually used in
the db. For media_types that are not used, drop all the
media_data tables and remove the migration info.

mediagoblin/db/sql/convert.py

index 2589d94319ded919be0e1f2b99e40c71d642100a..0690fa4cb78b8d92f5d813d316d2854dde7aeea3 100644 (file)
@@ -15,6 +15,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from copy import copy
+from itertools import chain, imap
 
 from mediagoblin.init import setup_global_and_app_config
 
@@ -191,13 +192,18 @@ def convert_media_comments(mk_db):
     session.close()
 
 
-def convert_add_migration_versions():
+media_types_tables = (
+    ("mediagoblin.media_types.image", (ImageData,)),
+    ("mediagoblin.media_types.video", (VideoData,)),
+    )
+
+
+def convert_add_migration_versions(dummy_sql_db):
     session = Session()
 
-    for name in ("__main__",
-                 "mediagoblin.media_types.image",
-                 "mediagoblin.media_types.video",
-                 ):
+    for name in chain(("__main__",),
+                      imap(lambda e: e[0], media_types_tables)):
+        print "\tAdding %s" % (name,)
         m = MigrationData(name=unicode(name), version=0)
         session.add(m)
 
@@ -205,6 +211,27 @@ def convert_add_migration_versions():
     session.close()
 
 
+def cleanup_sql_tables(sql_db):
+    for mt, table_list in media_types_tables:
+        session = Session()
+
+        count = session.query(MediaEntry.media_type). \
+            filter_by(media_type=unicode(mt)).count()
+        print "  %s: %d entries" % (mt, count)
+        if count == 0:
+            print "\tRemoving migration info"
+            mi = session.query(MigrationData).filter_by(name=unicode(mt)).one()
+            session.delete(mi)
+            session.commit()
+            session.close()
+
+            print "\tDropping tables %r" % (table_list,)
+            tables = [model.__table__ for model in table_list]
+            Base_v0.metadata.drop_all(sql_db.engine, tables=tables)
+
+        session.close()
+
+
 def print_header(title):
     print "\n=== %s ===" % (title,)
 
@@ -220,6 +247,7 @@ convert_call_list = (
 
 sql_call_list = (
     ("Filling Migration Tables", convert_add_migration_versions),
+    ("Analyzing/Cleaning SQL Data", cleanup_sql_tables),
     )
 
 def run_conversion(config_name):
@@ -237,7 +265,7 @@ def run_conversion(config_name):
     
     for title, func in sql_call_list:
         print_header(title)
-        func()
+        func(sql_db)
         Session.remove()