]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/admin.py
Merge pull request #2655 from lonvia/migration-internal-country-name
[nominatim.git] / nominatim / clicmd / admin.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Implementation of the 'admin' subcommand.
9 """
10 import logging
11
12 from nominatim.tools.exec_utils import run_legacy_script
13 from nominatim.db.connection import connect
14
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
19
20 LOG = logging.getLogger()
21
22 class AdminFuncs:
23     """\
24     Analyse and maintain the database.
25     """
26
27     @staticmethod
28     def add_args(parser):
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',
41                            const='search',
42                            help="Only pre-warm tables for search queries")
43         group.add_argument('--reverse-only', action='store_const', dest='target',
44                            const='reverse',
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')
52
53     @staticmethod
54     def run(args):
55         if args.warm:
56             return AdminFuncs._warm(args)
57
58         if args.check_database:
59             LOG.warning('Checking database')
60             from ..tools import check_database
61             return check_database.check_database(args.config)
62
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)
68             return 0
69
70         if args.migrate:
71             LOG.warning('Checking for necessary database migrations')
72             from ..tools import migration
73             return migration.migrate(args.config, args)
74
75         return 1
76
77
78     @staticmethod
79     def _warm(args):
80         LOG.warning('Warming database caches')
81         params = ['warm.php']
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)