Index: trunk/tools/editor_trends/database/db.py |
— | — | @@ -32,6 +32,23 @@ |
33 | 33 | return db |
34 | 34 | |
35 | 35 | |
| 36 | +def drop_collection(dbname, collection): |
| 37 | + db = init_mongo_db(dbname) |
| 38 | + db.drop_collection(collection) |
| 39 | + |
| 40 | + |
| 41 | +def get_collections(dbname): |
| 42 | + db = init_mongo_db(dbname) |
| 43 | + return db.collection_names() |
| 44 | + |
| 45 | + |
| 46 | +def cleanup_database(dbname): |
| 47 | + coll = get_collections(dbname) |
| 48 | + for c in coll: |
| 49 | + if not c.startswith('system'): |
| 50 | + drop_collection(dbname, c) |
| 51 | + |
| 52 | + |
36 | 53 | def remove_documents_from_mongo_db(collection, ids): |
37 | 54 | collection.remove(ids) |
38 | 55 | |
— | — | @@ -49,39 +66,39 @@ |
50 | 67 | mongo.collection.ensure_index(key) |
51 | 68 | |
52 | 69 | |
53 | | -def init_database(db=None): |
54 | | - ''' |
55 | | - This function initializes the connection with a sqlite db. |
56 | | - If the database already exists then it returns False to indicate |
57 | | - that the db already exists, else it returns True to indicate |
58 | | - that it's an empty database without tables. |
59 | | - ''' |
60 | | - if db == None: |
61 | | - db = settings.DATABASE_NAME |
| 70 | +#def init_database(db=None): |
| 71 | +# ''' |
| 72 | +# This function initializes the connection with a sqlite db. |
| 73 | +# If the database already exists then it returns False to indicate |
| 74 | +# that the db already exists, else it returns True to indicate |
| 75 | +# that it's an empty database without tables. |
| 76 | +# ''' |
| 77 | +# if db == None: |
| 78 | +# db = settings.DATABASE_NAME |
| 79 | +# |
| 80 | +# return sqlite.connect(db, check_same_thread=False) |
| 81 | +# |
| 82 | +# |
| 83 | +#def create_tables(cursor, tables): |
| 84 | +# ''' |
| 85 | +# Tables is expected to be a dictionary, with key |
| 86 | +# table name and value another dictionary. This second |
| 87 | +# dictionary contains variable names and datatypes. |
| 88 | +# ''' |
| 89 | +# for table in tables: |
| 90 | +# vars = '(' |
| 91 | +# for var, datatype in tables[table]: |
| 92 | +# vars = vars + '%s %s,' % (var, datatype) |
| 93 | +# vars = vars[:-1] |
| 94 | +# vars = vars + ')' |
| 95 | +# cursor.execute('CREATE TABLE IF NOT EXISTS ? ?' % (table, vars)) |
| 96 | +# |
| 97 | +# |
| 98 | +#def debug(): |
| 99 | +# connection = init_database() |
| 100 | +# cursor = connection.cursor() |
| 101 | +# create_tables(cursor, settings.TABLES) |
62 | 102 | |
63 | | - return sqlite.connect(db, check_same_thread=False) |
64 | 103 | |
65 | | - |
66 | | -def create_tables(cursor, tables): |
67 | | - ''' |
68 | | - Tables is expected to be a dictionary, with key |
69 | | - table name and value another dictionary. This second |
70 | | - dictionary contains variable names and datatypes. |
71 | | - ''' |
72 | | - for table in tables: |
73 | | - vars = '(' |
74 | | - for var, datatype in tables[table]: |
75 | | - vars = vars + '%s %s,' % (var, datatype) |
76 | | - vars = vars[:-1] |
77 | | - vars = vars + ')' |
78 | | - cursor.execute('CREATE TABLE IF NOT EXISTS ? ?' % (table, vars)) |
79 | | - |
80 | | - |
81 | | -def debug(): |
82 | | - connection = init_database() |
83 | | - cursor = connection.cursor() |
84 | | - create_tables(cursor, settings.TABLES) |
85 | | - |
86 | | - |
87 | 104 | if __name__ == '__main__': |
88 | 105 | debug() |