Skip to content

Commit 092d79f

Browse files
fix: unify global config singleton and fix conn() persistence
- instance._global_config now reuses settings.config instead of creating a duplicate Config object. This ensures dj.config["safemode"] = False actually affects self.connection._config["safemode"] reads. - schemas.py now uses _get_singleton_connection() from instance.py instead of the old conn() from connection.py, eliminating the duplicate singleton connection holder. - dj.conn() now only creates a new connection when the singleton doesn't exist or reset=True (not on every call with credentials). - test_uppercase_schema: use prompt=False for drop() calls. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 04a406d commit 092d79f

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

src/datajoint/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,12 @@ def conn(
135135

136136
_check_thread_safe()
137137

138-
# If credentials provided or reset requested, (re)create the singleton
139-
if host is not None or user is not None or password is not None or reset:
138+
# If reset requested, always recreate
139+
# If credentials provided and no singleton exists, create one
140+
# If credentials provided and singleton exists, return existing singleton
141+
if reset or (
142+
instance_module._singleton_connection is None and (host is not None or user is not None or password is not None)
143+
):
140144
# Use provided values or fall back to config
141145
host = host if host is not None else _global_config.database.host
142146
user = user if user is not None else _global_config.database.user

src/datajoint/instance.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from .connection import Connection
1414
from .errors import ThreadSafetyError
15-
from .settings import Config, _create_config
15+
from .settings import Config, _create_config, config as _settings_config
1616

1717
if TYPE_CHECKING:
1818
from .schemas import Schema as SchemaClass
@@ -179,7 +179,8 @@ def __repr__(self) -> str:
179179
# The global config is created at module load time and can be modified
180180
# The singleton connection is created lazily when conn() or Schema() is called
181181

182-
_global_config: Config = _create_config()
182+
# Reuse the config created in settings.py — there must be exactly one global config
183+
_global_config: Config = _settings_config
183184
_singleton_connection: Connection | None = None
184185

185186

src/datajoint/schemas.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import warnings
1717
from typing import TYPE_CHECKING, Any
1818

19-
from .connection import conn
2019
from .errors import AccessError, DataJointError
20+
from .instance import _get_singleton_connection
2121

2222
if TYPE_CHECKING:
2323
from .connection import Connection
@@ -173,7 +173,7 @@ def activate(
173173
if connection is not None:
174174
self.connection = connection
175175
if self.connection is None:
176-
self.connection = conn()
176+
self.connection = _get_singleton_connection()
177177
self.database = schema_name
178178
if create_schema is not None:
179179
self.create_schema = create_schema
@@ -860,7 +860,7 @@ def list_schemas(connection: Connection | None = None) -> list[str]:
860860
"""
861861
return [
862862
r[0]
863-
for r in (connection or conn()).query(
863+
for r in (connection or _get_singleton_connection()).query(
864864
'SELECT schema_name FROM information_schema.schemata WHERE schema_name <> "information_schema"'
865865
)
866866
]

tests/integration/test_schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,5 +265,5 @@ class Recording(dj.Manual):
265265
id: smallint
266266
"""
267267

268-
schema2.drop()
269-
schema1.drop()
268+
schema2.drop(prompt=False)
269+
schema1.drop(prompt=False)

0 commit comments

Comments
 (0)