forked from bonzo81/netbox-librenms-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Pr/librems id multi fixes #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
0844143
feat: auto-create librenms_id custom field via post_migrate signal
marcinpsk 8a893ca
feat(multi-server): JSON custom field librenms_id with server managem…
marcinpsk 22d08b5
fix(multi-server): CR review fixes, production hardening, and test co…
marcinpsk 2969704
docs: add all coverage test files to testing guide; tighten VC mock c…
marcinpsk 1eb0e4d
fix: use _norm_serial for master serial skip-check; drop over-require…
marcinpsk dcc25d8
fix: correctly mark missing role on refresh; restrict inventory mock …
marcinpsk cb9837a
fix(utils): surface DeviceType ambiguity instead of silently picking …
marcinpsk 1016c2e
fix(review): address PR #25 findings — VM name-sync, port validation,…
marcinpsk b6b1ce1
fix(tests): apply deferred PR #25 test improvements — cable server_ke…
marcinpsk 887929a
fix(review): address deferred PR #246 findings — vc_domain hash stabi…
marcinpsk e18d942
test(cables): mock ContentType in cable sync tests; remove spurious d…
marcinpsk b76af58
fix(cables): correct VC member interface resolution, fix cable tests
marcinpsk 8c39991
fix(pr25-cr): address CodeRabbit review findings
marcinpsk f156c33
fix(tests): add module-level patch/MagicMock imports to test_view_wiring
marcinpsk f1e4ce5
fix(pr25-cr): address lost CR review findings
marcinpsk bff2a12
fix(pr25-cr): address latest CodeRabbit review findings
marcinpsk 8a28ddb
fix(pr25-cr): address new CodeRabbit findings — ValueError handlers, …
marcinpsk 37aebe3
fix(pr25-cr): use _build_filter_hash in get_import_search_cache_key, …
marcinpsk c060308
chore: restore .devcontainer/README.md to develop version
marcinpsk b125d22
fix(pr25-cr): dedup guard order, remove duplicate ID write, validate …
marcinpsk ead5a5c
fix(pr25-cr): fix data.get message None fallback, extract _normalize_…
marcinpsk 45a527a
fix: CR findings — bulk_import refreshed path, librenms_api message f…
marcinpsk c96d61e
fix: create librenms_id custom field as JSON type for multi-server su…
marcinpsk 823fa5b
Merge branch 'develop' into feature/auto-create-librenms-id
marcinpsk 0251e58
Merge branch 'develop' into feature/auto-create-librenms-id
marcinpsk 147430d
Merge branch 'develop' into pr/librems-id-multi-fixes
marcinpsk dc80d1b
tests: expand coverage with new test files and reorganize existing tests
marcinpsk 1973476
Merge pull request #221 from marcinpsk/feature/auto-create-librenms-id
bonzo81 7c274bf
fix: address review findings — docstring wording, brittle source-pars…
marcinpsk ddf03c5
Merge pull request #253 from marcinpsk/pr/246-split-tests
bonzo81 6c09b39
Merge branch 'develop' into pr/librems-id-multi-fixes
marcinpsk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: marcinpsk/netbox-librenms-plugin
Length of output: 510
🏁 Script executed:
Repository: marcinpsk/netbox-librenms-plugin
Length of output: 2723
Scope post-migrate custom-field work per DB alias.
Line 82 uses a single process-wide
_executedflag, and lines 91 and 112 use default DB managers without.using(db_alias)scoping. In multi-DB migrations, this skips creation for later database aliases or targets the wrong database during migration.🔧 Proposed fix
def _ensure_librenms_id_custom_field(sender, **kwargs): """ Auto-create the 'librenms_id' custom field if it doesn't exist. Runs after migrations via post_migrate signal to ensure tables exist. Uses dispatch_uid to avoid duplicate connections. """ - # Only run once per migrate invocation (post_migrate fires per-app). - # The _executed flag is intentionally never reset: migrations are expected to - # run in short-lived CLI processes (manage.py migrate) where the flag is - # naturally cleared on exit. Long-running processes (e.g. gunicorn workers) - # should not rely on this handler re-executing after startup. - if getattr(_ensure_librenms_id_custom_field, "_executed", False): + db_alias = kwargs.get("using", "default") + + # Run once per DB alias per process (post_migrate fires per-app). + executed_aliases = getattr(_ensure_librenms_id_custom_field, "_executed_aliases", set()) + if db_alias in executed_aliases: return - _ensure_librenms_id_custom_field._executed = True # not reset; see comment above + executed_aliases.add(db_alias) + _ensure_librenms_id_custom_field._executed_aliases = executed_aliases try: from django.contrib.contenttypes.models import ContentType from extras.models import CustomField - cf, created = CustomField.objects.get_or_create( + cf, created = CustomField.objects.using(db_alias).get_or_create( name="librenms_id", defaults={ @@ -112,7 +119,7 @@ def _ensure_librenms_id_custom_field(sender, **kwargs): current_types = set(cf.object_types.values_list("pk", flat=True)) for model in required_models: - ct = ContentType.objects.get_for_model(model) + ct = ContentType.objects.db_manager(db_alias).get_for_model(model) if ct.pk not in current_types: cf.object_types.add(ct)🤖 Prompt for AI Agents