Skip to content

Commit f03b599

Browse files
fix: Remove unused init_fun/init_function setting
Removed init_function from ConnectionSettings and all connection code paths. This was dead code - a pass-through to PyMySQL's init_command that was never used by DataJoint. Removed from: - ConnectionSettings class (now removed entirely) - Config.connection field - Template generation - dj.conn() function - Connection.__init__ - Connection.from_config - MySQL adapter - Thread-safe mode spec Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 2d0b19e commit f03b599

File tree

6 files changed

+13
-51
lines changed

6 files changed

+13
-51
lines changed

docs/design/thread-safe-mode.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ Connection parameters (set at creation, read-only after):
6969
| `password` | str | *required* | Database password |
7070
| `backend` | str | "mysql" | Database backend |
7171
| `use_tls` | bool/dict | None | TLS configuration |
72-
| `init_function` | str | None | SQL init command |
7372

7473
---
7574

@@ -375,7 +374,7 @@ class Config(BaseSettings):
375374

376375
```python
377376
class Connection:
378-
def __init__(self, host, user, password, port=None, init_fun=None,
377+
def __init__(self, host, user, password, port=None,
379378
use_tls=None, backend=None, *, _config=None):
380379
# ... existing connection setup ...
381380

@@ -387,8 +386,7 @@ class Connection:
387386
def from_config(cls, cfg=None, *, host=None, user=None, password=None,
388387
port=None, backend=None, safemode=None, stores=None,
389388
database_prefix=None, cache=None, query_cache=None,
390-
reconnect=None, init_fun=None,
391-
use_tls=None) -> "Connection":
389+
reconnect=None, use_tls=None) -> "Connection":
392390
"""
393391
Create connection with explicit configuration.
394392
@@ -413,7 +411,6 @@ class Connection:
413411
user=effective_user,
414412
password=effective_password,
415413
port=effective_port,
416-
init_fun=effective_init_fun,
417414
use_tls=effective_use_tls,
418415
backend=effective_backend,
419416
_config=conn_config,

src/datajoint/adapters/mysql.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ def connect(
7575
Password for authentication.
7676
**kwargs : Any
7777
Additional MySQL-specific parameters:
78-
- init_command: SQL initialization command
7978
- ssl: TLS/SSL configuration dict (deprecated, use use_tls)
8079
- use_tls: bool or dict - DataJoint's SSL parameter (preferred)
8180
@@ -84,7 +83,6 @@ def connect(
8483
pymysql.Connection
8584
MySQL connection object.
8685
"""
87-
init_command = kwargs.get("init_command")
8886
# Handle both ssl (old) and use_tls (new) parameter names
8987
ssl_config = kwargs.get("use_tls", kwargs.get("ssl"))
9088
# Convert boolean True to dict for PyMySQL (PyMySQL expects dict or SSLContext)
@@ -97,7 +95,6 @@ def connect(
9795
"port": port,
9896
"user": user,
9997
"passwd": password,
100-
"init_command": init_command,
10198
"sql_mode": "NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,"
10299
"STRICT_ALL_TABLES,NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY",
103100
"autocommit": True, # DataJoint manages transactions explicitly

src/datajoint/connection.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ def conn(
250250
user: str | None = None,
251251
password: str | None = None,
252252
*,
253-
init_fun: Callable | None = None,
254253
reset: bool = False,
255254
use_tls: bool | dict | None = None,
256255
) -> "Connection":
@@ -275,8 +274,6 @@ def conn(
275274
Database username. Required if not set in config.
276275
password : str, optional
277276
Database password. Required if not set in config.
278-
init_fun : callable, optional
279-
Initialization function called after connection.
280277
reset : bool, optional
281278
If True, reset existing connection. Default False.
282279
use_tls : bool or dict, optional
@@ -317,9 +314,8 @@ def conn(
317314
raise errors.DataJointError(
318315
"Database password not configured. Set datajoint.config['database.password'] or pass password= argument."
319316
)
320-
init_fun = init_fun if init_fun is not None else config["connection.init_function"]
321317
use_tls = use_tls if use_tls is not None else config["database.use_tls"]
322-
conn.connection = Connection(host, user, password, None, init_fun, use_tls)
318+
conn.connection = Connection(host, user, password, None, use_tls)
323319
return conn.connection
324320

325321

@@ -364,8 +360,6 @@ class Connection:
364360
Database password.
365361
port : int, optional
366362
Port number. Overridden if specified in host.
367-
init_fun : str, optional
368-
SQL initialization command.
369363
use_tls : bool or dict, optional
370364
TLS encryption option.
371365
backend : str, optional
@@ -388,7 +382,6 @@ def __init__(
388382
user: str,
389383
password: str,
390384
port: int | None = None,
391-
init_fun: str | None = None,
392385
use_tls: bool | dict | None = None,
393386
backend: str | None = None,
394387
*,
@@ -417,7 +410,6 @@ def __init__(
417410
# use_tls=True: enable SSL with default settings
418411
self.conn_info["ssl"] = True
419412
self.conn_info["ssl_input"] = use_tls
420-
self.init_fun = init_fun
421413
self._conn = None
422414
self._query_cache = None
423415
self._is_closed = True # Mark as closed until connect() succeeds
@@ -454,7 +446,6 @@ def from_config(
454446
password: str | None = None,
455447
port: int | None = None,
456448
backend: str | None = None,
457-
init_fun: str | None = None,
458449
use_tls: bool | dict | None = None,
459450
# Connection-scoped settings
460451
safemode: bool | None = None,
@@ -494,8 +485,6 @@ def from_config(
494485
backend : str, optional
495486
Database backend ('mysql' or 'postgresql'). Overrides cfg['backend'].
496487
Default: 'mysql'.
497-
init_fun : str, optional
498-
SQL initialization command. Overrides cfg['init_function'].
499488
use_tls : bool or dict, optional
500489
TLS encryption option. Overrides cfg['use_tls'].
501490
safemode : bool, optional
@@ -565,7 +554,6 @@ def from_config(
565554
effective_password = None
566555
effective_port = None # Will be set based on backend
567556
effective_backend = "mysql"
568-
effective_init_fun = None
569557
effective_use_tls = None
570558

571559
# Connection-scoped settings (will be passed to ConnectionConfig)
@@ -584,8 +572,6 @@ def from_config(
584572
effective_port = cfg["port"]
585573
if "backend" in cfg:
586574
effective_backend = cfg["backend"]
587-
if "init_function" in cfg:
588-
effective_init_fun = cfg["init_function"]
589575
if "use_tls" in cfg:
590576
effective_use_tls = cfg["use_tls"]
591577

@@ -624,8 +610,6 @@ def from_config(
624610
effective_port = port
625611
if backend is not None:
626612
effective_backend = backend
627-
if init_fun is not None:
628-
effective_init_fun = init_fun
629613
if use_tls is not None:
630614
effective_use_tls = use_tls
631615

@@ -679,7 +663,6 @@ def from_config(
679663
user=effective_user,
680664
password=effective_password,
681665
port=effective_port,
682-
init_fun=effective_init_fun,
683666
use_tls=effective_use_tls,
684667
backend=effective_backend,
685668
_config=conn_config,
@@ -705,7 +688,6 @@ def connect(self) -> None:
705688
port=self.conn_info["port"],
706689
user=self.conn_info["user"],
707690
password=self.conn_info["passwd"],
708-
init_command=self.init_fun,
709691
use_tls=self.conn_info.get("ssl"),
710692
)
711693
except Exception as ssl_error:
@@ -721,7 +703,6 @@ def connect(self) -> None:
721703
port=self.conn_info["port"],
722704
user=self.conn_info["user"],
723705
password=self.conn_info["passwd"],
724-
init_command=self.init_fun,
725706
use_tls=False, # Explicitly disable SSL for fallback
726707
)
727708
else:

src/datajoint/settings.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,6 @@ def set_default_port_from_backend(self) -> "DatabaseSettings":
219219
return self
220220

221221

222-
class ConnectionSettings(BaseSettings):
223-
"""Connection behavior settings."""
224-
225-
model_config = SettingsConfigDict(extra="forbid", validate_assignment=True)
226-
227-
init_function: str | None = None
228-
229-
230222
class DisplaySettings(BaseSettings):
231223
"""Display and preview settings."""
232224

@@ -325,7 +317,6 @@ class Config(BaseSettings):
325317

326318
# Nested settings groups
327319
database: DatabaseSettings = Field(default_factory=DatabaseSettings)
328-
connection: ConnectionSettings = Field(default_factory=ConnectionSettings)
329320
display: DisplaySettings = Field(default_factory=DisplaySettings)
330321
jobs: JobsSettings = Field(default_factory=JobsSettings)
331322

@@ -827,9 +818,6 @@ def save_template(
827818
"reconnect": True,
828819
"use_tls": None,
829820
},
830-
"connection": {
831-
"init_function": None,
832-
},
833821
"display": {
834822
"limit": 12,
835823
"width": 14,

tests/unit/test_settings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,6 @@ def test_save_full_template(self, tmp_path):
561561

562562
# Full template should have all settings groups
563563
assert "database" in content
564-
assert "connection" in content
565564
assert "display" in content
566565
assert "stores" in content
567566
assert "loglevel" in content

tests/unit/test_thread_safe.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def test_from_config_with_explicit_params(self):
128128

129129
captured_args = {}
130130

131-
def mock_init(self, host, user, password, port=None, init_fun=None, use_tls=None, backend=None, *, _config=None):
131+
def mock_init(self, host, user, password, port=None, use_tls=None, backend=None, *, _config=None):
132132
captured_args["host"] = host
133133
captured_args["user"] = user
134134
captured_args["port"] = port
@@ -158,7 +158,7 @@ def test_from_config_with_dict(self):
158158

159159
captured_args = {}
160160

161-
def mock_init(self, host, user, password, port=None, init_fun=None, use_tls=None, backend=None, *, _config=None):
161+
def mock_init(self, host, user, password, port=None, use_tls=None, backend=None, *, _config=None):
162162
captured_args["host"] = host
163163
captured_args["port"] = port
164164

@@ -175,7 +175,7 @@ def test_from_config_kwargs_override_dict(self):
175175
cfg = {"host": "dicthost", "user": "dictuser", "password": "dictpass"}
176176
captured_args = {}
177177

178-
def mock_init(self, host, user, password, port=None, init_fun=None, use_tls=None, backend=None, *, _config=None):
178+
def mock_init(self, host, user, password, port=None, use_tls=None, backend=None, *, _config=None):
179179
captured_args["host"] = host
180180
captured_args["user"] = user
181181

@@ -193,7 +193,7 @@ def test_from_config_works_in_thread_safe_mode(self):
193193

194194
captured_args = {}
195195

196-
def mock_init(self, host, user, password, port=None, init_fun=None, use_tls=None, backend=None, *, _config=None):
196+
def mock_init(self, host, user, password, port=None, use_tls=None, backend=None, *, _config=None):
197197
captured_args["host"] = host
198198

199199
with patch.object(dj.Connection, "__init__", mock_init):
@@ -212,7 +212,7 @@ def test_from_config_default_port_mysql(self):
212212

213213
captured_args = {}
214214

215-
def mock_init(self, host, user, password, port=None, init_fun=None, use_tls=None, backend=None, *, _config=None):
215+
def mock_init(self, host, user, password, port=None, use_tls=None, backend=None, *, _config=None):
216216
captured_args["port"] = port
217217
captured_args["backend"] = backend
218218

@@ -228,7 +228,7 @@ def test_from_config_default_port_postgresql(self):
228228

229229
captured_args = {}
230230

231-
def mock_init(self, host, user, password, port=None, init_fun=None, use_tls=None, backend=None, *, _config=None):
231+
def mock_init(self, host, user, password, port=None, use_tls=None, backend=None, *, _config=None):
232232
captured_args["port"] = port
233233

234234
with patch.object(dj.Connection, "__init__", mock_init):
@@ -401,7 +401,7 @@ def test_from_config_creates_connection_config(self):
401401

402402
captured_config = {}
403403

404-
def mock_init(self, host, user, password, port=None, init_fun=None, use_tls=None, backend=None, *, _config=None):
404+
def mock_init(self, host, user, password, port=None, use_tls=None, backend=None, *, _config=None):
405405
captured_config["config"] = _config
406406

407407
with patch.object(dj.Connection, "__init__", mock_init):
@@ -417,7 +417,7 @@ def test_from_config_passes_all_settings(self):
417417

418418
captured_config = {}
419419

420-
def mock_init(self, host, user, password, port=None, init_fun=None, use_tls=None, backend=None, *, _config=None):
420+
def mock_init(self, host, user, password, port=None, use_tls=None, backend=None, *, _config=None):
421421
captured_config["config"] = _config
422422

423423
with patch.object(dj.Connection, "__init__", mock_init):
@@ -443,7 +443,7 @@ def test_from_config_extracts_settings_from_dict(self):
443443

444444
captured_config = {}
445445

446-
def mock_init(self, host, user, password, port=None, init_fun=None, use_tls=None, backend=None, *, _config=None):
446+
def mock_init(self, host, user, password, port=None, use_tls=None, backend=None, *, _config=None):
447447
captured_config["config"] = _config
448448

449449
cfg_dict = {
@@ -471,7 +471,7 @@ def test_from_config_does_not_use_global_fallback(self):
471471

472472
captured_config = {}
473473

474-
def mock_init(self, host, user, password, port=None, init_fun=None, use_tls=None, backend=None, *, _config=None):
474+
def mock_init(self, host, user, password, port=None, use_tls=None, backend=None, *, _config=None):
475475
captured_config["config"] = _config
476476

477477
try:

0 commit comments

Comments
 (0)