1 # Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Script for administering the Continuous Integration Database."""
7 from __future__ import print_function
12 from chromite.lib import cidb
13 from chromite.lib import commandline
14 from chromite.lib import cros_build_lib
19 COMMANDS = [MIGRATE, WIPE]
22 """Creates the argparse parser."""
23 parser = commandline.ArgumentParser(description=__doc__)
25 # Put options that control the mode of script into mutually exclusive group.
27 parser.add_argument('command', action='store', choices=COMMANDS,
28 help='The action to execute.')
29 parser.add_argument('cred_dir', action='store',
30 metavar='CIDB_CREDENTIALS_DIR',
31 help='Database credentials directory with certificates '
32 'and other connection information.')
33 parser.add_argument('--migrate-version', action='store', default=None,
34 help='Maximum schema version to migrate to.')
41 options = parser.parse_args(argv)
43 logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
45 if options.command == MIGRATE:
46 positive_confirmation = 'please modify my database'
47 warn = ('This option will apply schema changes to your existing database. '
48 'You should not run this against the production database unless '
49 'your changes are thoroughly tested, and those tests included '
50 'in cidb_integration_test.py (including tests that old data is '
51 'sanely migrated forward). Database corruption could otherwise '
52 'result. Are you sure you want to proceed? If so, type "%s" '
53 'now.\n') % positive_confirmation
54 elif options.command == WIPE:
55 positive_confirmation = 'please delete my data'
56 warn = ('This operation will wipe (i.e. DELETE!) the entire contents of '
57 'the database pointed at by %s. Are you sure you want to proceed? '
58 'If so, type "%s" now.\n') % (
59 os.path.join(options.cred_dir, 'host.txt'),
60 positive_confirmation)
62 print('No command or unsupported command. Exiting.')
66 conf_string = cros_build_lib.GetInput('(%s)?: ' % positive_confirmation)
67 if conf_string != positive_confirmation:
68 print('You changed your mind. Aborting.')
71 if options.command == MIGRATE:
72 print('OK, applying migrations...')
73 db = cidb.CIDBConnection(options.cred_dir)
74 db.ApplySchemaMigrations(maxVersion = options.migrate_version)
75 elif options.command == WIPE:
76 print('OK, wiping database...')
77 db = cidb.CIDBConnection(options.cred_dir)