summaryrefslogtreecommitdiffstats
path: root/database/scripts/reset-ranking.py
blob: 5f7afde9f0ef9dc20a61441dd97dd12f520d3e8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python

import sys
from subprocess import Popen, PIPE
import string

"""
NOTE: This script should be run as the 'postgres' role.

NOTE: Eventually, once the ranking feature is mature, this script should be removed
      and the effect it has should be implemented in reset.py instead.
"""

# --- Global functions -------------------------------------------------

def runCommand(cmd, exitOnFail = True):
    print "running command >%s< ..." % string.join(cmd, " "),
    p = Popen(cmd, stdout = PIPE, stderr = PIPE)
    stdout, stderr = p.communicate()
    if (p.returncode != 0):
        print "FAIL:"
        print "  return code:", p.returncode
        print "  stdout: >%s<" % stdout.strip()
        print "  stderr: >%s<" % stderr.strip()
        if (exitOnFail):
            print "exiting ..."
            sys.exit(1)
        else:
            print "CONTINUING ..."
    else:
        print "PASS"


# --- Main program -------------------------------------------------

# Ensure the intention of running this script.
reply = raw_input(
    "WARNING: The 'ranking' and 'rankingStat' tables will be deleted!"
    " Continue [y/N]?").strip().lower()
if (reply != "y" and reply != "yes"):
    sys.exit(1)

database = "bm"
user = "bmuser"
owner = "postgres"
tabledefs = "tabledefs-ranking.sql"
privileges = "privileges-ranking.sql"

# Load table definitions (deleting existing tables).
runCommand(["psql", "-U", owner, "-d", database, "-f", tabledefs])

# Define user privileges (if necessary).
# (Note: remote client access is defined separately in pg_hba.conf)
runCommand(["psql", "-U", owner, "-d", database, "-f", privileges])