fix: seed import update mode fills gaps only — MSFS is primary
Changes behavior: ALL fields now use ONLY_IF_EMPTY policy for existing assets. Previously, AUTO fields (name, make, model, company, place, building_name, floor, etc.) always overwrote existing data, clobbering MSFS-synced and manually-cleaned records. - db_writer.py: update() treats every field as 'skip if existing value non-null' — same as old PRESERVE behavior but universal - sync.py: UPDATE path now fetches current values and only sets fields that are currently NULL/empty - New assets (not found by machine_id) still insert with full seed data - sync.py docstring and stats output updated to reflect new policy Closes #13 — seed import no longer overwrites MSFS data
This commit is contained in:
+16
-20
@@ -350,10 +350,12 @@ class DatabaseWriter:
|
||||
"""
|
||||
Update mode: Upsert by machine_id.
|
||||
|
||||
PRESERVE fields (lat, lng, photo_path, install_date, deployed, pulled_date):
|
||||
MSFS is primary data source — seed import fills gaps only.
|
||||
|
||||
ALL fields (except machine_id PK) are treated as ONLY_IF_EMPTY:
|
||||
Skip update if existing value is non-null.
|
||||
AUTO fields: Always overwrite.
|
||||
machine_id: Never changes (used as match key).
|
||||
|
||||
New assets (not found by machine_id) are inserted with full data.
|
||||
|
||||
Returns dict with counts.
|
||||
"""
|
||||
@@ -408,27 +410,21 @@ class DatabaseWriter:
|
||||
stats["errors"] += 1
|
||||
print(f" ⚠️ Error inserting machine {mid}: {e}")
|
||||
else:
|
||||
# UPDATE existing record with preserve/auto rules
|
||||
# UPDATE existing record — MSFS is primary, seed fills gaps
|
||||
existing_dict = dict(existing)
|
||||
updates = {}
|
||||
preserved_count = 0
|
||||
skipped_count = 0
|
||||
|
||||
for col in update_cols:
|
||||
if col == "machine_id":
|
||||
continue # PK — never update
|
||||
new_val = row.get(col)
|
||||
old_val = existing_dict.get(col)
|
||||
|
||||
if col in PRESERVE_FIELDS:
|
||||
# PRESERVE: skip if existing value is non-null
|
||||
if old_val is not None and old_val != "":
|
||||
preserved_count += 1
|
||||
continue # Don't update
|
||||
# If old is NULL, we can set it
|
||||
updates[col] = new_val
|
||||
elif col == "machine_id":
|
||||
continue # PK — never update
|
||||
else:
|
||||
# AUTO: always overwrite
|
||||
updates[col] = new_val
|
||||
# Skip if existing value is non-null/empty (MSFS or user data wins)
|
||||
if old_val is not None and old_val != "":
|
||||
skipped_count += 1
|
||||
continue
|
||||
updates[col] = new_val
|
||||
|
||||
if updates:
|
||||
# Build dynamic UPDATE
|
||||
@@ -448,7 +444,7 @@ class DatabaseWriter:
|
||||
set_values,
|
||||
)
|
||||
stats["updated"] += 1
|
||||
stats["preserved"] += preserved_count
|
||||
stats["preserved"] += skipped_count
|
||||
except Exception as e:
|
||||
stats["errors"] += 1
|
||||
print(f" ⚠️ Error updating machine {mid}: {e}")
|
||||
@@ -497,7 +493,7 @@ def write_update(machines, db_path):
|
||||
final_count = writer.get_machine_count()
|
||||
print(f" ✅ Inserted: {stats['inserted']}")
|
||||
print(f" 🔄 Updated: {stats['updated']}")
|
||||
print(f" 🛡️ Preserved fields: {stats['preserved']}")
|
||||
print(f" 🛡️ Existing data preserved: {stats['preserved']} fields")
|
||||
print(f" ⏭️ Skipped (no changes): {stats['skipped']}")
|
||||
print(f" 📊 Total in DB: {final_count}")
|
||||
if stats["errors"]:
|
||||
|
||||
Reference in New Issue
Block a user