Parent: [ad6248] (diff)

Download this file

recover-user-databases.py    68 lines (59 with data), 2.5 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
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()