netbox icon indicating copy to clipboard operation
netbox copied to clipboard

AutoSyncRecord inconsistent after file change

Open alehaa opened this issue 1 year ago • 3 comments

Deployment Type

Self-hosted

NetBox Version

v3.7.1

Python Version

3.11

Steps to Reproduce

  1. Create data source TestData:

    Field Value
    Name TestData
    Type git
    URL https://github.com/netbox-community/netbox-plugin-tutorial.git
    enabled yes
  2. Sync data source TestData.

  3. Create export template TestExport:

    Field Value
    Name TestExport
    Content Types Circuits > Circuit (but any will do)
    Data Source TestData
    Data File README.md
    Auto Sync Enabled yes
  4. Edit export template TestExport.

  5. Select Data file tutorial/step01-initial-setup.md and save. (will fail).

  6. Go back to edit form for export template TestExport.

  7. Set Data source, Data file and Auto Sync enabled to null and save.

Expected Behavior

Step 5: Edit succeeds. Step 7: AutoSyncRecord is removed from database.

Observed Behavior

Step 5: An IntegrityError error is raised, as SyncedDataMixin's save() method won't change existing AutoSyncRecord objects.

Step 7: Edit succeeds, but the related AutoSyncRecord stays in the database. This is due SyncedDataMixin's save() method will remove related AutoSyncRecord objects, but the QuerySet filter uses fields of the object previously set to None in the edit form.

alehaa avatar Jan 26 '24 14:01 alehaa

Please revise your post above to include explicit instructions. It's not clear exactly which actions are needed to reproduce the bug. Providing a concrete example would be very helpful.

jeremystretch avatar Jan 29 '24 17:01 jeremystretch

@jeremystretch I've updated the instructions to reproduce the issue.

alehaa avatar Feb 01 '24 09:02 alehaa

I believe this could be solved easily by altering the delete mechanism in SyncDataMixin to delete all records that should not be present in the database at the end of save() and delete(). I could check this locally and provide a PR if wanted …

alehaa avatar Feb 09 '24 15:02 alehaa

I believe this could be solved easily by altering the delete mechanism in SyncDataMixin to delete all records that should not be present in the database at the end of save() and delete(). I could check this locally and provide a PR if wanted …

Thanks @alehaa. I think you're on the right track here. We can probably just change get_or_create() to update_or_create() to handle both conditions. I'll submit the change for review in a minute.

jeremystretch avatar Feb 16 '24 13:02 jeremystretch