Database: re-write and fix drop_tables

Exceptions: add FpdbDatabaseError
SQL: default all tourneytypeids to 1
fpdb_import: import error tuples set to right size
This commit is contained in:
Eric Blade 2009-09-04 06:49:46 -05:00
parent 9a44a2efef
commit 9fa57b6014
4 changed files with 57 additions and 38 deletions

View File

@ -884,35 +884,51 @@ class Database:
def drop_tables(self): def drop_tables(self):
"""Drops the fpdb tables from the current db""" """Drops the fpdb tables from the current db"""
try: try:
c = self.get_cursor() c = self.get_cursor()
if(self.get_backend_name() == 'MySQL InnoDB'):
#Databases with FOREIGN KEY support need this switched of before you can drop tables
self.drop_referential_integrity()
# Query the DB to see what tables exist
c.execute(self.sql.query['list_tables'])
for table in c:
c.execute(self.sql.query['drop_table'] + table[0])
elif(self.get_backend_name() == 'PostgreSQL'):
self.commit()# I have no idea why this makes the query work--REB 07OCT2008
c.execute(self.sql.query['list_tables'])
tables = c.fetchall()
for table in tables:
c.execute(self.sql.query['drop_table'] + table[0] + ' cascade')
elif(self.get_backend_name() == 'SQLite'):
c.execute(self.sql.query['list_tables'])
for table in c.fetchall():
log.debug(self.sql.query['drop_table'] + table[0])
c.execute(self.sql.query['drop_table'] + table[0])
self.commit()
except: except:
err = traceback.extract_tb(sys.exc_info()[2])[-1] print "*** Error unable to get cursor"
print "***Error dropping tables: "+err[2]+"("+str(err[1])+"): "+str(sys.exc_info()[1]) else:
self.rollback() backend = self.get_backend_name()
raise if backend == 'MySQL InnoDB': # what happens if someone is using MyISAM?
try:
self.drop_referential_integrity() # needed to drop tables with foreign keys
c.execute(self.sql.query['list_tables'])
tables = c.fetchall()
for table in tables:
c.execute(self.sql.query['drop_table'] + table[0])
except:
err = traceback.extract_tb(sys.exc_info()[2])[-1]
print "***Error dropping tables: "+err[2]+"("+str(err[1])+"): "+str(sys.exc_info()[1])
self.rollback()
elif backend == 'PostgreSQL':
try:
self.commit()
c.execute(self.sql.query['list_tables'])
tables = c.fetchall()
for table in tables:
c.execute(self.sql.query['drop_table'] + table[0] + ' cascade')
except:
err = traceback.extract_tb(sys.exc_info()[2])[-1]
print "***Error dropping tables: "+err[2]+"("+str(err[1])+"): "+str(sys.exc_info()[1])
self.rollback()
elif backend == 'SQLite':
try:
c.execute(self.sql.query['list_tables'])
for table in c.fetchall():
log.debug(self.sql.query['drop_table'] + table[0])
c.execute(self.sql.query['drop_table'] + table[0])
except:
err = traceback.extract_tb(sys.exc_info()[2])[-1]
print "***Error dropping tables: "+err[2]+"("+str(err[1])+"): "+str(sys.exc_info()[1])
self.rollback()
try:
self.commit()
except:
print "*** Error in committing table drop"
err = traceback.extract_tb(sys.exc_info()[2])[-1]
print "***Error dropping tables: "+err[2]+"("+str(err[1])+"): "+str(sys.exc_info()[1])
self.rollback()
#end def drop_tables #end def drop_tables
def createAllIndexes(self): def createAllIndexes(self):
@ -998,7 +1014,7 @@ class Database:
if self.backend == self.SQLITE: if self.backend == self.SQLITE:
c.execute("INSERT INTO TourneyTypes (id, siteId, buyin, fee) VALUES (NULL, 1, 0, 0);") c.execute("INSERT INTO TourneyTypes (id, siteId, buyin, fee) VALUES (NULL, 1, 0, 0);")
else: else:
c.execute("INSERT INTO TourneyTypes (siteId, buyin, fee) VALUES (1, 0, 0);") c.execute("insert into tourneytypes values (0,1,0,0,0,0,0,null,0,0,0);")
#end def fillDefaultData #end def fillDefaultData

View File

@ -14,5 +14,8 @@ class FpdbParseError(FpdbError):
else: else:
return repr(self.value) return repr(self.value)
class FpdbDatabaseError(FpdbError):
pass
class DuplicateError(FpdbError): class DuplicateError(FpdbError):
pass pass

View File

@ -370,7 +370,7 @@ class Sql:
if db_server == 'mysql': if db_server == 'mysql':
self.query['createTourneysTable'] = """CREATE TABLE Tourneys ( self.query['createTourneysTable'] = """CREATE TABLE Tourneys (
id INT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), id INT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id),
tourneyTypeId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), tourneyTypeId SMALLINT UNSIGNED NOT NULL DEFAULT 1, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id),
siteTourneyNo BIGINT NOT NULL, siteTourneyNo BIGINT NOT NULL,
entries INT NOT NULL, entries INT NOT NULL,
prizepool INT NOT NULL, prizepool INT NOT NULL,
@ -392,7 +392,7 @@ class Sql:
elif db_server == 'postgresql': elif db_server == 'postgresql':
self.query['createTourneysTable'] = """CREATE TABLE Tourneys ( self.query['createTourneysTable'] = """CREATE TABLE Tourneys (
id SERIAL, PRIMARY KEY (id), id SERIAL, PRIMARY KEY (id),
tourneyTypeId INT, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), tourneyTypeId INT DEFAULT 1, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id),
siteTourneyNo BIGINT, siteTourneyNo BIGINT,
entries INT, entries INT,
prizepool INT, prizepool INT,
@ -413,7 +413,7 @@ class Sql:
elif db_server == 'sqlite': elif db_server == 'sqlite':
self.query['createTourneysTable'] = """CREATE TABLE Tourneys ( self.query['createTourneysTable'] = """CREATE TABLE Tourneys (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
tourneyTypeId INT, tourneyTypeId INT DEFAULT 1,
siteTourneyNo INT, siteTourneyNo INT,
entries INT, entries INT,
prizepool INT, prizepool INT,
@ -460,7 +460,7 @@ class Sql:
comment text, comment text,
commentTs DATETIME, commentTs DATETIME,
tourneysPlayersId BIGINT UNSIGNED, tourneysPlayersId BIGINT UNSIGNED,
tourneyTypeId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), tourneyTypeId SMALLINT UNSIGNED NOT NULL DEFAULT 1, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id),
wonWhenSeenStreet1 FLOAT, wonWhenSeenStreet1 FLOAT,
wonWhenSeenStreet2 FLOAT, wonWhenSeenStreet2 FLOAT,
@ -578,7 +578,7 @@ class Sql:
comment text, comment text,
commentTs timestamp without time zone, commentTs timestamp without time zone,
tourneysPlayersId BIGINT, tourneysPlayersId BIGINT,
tourneyTypeId INT NOT NULL, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), tourneyTypeId INT NOT NULL DEFAULT 1, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id),
wonWhenSeenStreet1 FLOAT, wonWhenSeenStreet1 FLOAT,
wonWhenSeenStreet2 FLOAT, wonWhenSeenStreet2 FLOAT,
@ -695,7 +695,7 @@ class Sql:
comment TEXT, comment TEXT,
commentTs REAL, commentTs REAL,
tourneysPlayersId INT, tourneysPlayersId INT,
tourneyTypeId INT NOT NULL, tourneyTypeId INT NOT NULL DEFAULT 1,
wonWhenSeenStreet1 REAL, wonWhenSeenStreet1 REAL,
wonWhenSeenStreet2 REAL, wonWhenSeenStreet2 REAL,
@ -865,7 +865,7 @@ class Sql:
playerId INT UNSIGNED NOT NULL, FOREIGN KEY (playerId) REFERENCES Players(id), playerId INT UNSIGNED NOT NULL, FOREIGN KEY (playerId) REFERENCES Players(id),
activeSeats SMALLINT NOT NULL, activeSeats SMALLINT NOT NULL,
position CHAR(1), position CHAR(1),
tourneyTypeId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), tourneyTypeId SMALLINT UNSIGNED NOT NULL DEFAULT 1, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id),
styleKey CHAR(7) NOT NULL, /* 1st char is style (A/T/H/S), other 6 are the key */ styleKey CHAR(7) NOT NULL, /* 1st char is style (A/T/H/S), other 6 are the key */
HDs INT NOT NULL, HDs INT NOT NULL,
@ -966,7 +966,7 @@ class Sql:
playerId INT, FOREIGN KEY (playerId) REFERENCES Players(id), playerId INT, FOREIGN KEY (playerId) REFERENCES Players(id),
activeSeats SMALLINT, activeSeats SMALLINT,
position CHAR(1), position CHAR(1),
tourneyTypeId INT, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), tourneyTypeId INT DEFAULT 1, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id),
styleKey CHAR(7) NOT NULL, /* 1st char is style (A/T/H/S), other 6 are the key */ styleKey CHAR(7) NOT NULL, /* 1st char is style (A/T/H/S), other 6 are the key */
HDs INT, HDs INT,
@ -1065,7 +1065,7 @@ class Sql:
playerId INT, playerId INT,
activeSeats INT, activeSeats INT,
position TEXT, position TEXT,
tourneyTypeId INT, tourneyTypeId INT DEFAULT 1,
styleKey TEXT NOT NULL, /* 1st char is style (A/T/H/S), other 6 are the key */ styleKey TEXT NOT NULL, /* 1st char is style (A/T/H/S), other 6 are the key */
HDs INT, HDs INT,

View File

@ -418,10 +418,10 @@ class Importer:
else: else:
# conversion didn't work # conversion didn't work
# TODO: appropriate response? # TODO: appropriate response?
return (0, 0, 0, 1, 0, -1) return (0, 0, 0, 1, 0)
else: else:
log.warning("Unknown filter filter_name:'%s' in filter:'%s'" %(filter_name, filter)) log.warning("Unknown filter filter_name:'%s' in filter:'%s'" %(filter_name, filter))
return (0, 0, 0, 1, 0, -1) return (0, 0, 0, 1, 0)
#This will barf if conv.getStatus != True #This will barf if conv.getStatus != True
return (stored, duplicates, partial, errors, ttime) return (stored, duplicates, partial, errors, ttime)