| # 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() |