Skip to content

Commit 0db00de

Browse files
authored
Merge pull request #238 from netboxlabs/172-postgres
avoid postgres errors pre migration run
2 parents a3b69c4 + ecf7a83 commit 0db00de

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

netbox_custom_objects/__init__.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import sys
22
import warnings
33

4-
from django.db import transaction
54
from django.db.utils import DatabaseError, OperationalError, ProgrammingError
65
from netbox.plugins import PluginConfig
76

@@ -24,18 +23,23 @@ def check_custom_object_type_table_exists():
2423
Check if the CustomObjectType table exists in the database.
2524
Returns True if the table exists, False otherwise.
2625
"""
26+
from django.db import connection
2727
from .models import CustomObjectType
2828

2929
try:
30-
# Try to query the model - if the table doesn't exist, this will raise an exception
31-
# this check and the transaction.atomic() is only required when running tests as the
32-
# migration check doesn't work correctly in the test environment
33-
with transaction.atomic():
34-
# Force immediate execution by using first()
35-
CustomObjectType.objects.first()
36-
return True
30+
# Use raw SQL to check table existence without generating ORM errors
31+
with connection.cursor() as cursor:
32+
table_name = CustomObjectType._meta.db_table
33+
cursor.execute("""
34+
SELECT EXISTS (
35+
SELECT FROM information_schema.tables
36+
WHERE table_name = %s
37+
)
38+
""", [table_name])
39+
table_exists = cursor.fetchone()[0]
40+
return table_exists
3741
except (OperationalError, ProgrammingError, DatabaseError):
38-
# Catch database-specific errors (table doesn't exist, permission issues, etc.)
42+
# Catch database-specific errors (permission issues, etc.)
3943
return False
4044

4145

0 commit comments

Comments
 (0)