11import sys
22import warnings
33
4+ from django .core .management import call_command
5+ from django .core .management .base import CommandError
46from django .db import transaction
57from django .db .utils import DatabaseError , OperationalError , ProgrammingError
68from netbox .plugins import PluginConfig
@@ -42,28 +44,22 @@ def _is_running_test():
4244 return "test" in sys .argv
4345
4446 @staticmethod
45- def _check_custom_object_type_table_exists ():
47+ def _all_migrations_applied ():
4648 """
47- Check if the CustomObjectType table exists in the database .
48- Returns True if the table exists , False otherwise.
49+ Check if all migrations for this app are applied .
50+ Returns True if all migrations are applied , False otherwise.
4951 """
50- from django .db import connection
51- from .models import CustomObjectType
52-
5352 try :
54- # Use raw SQL to check table existence without generating ORM errors
55- with connection .cursor () as cursor :
56- table_name = CustomObjectType ._meta .db_table
57- cursor .execute ("""
58- SELECT EXISTS (
59- SELECT FROM information_schema.tables
60- WHERE table_name = %s
61- )
62- """ , [table_name ])
63- table_exists = cursor .fetchone ()[0 ]
64- return table_exists
65- except (OperationalError , ProgrammingError , DatabaseError ):
66- # Catch database-specific errors (permission issues, etc.)
53+ call_command (
54+ "migrate" ,
55+ APP_LABEL ,
56+ check = True ,
57+ dry_run = True ,
58+ interactive = False ,
59+ verbosity = 0 ,
60+ )
61+ return True
62+ except (CommandError , Exception ):
6763 return False
6864
6965 def ready (self ):
@@ -80,7 +76,11 @@ def ready(self):
8076 )
8177
8278 # Skip database calls if running during migration or if table doesn't exist
83- if self ._is_running_migration () or not self ._check_custom_object_type_table_exists ():
79+ # or if not all migrations have been applied yet
80+ if (
81+ self ._is_running_migration ()
82+ or not self ._all_migrations_applied ()
83+ ):
8484 super ().ready ()
8585 return
8686
@@ -148,7 +148,11 @@ def get_models(self, include_auto_created=False, include_swapped=False):
148148 )
149149
150150 # Skip custom object type model loading if running during migration
151- if self ._is_running_migration () or not self ._check_custom_object_type_table_exists ():
151+ # or if not all migrations have been applied yet
152+ if (
153+ self ._is_running_migration ()
154+ or not self ._all_migrations_applied ()
155+ ):
152156 return
153157
154158 # Add custom object type models
0 commit comments