Skip to content

Commit a8a1814

Browse files
Merge pull request #1346 from datajoint/fix/conn-no-user-input
v2.0: Remove conn() prompts + add schema_prefix and create_tables config
2 parents 3007218 + 460f03e commit a8a1814

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

src/datajoint/connection.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import re
1212
import warnings
1313
from contextlib import contextmanager
14-
from getpass import getpass
1514
from typing import Callable
1615

1716
import pymysql as client
@@ -110,9 +109,9 @@ def conn(
110109
host : str, optional
111110
Database hostname.
112111
user : str, optional
113-
MySQL username.
112+
Database username. Required if not set in config.
114113
password : str, optional
115-
MySQL password. Prompts if not provided.
114+
Database password. Required if not set in config.
116115
init_fun : callable, optional
117116
Initialization function called after connection.
118117
reset : bool, optional
@@ -125,15 +124,24 @@ def conn(
125124
-------
126125
Connection
127126
Persistent database connection.
127+
128+
Raises
129+
------
130+
DataJointError
131+
If user or password is not provided and not set in config.
128132
"""
129133
if not hasattr(conn, "connection") or reset:
130134
host = host if host is not None else config["database.host"]
131135
user = user if user is not None else config["database.user"]
132136
password = password if password is not None else config["database.password"]
133137
if user is None:
134-
user = input("Please enter DataJoint username: ")
138+
raise errors.DataJointError(
139+
"Database user not configured. Set datajoint.config['database.user'] or pass user= argument."
140+
)
135141
if password is None:
136-
password = getpass(prompt="Please enter DataJoint password: ")
142+
raise errors.DataJointError(
143+
"Database password not configured. Set datajoint.config['database.password'] or pass password= argument."
144+
)
137145
init_fun = init_fun if init_fun is not None else config["connection.init_function"]
138146
use_tls = use_tls if use_tls is not None else config["database.use_tls"]
139147
conn.connection = Connection(host, user, password, None, init_fun, use_tls)

src/datajoint/schemas.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class Schema:
7272
create_schema : bool, optional
7373
If False, raise error if schema doesn't exist. Default True.
7474
create_tables : bool, optional
75-
If False, raise error when accessing missing tables. Default True.
75+
If False, raise error when accessing missing tables.
76+
Default from ``dj.config.database.create_tables`` (True unless configured).
7677
add_objects : dict, optional
7778
Additional objects for the declaration context.
7879
@@ -93,7 +94,7 @@ def __init__(
9394
*,
9495
connection: Connection | None = None,
9596
create_schema: bool = True,
96-
create_tables: bool = True,
97+
create_tables: bool | None = None,
9798
add_objects: dict[str, Any] | None = None,
9899
) -> None:
99100
"""
@@ -110,15 +111,16 @@ def __init__(
110111
create_schema : bool, optional
111112
If False, raise error if schema doesn't exist. Default True.
112113
create_tables : bool, optional
113-
If False, raise error when accessing missing tables. Default True.
114+
If False, raise error when accessing missing tables.
115+
Default from ``dj.config.database.create_tables`` (True unless configured).
114116
add_objects : dict, optional
115117
Additional objects for the declaration context.
116118
"""
117119
self.connection = connection
118120
self.database = None
119121
self.context = context
120122
self.create_schema = create_schema
121-
self.create_tables = create_tables
123+
self.create_tables = create_tables if create_tables is not None else config.database.create_tables
122124
self.add_objects = add_objects
123125
self.declare_list = []
124126
if schema_name:

src/datajoint/settings.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
"database.user": "DJ_USER",
6161
"database.password": "DJ_PASS",
6262
"database.port": "DJ_PORT",
63+
"database.schema_prefix": "DJ_SCHEMA_PREFIX",
64+
"database.create_tables": "DJ_CREATE_TABLES",
6365
"loglevel": "DJ_LOG_LEVEL",
6466
}
6567

@@ -185,6 +187,18 @@ class DatabaseSettings(BaseSettings):
185187
port: int = Field(default=3306, validation_alias="DJ_PORT")
186188
reconnect: bool = True
187189
use_tls: bool | None = None
190+
schema_prefix: str = Field(
191+
default="",
192+
validation_alias="DJ_SCHEMA_PREFIX",
193+
description="Project-specific prefix for schema names. "
194+
"Not automatically applied; use dj.config.database.schema_prefix when creating schemas.",
195+
)
196+
create_tables: bool = Field(
197+
default=True,
198+
validation_alias="DJ_CREATE_TABLES",
199+
description="Default for Schema create_tables parameter. "
200+
"Set to False for production mode to prevent automatic table creation.",
201+
)
188202

189203

190204
class ConnectionSettings(BaseSettings):

src/datajoint/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# version bump auto managed by Github Actions:
22
# label_prs.yaml(prep), release.yaml(bump), post_release.yaml(edit)
33
# manually set this version will be eventually overwritten by the above actions
4-
__version__ = "2.0.0a22"
4+
__version__ = "2.0.0a24"

0 commit comments

Comments
 (0)