def print_start(thing):
# Truncate to 66 characters, leaving 10 characters for a success
# or failure message
- truncated_thing = thing[0:66]
+ truncated_thing = thing[:66]
# Also, space-pad to keep the cursor in a known column
num_spaces = 66 - len(truncated_thing)
# them to the session last
# ASSUMPTION: Self-referential tables have a single PK called "id"
deferred_rows = [] # ( row referring to id, [foreign ids we need] )
- seen_ids = {} # primary key we've seen => 1
+ seen_ids = set() # primary keys we've seen
# Fetch foreign key columns that point at this table, if any
self_ref_columns = []
for column in table_obj.c:
- if any(_.references(table_obj) for _ in column.foreign_keys):
+ if any(x.references(table_obj) for x in column.foreign_keys):
self_ref_columns.append(column)
new_rows = []
# May need to stash this row and add it later if it refers to a
# later row in this table
if self_ref_columns:
- foreign_ids = [row_data[_.name] for _ in self_ref_columns]
- foreign_ids = [_ for _ in foreign_ids if _] # remove NULL ids
+ foreign_ids = set(row_data[x.name] for x in self_ref_columns)
+ foreign_ids.discard(None) # remove NULL ids
if not foreign_ids:
# NULL key. Remember this row and add as usual.
- seen_ids[row_data['id']] = 1
+ seen_ids.add(row_data['id'])
- elif all(_ in seen_ids for _ in foreign_ids):
+ elif foreign_ids.issubset(seen_ids):
# Non-NULL key we've already seen. Remember it and commit
# so we know the old row exists when we add the new one
insert_and_commit()
- seen_ids[row_data['id']] = 1
+ seen_ids.add(row_data['id'])
else:
# Non-NULL future id. Save this and insert it later!
# Attempt to add any spare rows we've collected
for row_data, foreign_ids in deferred_rows:
- if not all(_ in seen_ids for _ in foreign_ids):
+ if not foreign_ids.issubset(seen_ids):
# Could happen if row A refers to B which refers to C.
# This is ridiculous and doesn't happen in my data so far
raise ValueError("Too many levels of self-reference! "