Permalink
Browse files

Add deduper for people bitten by my bad db from before, use at own risk

  • Loading branch information...
1 parent b4b8136 commit 22e508a3a2b9d92ec66059ac191034f897e748a5 @mitechie committed Jun 28, 2012
Showing with 59 additions and 1 deletion.
  1. +55 −0 scripts/admin/deduper.py
  2. +4 −1 scripts/admin/usermgr.py
View
55 scripts/admin/deduper.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+"""We weren't good and got duplicate bookmarks for users.
+
+This will go through and find bookmarks, per user, with the same hash_id,
+combine the tags from one onto the other, and then remove the dupe.
+
+"""
+import transaction
+
+from collections import defaultdict
+from ConfigParser import ConfigParser
+from os import path
+
+from bookie.models import initialize_sql
+if __name__ == "__main__":
+ ini = ConfigParser()
+ ini_path = path.join(path.dirname(path.dirname(path.dirname(__file__))),
+ 'bookie.ini')
+
+ ini.readfp(open(ini_path))
+ initialize_sql(dict(ini.items("app:main")))
+
+ from bookie.models import DBSession
+ from bookie.models import Bmark
+
+ bookmarks = Bmark.query.all()
+ index = defaultdict(list)
+ to_delete = []
+ for b in bookmarks:
+ key = (b.username, b.hash_id)
+ index[key].append(b)
+
+ for k, v in index.iteritems():
+ if len(v) > 1:
+ first = v[0]
+ second = v[1]
+
+ print first.hash_id
+ to_delete.append(first)
+ for name, tag in first.tags.items():
+ if name in second.tags:
+ print "HAS IT"
+ else:
+ print "ADDING" + name
+ second.tags[name] = tag
+
+ assert len(first.tags) <= len(second.tags)
+
+ print to_delete
+ print len(to_delete)
+ for b in to_delete:
+ DBSession.delete(b)
+
+ DBSession.flush()
+ transaction.commit()
View
5 scripts/admin/usermgr.py
@@ -13,6 +13,7 @@
from bookie.models.auth import User
from bookie.models.auth import UserMgr
+
def parse_args():
"""Handle arguments building and processing"""
desc = "Update existing bookbmarks with the readability parsed"
@@ -64,10 +65,12 @@ def parse_args():
args = parser.parse_args()
return args
+
def _init_sql(args):
"""Init the sql session for things to work out"""
ini = ConfigParser()
- ini_path = path.join(path.dirname(path.dirname(path.dirname(__file__))), args.ini)
+ ini_path = path.join(path.dirname(path.dirname(path.dirname(__file__))),
+ args.ini)
ini.readfp(open(ini_path))
here = path.abspath(path.join(path.dirname(__file__), '../../'))
ini.set('app:main', 'here', here)

0 comments on commit 22e508a

Please sign in to comment.