]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/admin.py
replace add-data function with native Python code
[nominatim.git] / nominatim / clicmd / admin.py
1 """
2 Implementation of the 'admin' subcommand.
3 """
4 import logging
5
6 from nominatim.tools.exec_utils import run_legacy_script
7 from nominatim.db.connection import connect
8
9 # Do not repeat documentation of subcommand classes.
10 # pylint: disable=C0111
11 # Using non-top-level imports to avoid eventually unused imports.
12 # pylint: disable=E0012,C0415
13
14 LOG = logging.getLogger()
15
16 class AdminFuncs:
17     """\
18     Analyse and maintain the database.
19     """
20
21     @staticmethod
22     def add_args(parser):
23         group = parser.add_argument_group('Admin tasks')
24         objs = group.add_mutually_exclusive_group(required=True)
25         objs.add_argument('--warm', action='store_true',
26                           help='Warm database caches for search and reverse queries.')
27         objs.add_argument('--check-database', action='store_true',
28                           help='Check that the database is complete and operational.')
29         objs.add_argument('--migrate', action='store_true',
30                           help='Migrate the database to a new software version.')
31         objs.add_argument('--analyse-indexing', action='store_true',
32                           help='Print performance analysis of the indexing process.')
33         group = parser.add_argument_group('Arguments for cache warming')
34         group.add_argument('--search-only', action='store_const', dest='target',
35                            const='search',
36                            help="Only pre-warm tables for search queries")
37         group.add_argument('--reverse-only', action='store_const', dest='target',
38                            const='reverse',
39                            help="Only pre-warm tables for reverse queries")
40         group = parser.add_argument_group('Arguments for index anaysis')
41         mgroup = group.add_mutually_exclusive_group()
42         mgroup.add_argument('--osm-id', type=str,
43                             help='Analyse indexing of the given OSM object')
44         mgroup.add_argument('--place-id', type=int,
45                             help='Analyse indexing of the given Nominatim object')
46
47     @staticmethod
48     def run(args):
49         if args.warm:
50             return AdminFuncs._warm(args)
51
52         if args.check_database:
53             LOG.warning('Checking database')
54             from ..tools import check_database
55             return check_database.check_database(args.config)
56
57         if args.analyse_indexing:
58             LOG.warning('Analysing performance of indexing function')
59             from ..tools import admin
60             with connect(args.config.get_libpq_dsn()) as conn:
61                 admin.analyse_indexing(conn, osm_id=args.osm_id, place_id=args.place_id)
62             return 0
63
64         if args.migrate:
65             LOG.warning('Checking for necessary database migrations')
66             from ..tools import migration
67             return migration.migrate(args.config, args)
68
69         return 1
70
71
72     @staticmethod
73     def _warm(args):
74         LOG.warning('Warming database caches')
75         params = ['warm.php']
76         if args.target == 'reverse':
77             params.append('--reverse-only')
78         if args.target == 'search':
79             params.append('--search-only')
80         return run_legacy_script(*params, nominatim_env=args)