1 # SPDX-License-Identifier: GPL-2.0-only
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2022 by the Nominatim developer community.
 
   6 # For a full list of authors see the git log.
 
   8 Implementation of the 'admin' subcommand.
 
  12 from nominatim.tools.exec_utils import run_legacy_script
 
  13 from nominatim.db.connection import connect
 
  15 # Do not repeat documentation of subcommand classes.
 
  16 # pylint: disable=C0111
 
  17 # Using non-top-level imports to avoid eventually unused imports.
 
  18 # pylint: disable=E0012,C0415
 
  20 LOG = logging.getLogger()
 
  24     Analyse and maintain the database.
 
  29         group = parser.add_argument_group('Admin tasks')
 
  30         objs = group.add_mutually_exclusive_group(required=True)
 
  31         objs.add_argument('--warm', action='store_true',
 
  32                           help='Warm database caches for search and reverse queries')
 
  33         objs.add_argument('--check-database', action='store_true',
 
  34                           help='Check that the database is complete and operational')
 
  35         objs.add_argument('--migrate', action='store_true',
 
  36                           help='Migrate the database to a new software version')
 
  37         objs.add_argument('--analyse-indexing', action='store_true',
 
  38                           help='Print performance analysis of the indexing process')
 
  39         group = parser.add_argument_group('Arguments for cache warming')
 
  40         group.add_argument('--search-only', action='store_const', dest='target',
 
  42                            help="Only pre-warm tables for search queries")
 
  43         group.add_argument('--reverse-only', action='store_const', dest='target',
 
  45                            help="Only pre-warm tables for reverse queries")
 
  46         group = parser.add_argument_group('Arguments for index anaysis')
 
  47         mgroup = group.add_mutually_exclusive_group()
 
  48         mgroup.add_argument('--osm-id', type=str,
 
  49                             help='Analyse indexing of the given OSM object')
 
  50         mgroup.add_argument('--place-id', type=int,
 
  51                             help='Analyse indexing of the given Nominatim object')
 
  56             return AdminFuncs._warm(args)
 
  58         if args.check_database:
 
  59             LOG.warning('Checking database')
 
  60             from ..tools import check_database
 
  61             return check_database.check_database(args.config)
 
  63         if args.analyse_indexing:
 
  64             LOG.warning('Analysing performance of indexing function')
 
  65             from ..tools import admin
 
  66             with connect(args.config.get_libpq_dsn()) as conn:
 
  67                 admin.analyse_indexing(conn, osm_id=args.osm_id, place_id=args.place_id)
 
  71             LOG.warning('Checking for necessary database migrations')
 
  72             from ..tools import migration
 
  73             return migration.migrate(args.config, args)
 
  80         LOG.warning('Warming database caches')
 
  82         if args.target == 'reverse':
 
  83             params.append('--reverse-only')
 
  84         if args.target == 'search':
 
  85             params.append('--search-only')
 
  86         return run_legacy_script(*params, nominatim_env=args)