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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#!/usr/bin/env python
# SQL Store Upgrade Script
# for version 1.x to 2.0 of the OpenID library.
# Doesn't depend on the openid library, so you can run this python
# script to update databases for ruby or PHP as well.
#
# Testers note:
#
# A SQLite3 db with the 1.2 schema exists in
# openid/test/data/openid-1.2-consumer-sqlitestore.db if you want something
# to try upgrading.
#
# TODO:
# * test data for mysql and postgresql.
# * automated tests.
import os
import getpass
import sys
from optparse import OptionParser
def askForPassword():
return getpass.getpass("DB Password: ")
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().strip().startswith('y')
def doSQLiteUpgrade(db_conn, nonce_table_name='oid_nonces'):
cur = db_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 = db_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 = db_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()
db_conn.commit()
def main(argv=None):
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',
default='localhost',
help='Host on which to find MySQL or PostgreSQL DB.')
(options, args) = parser.parse_args(argv)
db_conn = None
if options.sqlite_db_name:
try:
from pysqlite2 import dbapi2 as sqlite
except ImportError:
print "You must have pysqlite2 installed in your PYTHONPATH."
return 1
try:
db_conn = sqlite.connect(options.sqlite_db_name)
except Exception, e:
print "Could not connect to SQLite database:", str(e)
return 1
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."
return 1
password = askForPassword()
try:
import psycopg
except ImportError:
print "You need psycopg installed to update a postgres DB."
return 1
try:
db_conn = psycopg.connect(database = options.postgres_db_name,
user = options.username,
host = options.db_host,
password = password)
except Exception, e:
print "Could not connect to PostgreSQL database:", str(e)
return 1
if askForConfirmation(options.postgres_db_name, options.tablename):
doPostgreSQLUpgrade(db_conn, nonce_table_name=options.tablename)
if options.mysql_db_name:
if not options.username:
print "A username is required to open a MySQL Database."
return 1
password = askForPassword()
try:
import MySQLdb
except ImportError:
print "You must have MySQLdb installed to update a MySQL DB."
return 1
try:
db_conn = MySQLdb.connect(options.db_host, options.username,
password, options.mysql_db_name)
except Exception, e:
print "Could not connect to MySQL database:", str(e)
return 1
if askForConfirmation(options.mysql_db_name, options.tablename):
doMySQLUpgrade(db_conn, nonce_table_name=options.tablename)
if db_conn:
db_conn.close()
else:
parser.print_help()
return 0
if __name__ == '__main__':
retval = main()
sys.exit(retval)
|