Child: [ddf08c] (diff)

Download this file

recover-user-databases.py    51 lines (43 with data), 1.6 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sys
import logging
from ming.orm import session
from allura import model as M
log = logging.getLogger(__name__)
IGNORED_COLLECTIONS = [
'_flyway_migration_info',
'user',
'config',
'system.indexes']
def main():
conn = M.session.main_doc_session.bind.conn
n = M.Neighborhood.query.get(url_prefix='/u/')
for p in M.Project.query.find(dict(neighborhood_id=n._id)):
if not p.database_configured: continue
if not p.shortname.startswith('u/'): continue
log.info('Checking to see if %s is configured...', p.database)
db = conn[p.database]
if is_unconfigured(db):
if sys.argv[-1] == 'test':
log.info('... it is not, so I would drop it.')
continue
log.info('... it is not, so dropping it.')
conn.drop_database(p.database)
p.database_configured = False
session(p).flush()
else:
log.info('... it is.')
def is_unconfigured(db):
# Check for data in collections other than those we pre-fill with data
for collection_name in db.collection_names():
if collection_name in IGNORED_COLLECTIONS: continue
collection = db[collection_name]
if collection.count():
log.info('...%s has data', collection_name)
return False
# DB is configured if it has more than profile/admin/search tools installed
if db.config.count() != 3:
log.info('...has %d tools', db.config.count())
return False
return True
if __name__ == '__main__':
main()