scripts/migrate_shorturls.py
changeset 22 72696ca68c34
child 26 81d6679d50d0
equal deleted inserted replaced
21:b75f9514e797 22:72696ca68c34
       
     1 from lib import shorturl, db
       
     2 
       
     3 sdb = shorturl.DB()
       
     4 
       
     5 def gen () :
       
     6     count = 0
       
     7 
       
     8     for key in sdb.db.keys() :
       
     9         if key.startswith("_") :
       
    10             continue
       
    11         
       
    12         print "%6s" % key,
       
    13 
       
    14         value = sdb.db[key]
       
    15 
       
    16         if not isinstance(value, tuple) or len(value) != 3 :
       
    17             print "CORRUPT VALUE!!!"
       
    18             continue
       
    19 
       
    20         type, dirpath, fname = value
       
    21 
       
    22         if type == "img" :
       
    23             id = shorturl.key2int(key)
       
    24 
       
    25             dirpath = dirpath.lstrip('.').lstrip('/')
       
    26 
       
    27             print "%6d %50s %10s" % (id, dirpath, fname)
       
    28 
       
    29             yield id, dirpath, fname
       
    30 
       
    31             count += 1
       
    32 
       
    33             if count % 500 == 0 :
       
    34                 print count
       
    35         else :
       
    36             print "dir"
       
    37 
       
    38 print "Starting import..."
       
    39 
       
    40 c = db.cursor()
       
    41 
       
    42 c.executemany("""
       
    43     INSERT INTO images VALUES (?, ?, ?)
       
    44 """, gen())
       
    45 
       
    46 print "Done!"
       
    47 
       
    48 print "%d rows affected" % c.rowcount
       
    49 
       
    50 c.execute("SELECT id FROM images").fetchall()
       
    51