-#!/usr/bin/python
+#!/usr/bin/env python3
#
# Tool that removes zombie records from database tables#
import sys
schema_file = None
config_file = "/etc/planetlab/plc_config"
config = {}
-execfile(config_file, config)
+exec(compile(open(config_file).read(), config_file, 'exec'), config)
def usage():
- print "Usage: %s SCHEMA_FILE " % sys.argv[0]
+ print("Usage: %s SCHEMA_FILE " % sys.argv[0])
sys.exit(1)
try:
schema_file = sys.argv[1]
except IndexError:
- print "Error: too few arguments"
+ print("Error: too few arguments")
usage()
# all foreing keys exist as primary kyes in another table
line = lines[index].strip()
# find all created objects
if line.startswith("CREATE"):
- item_type = line.split(" ")[1].strip()
- item_name = line.split(" ")[2].strip()
+ line_parts = line.split(" ")
+ if line_parts[1:3] == ['OR', 'REPLACE']:
+ line_parts = line_parts[2:]
+ item_type = line_parts[1].strip()
+ item_name = line_parts[2].strip()
if item_type.upper() in ['TABLE']:
while index < len(lines):
index = index + 1
# do not delete from primary tables
if foreign_key_parts[0] in ['addresses', 'boot_states', 'conf_files', \
- 'keys', 'messages', 'nodegroups', 'nodenetworks', 'nodes', 'pcus', 'peers' \
+ 'keys', 'messages', 'nodegroups', 'interfaces', 'nodes', 'pcus', 'peers' \
'persons', 'roles', 'sessions', 'sites', 'slices']:
#print "skipping table %s" % foreign_key_parts[0]
continue
result = desc.readlines()
if primary_key_parts[0] in ['slices']:
sql = sql + " where name not like '%_deleted'"
- elif filter(lambda line: line.find("deleted") > -1, result):
+ elif [line for line in result if line.find("deleted") > -1]:
sql = sql + " where deleted = false"
cursor.execute(sql)
# also, ignore null foreign keys, not considered zombied
zombie_keys_func = lambda key: key not in primary_key_rows and not key == [None]
zombie_keys_list = [zombie_key[0] for zombie_key in filter(zombie_keys_func, foreign_rows)]
- print zombie_keys_list
+ print(zombie_keys_list)
# delete these zombie records
if zombie_keys_list:
- print " -> Deleting %d zombie record(s) from %s after checking %s" % \
- (len(zombie_keys_list), foreign_key[0], primary_key[0])
+ print(" -> Deleting %d zombie record(s) from %s after checking %s" % \
+ (len(zombie_keys_list), foreign_key_parts[0], primary_key_parts[0]))
sql_delete = 'DELETE FROM %s WHERE %s IN %s' % \
(foreign_key_parts[0], foreign_key_parts[1], tuple(zombie_keys_list))
cursor.execute(sql_delete)
db.commit()
#zombie_keys[foreign_key] = zombie_keys_list
- print "done"
+ print("done")
except pgdb.DatabaseError:
raise