try to improve reliability of index/fk dropping in postgres and add warnings (not errors) when problems occur

This commit is contained in:
sqlcoder 2009-05-31 21:06:33 +01:00
parent c741d720a0
commit de98ff67dd

View File

@ -20,6 +20,7 @@
import datetime import datetime
import time import time
import re import re
import sys
import Card import Card
@ -156,7 +157,7 @@ def prepareBulkImport(fdb):
"AND referenced_column_name = %s ", "AND referenced_column_name = %s ",
(fk['fktab'], fk['fkcol'], fk['rtab'], fk['rcol']) ) (fk['fktab'], fk['fkcol'], fk['rtab'], fk['rcol']) )
cons = fdb.cursor.fetchone() cons = fdb.cursor.fetchone()
print "preparebulk: cons=", cons #print "preparebulk: cons=", cons
if cons: if cons:
print "dropping mysql fk", cons[0], fk['fktab'], fk['fkcol'] print "dropping mysql fk", cons[0], fk['fktab'], fk['fkcol']
try: try:
@ -165,13 +166,25 @@ def prepareBulkImport(fdb):
pass pass
elif fdb.backend == PGSQL: elif fdb.backend == PGSQL:
# DON'T FORGET TO RECREATE THEM!! # DON'T FORGET TO RECREATE THEM!!
#print "dropping pg fk", fk['fktab'], fk['fkcol'] print "dropping pg fk", fk['fktab'], fk['fkcol']
try: try:
#print "alter table %s drop constraint %s_%s_fkey" % (fk['fktab'], fk['fktab'], fk['fkcol']) # try to lock table to see if index drop will work:
fdb.cursor.execute("alter table %s drop constraint %s_%s_fkey" % (fk['fktab'], fk['fktab'], fk['fkcol'])) # hmmm, tested by commenting out rollback in grapher. lock seems to work but
print "dropped pg fk pg fk %s_%s_fkey" % (fk['fktab'], fk['fkcol']) # then drop still hangs :-( does work in some tests though??
# will leave code here for now pending further tests/enhancement ...
fdb.cursor.execute( "lock table %s in exclusive mode nowait" % (fk['fktab'],) )
#print "after lock, status:", fdb.cursor.statusmessage
#print "alter table %s drop constraint %s_%s_fkey" % (fk['fktab'], fk['fktab'], fk['fkcol'])
try:
fdb.cursor.execute("alter table %s drop constraint %s_%s_fkey" % (fk['fktab'], fk['fktab'], fk['fkcol']))
print "dropped pg fk pg fk %s_%s_fkey, continuing ..." % (fk['fktab'], fk['fkcol'])
except:
if "does not exist" not in str(sys.exc_value):
print "warning: drop pg fk %s_%s_fkey failed: %s, continuing ..." \
% (fk['fktab'], fk['fkcol'], str(sys.exc_value).rstrip('\n') )
except: except:
print "! failed drop pg fk %s_%s_fkey" % (fk['fktab'], fk['fkcol']) print "warning: constraint %s_%s_fkey not dropped: %s, continuing ..." \
% (fk['fktab'],fk['fkcol'], str(sys.exc_value).rstrip('\n'))
else: else:
print "Only MySQL and Postgres supported so far" print "Only MySQL and Postgres supported so far"
return -1 return -1
@ -181,22 +194,32 @@ def prepareBulkImport(fdb):
if fdb.backend == MYSQL_INNODB: if fdb.backend == MYSQL_INNODB:
print "dropping mysql index ", idx['tab'], idx['col'] print "dropping mysql index ", idx['tab'], idx['col']
try: try:
# apparently nowait is not implemented in mysql so this just hands if there are locks
# preventing the index drop :-(
fdb.cursor.execute( "alter table %s drop index %s", (idx['tab'],idx['col']) ) fdb.cursor.execute( "alter table %s drop index %s", (idx['tab'],idx['col']) )
except: except:
pass pass
elif fdb.backend == PGSQL: elif fdb.backend == PGSQL:
# DON'T FORGET TO RECREATE THEM!! # DON'T FORGET TO RECREATE THEM!!
#print "Index dropping disabled for postgresql." print "dropping pg index ", idx['tab'], idx['col']
#print "dropping pg index ", idx['tab'], idx['col']
# mod to use tab_col for index name?
try: try:
fdb.cursor.execute( "drop index %s_%s_idx" % (idx['tab'],idx['col']) ) # try to lock table to see if index drop will work:
print "drop index %s_%s_idx" % (idx['tab'],idx['col']) fdb.cursor.execute( "lock table %s in exclusive mode nowait" % (idx['tab'],) )
#print "dropped pg index ", idx['tab'], idx['col'] #print "after lock, status:", fdb.cursor.statusmessage
try:
# table locked ok so index drop should work:
#print "drop index %s_%s_idx" % (idx['tab'],idx['col'])
fdb.cursor.execute( "drop index if exists %s_%s_idx" % (idx['tab'],idx['col']) )
#print "dropped pg index ", idx['tab'], idx['col']
except:
if "does not exist" not in str(sys.exc_value):
print "warning: drop index %s_%s_idx failed: %s, continuing ..." \
% (idx['tab'],idx['col'], str(sys.exc_value).rstrip('\n'))
except: except:
print "! failed drop index %s_%s_idx" % (idx['tab'],idx['col']) print "warning: index %s_%s_idx not dropped %s, continuing ..." \
% (idx['tab'],idx['col'], str(sys.exc_value).rstrip('\n'))
else: else:
print "Only MySQL and Postgres supported so far" print "Error: Only MySQL and Postgres supported so far"
return -1 return -1
if fdb.backend == PGSQL: if fdb.backend == PGSQL: