Read a numeric natural key as a name before reading it as an id
An Azure device import died part-way with a foreign-key error, and the crash was the lucky outcome. The Azure adapter routes a device that already exists to its real asset by writing that asset's assetnumber into the assetid column of the CSV it generates, expecting the engine to look it up. The resolver instead takes any all-digit string at face value as a literal id. This site numbers operations 0NNN, so numeric assetnumbers are ordinary here: `0704` was read as id 704, which does not exist, hence the error. `0600` would have been read as id 600 - a printer - and bound a PC to it silently, reporting success. Two changes, both needed. The resolver now tries the label first for an all-digit string and only falls back to reading it as an id when nothing is named that. The face-value behaviour exists for a real reason - exports round-trip and carry real ids, as its original comment says - so it stays as the fallback rather than being removed. Somebody writing `0600` in a column keyed on assetnumber means the asset called 0600, and binding the row to an unrelated record is far worse than failing. _existing_hostname_map now hands over the assetid rather than the assetnumber. Giving the resolver the id it is going to treat the value as makes the passthrough correct by construction instead of correct by luck. Verified against a restored copy of the production database: 234 devices, 221 matching an existing computer and updating in place, 13 new, no problems. The same run previously failed on the foreign key. Neither half was wrong alone. The passthrough dates from the original CSV import and the hostname map from the wilm merge months later; they only meet at a site whose natural keys are numeric. Scope: product
This commit is contained in:
@@ -427,12 +427,41 @@ class Resolver:
|
||||
def resolve(self, tablename, idcolumn, value):
|
||||
if value is None:
|
||||
return None
|
||||
# A number is taken at face value: exports round-trip, and some sites do
|
||||
# know their ids.
|
||||
if isinstance(value, int) or (isinstance(value, str) and value.strip().isdigit()):
|
||||
# An int is an id. It cannot be anything else.
|
||||
if isinstance(value, int):
|
||||
return int(value)
|
||||
|
||||
label = _label_column(tablename)
|
||||
|
||||
# An ALL-DIGIT STRING IS AMBIGUOUS, and used to be taken at face value
|
||||
# as an id. That is wrong wherever a site's natural keys are numeric,
|
||||
# and this one numbers operations 0NNN: assetnumber `0600` was read as
|
||||
# id 600, which is a different asset - a printer, in the case that
|
||||
# found this - or none at all, which surfaced as a foreign-key error
|
||||
# partway through an Azure device import.
|
||||
#
|
||||
# The label wins when one matches. Somebody writing `0600` in a column
|
||||
# keyed on assetnumber means the asset called 0600, and silently
|
||||
# binding a row to an unrelated record is far worse than the error.
|
||||
# Fall through to the id reading only when no label matches, which
|
||||
# keeps round-tripped exports and sites that do know their ids working.
|
||||
if isinstance(value, str) and value.strip().isdigit():
|
||||
if label:
|
||||
key = (tablename, value.strip().lower())
|
||||
if key in self._cache:
|
||||
return self._cache[key]
|
||||
table = db.metadata.tables[tablename]
|
||||
row = db.session.execute(
|
||||
table.select().where(
|
||||
db.func.lower(db.func.trim(table.c[label]))
|
||||
== value.strip().lower())
|
||||
).first()
|
||||
if row is not None:
|
||||
found = getattr(row, idcolumn)
|
||||
self._cache[key] = found
|
||||
return found
|
||||
return int(value)
|
||||
|
||||
if not label:
|
||||
raise ValueError(
|
||||
"cannot look up %s by name - give the numeric %s instead" % (tablename, idcolumn))
|
||||
|
||||
Reference in New Issue
Block a user