blob: 587a2aa1c926f676e8a41737a5b3a3ffe33b77ab [file] [log] [blame]
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""This is a small script designed to issue REINDEX TABLE commands to psql."""
import argparse
import os
import sys
# Import 'common.env' to load our Infra PYTHONPATH
sys.path.insert(0, os.path.join(
os.path.dirname(os.path.realpath(__file__)), os.pardir))
import common.env
from common import chromium_utils
def get_database_creds(dbconfig):
print 'reading dbconfig from %s' % dbconfig
values = {}
if os.path.isfile(dbconfig):
execfile(dbconfig, values)
if 'password' not in values:
raise Exception('could not get db password')
return values
def main():
parser = argparse.ArgumentParser(
description='Run a REINDEX TABLE command on postgres.')
help='location of the master to reindex.')
parser.add_argument('--dbconfig-filename', default='.dbconfig',
help='name of the dbconfig, defaults to %(default)s.')
parser.add_argument('--prod', action='store_true',
help='actually execute command instead of just displaying it.')
args = parser.parse_args()
filename = chromium_utils.AbsoluteCanonicalPath(os.path.join(, args.dbconfig_filename))
dbconfig = get_database_creds(filename)
cmd = ['psql', '-h', 'localhost', '-U', dbconfig['username'],
'-d', dbconfig['dbname'], '-c',
'REINDEX TABLE buildrequests;']
new_env = os.environ.copy()
new_env['PGPASSWORD'] = dbconfig['password']
return chromium_utils.RunCommand(cmd, env=new_env)
print 'Would have run %s.' % cmd
print 'If this looks good, re-run with --prod.'
return 0
if __name__ == '__main__':