<commit_before><commit_msg>[project @ db upgrade script]<commit_after># SQL Store Upgrade Script
# doesn't depend on the openid library, so you can run this python
# script to update databases for ruby or php as well

from optparse import OptionParser


def askForPassword():
    return raw_input("DB Password (not hidden): ")

def askForConfirmation(dbname,tablename):
    print """The table %s from the database %s will be dropped, and 
    an empty table with the new nonce table schema will replace it."""%(
    tablename, dbname)
    return raw_input("Continue? ").lower().startswith('y')

def doSQLiteUpgrade(db_conn, nonce_table_name='oid_nonces'):
    cur = conn.cursor()
    cur.execute('DROP TABLE %s'%nonce_table_name)
    sql = """
    CREATE TABLE %s (
        server_url VARCHAR,
        timestamp INTEGER,
        salt CHAR(40),
        UNIQUE(server_url, timestamp, salt)
    );
    """%nonce_table_name
    cur.execute(sql)
    cur.close()
    
def doMySQLUpgrade(db_conn, nonce_table_name='oid_nonces'):
    cur = conn.cursor()
    cur.execute('DROP TABLE %s'%nonce_table_name)
    sql = """
    CREATE TABLE %s (
        server_url BLOB,
        timestamp INTEGER,
        salt CHAR(40),
        PRIMARY KEY (server_url(255), timestamp, salt)
    )
    TYPE=InnoDB;
    """%nonce_table_name
    cur.execute(sql)
    cur.close()

def doPostGreSQLUpgrade(db_conn, nonce_table_name='oid_nonces'):
    cur = conn.cursor()
    cur.execute('DROP TABLE %s'%nonce_table_name)
    sql = """
    CREATE TABLE %s (
        server_url VARCHAR(2047),
        timestamp INTEGER,
        salt CHAR(40),
        PRIMARY KEY (server_url, timestamp, salt)
    );
    """%nonce_table_name
    cur.execute(sql)
    cur.close()
    conn.commit()

    

if __name__ == '__main__':
    parser = OptionParser()
    parser.add_option("-u", "--user", dest="username", default=os.environ.get('USER')
                    help="User name to use to connect to the DB. Defaults to USER environment variable.")
    parser.add_option('-t', '--table', dest='tablename', default='oid_nonces',
                    help='The name of the nonce table to drop and recreate. defaults to "oid_nonces", the default table name for the openid stores.')
    parser.add_option('--mysql', dest='mysql_db_name', 
                    help='Upgrade a table from this MySQL database. Requires username for database.')
    parser.add_option('--pg', '--postgresql', dest='postgres_db_name',
                    help='Upgrade a table from this PostGreSQL database. Requires username for database.')
    parser.add_option('--sqlite', dest='sqlite_db_name',
                    help='Upgrade a table from this SQLite database file')
    parser.add_option('--host', dest='db_host',
                    help='Host on which to find MySQL or PostGreSQL DB')
    (options, args) = parser.parse_args()

    if options.sqlite_db_name:
        
        try:
            from pysqlite2 import dbapi2 as sqlite
        except ImportError:
            print "You must have pysqlite2 installed in your PYTHONPATH."
            exit
        db_conn = sqlite.connect(options.sqlite_db_name)
        if askForConfirmation(options.sqlite_db_name, options.tablename):
            doSQLiteUpgrade(db_conn, nonce_table_name=options.tablename)
            
    if options.postgres_db_name:
        if not options.username:
            print "A username is required to open a PostGreSQL Database."
            exit
        password = askForPassword()
        try:
            import psycopg
        except ImportError:
            print "You need psycopg installed to update a postgres DB."
            exit
        db_conn = psycopg.connect(database = options.postgres_db_name,
                               user = options.username,
                               host = options.db_host
                               passwd = password)
        if askForConfirmation(options.postgres_db_name, options.tablename):
            doPostGreSQLUpgrade(db_conn, nonce_table_name=options.tablename)
        conn.close()
    
    if options.mysql_db_name:
        if not options.username:
            print "A username is required to open a MySQL Database."
            exit
        password = askForPassword()
        try:
            import MySQLdb
        except ImportError:
            print "You must have MySQLdb installed to update a MySQL DB."
            exit
        db_conn = MySQLdb.connect(database = options.mysql_db_name,
                               user = options.username,
                               host = options.db_host
                               passwd = password)
        if askForConfirmation(options.mysql_db_name, options.tablename):
            doMySQLUpgrade(db_conn, nonce_table_name=options.tablename)
        
    
{'fraction_non_alphanumeric': 0.09229484234704531, 'fraction_numerical': 0.003340989768218835, 'mean_word_length': 2.305728088336784, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4687290', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1362, 'n_words': 375}