Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions pyiron_base/database/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,21 @@ def _credentialed_sqalchemy_string(self, prefix: str):

@property
def sql_connection_string(self) -> str:
sql_type = s.configuration["sql_type"]
if sql_type == "Postgres":
return self._credentialed_sqalchemy_string("postgresql")
elif sql_type == "MySQL":
return self._credentialed_sqalchemy_string("mysql+pymysql")
elif sql_type == "SQLalchemy":
return s.configuration["sql_connection_string"]
elif sql_type == "SQLite":
return "sqlite:///" + s.configuration["sql_file"].replace("\\", "/")
if s.configuration["sql_connection_string"] is None:
sql_type = s.configuration["sql_type"]
if sql_type == "Postgres":
return self._credentialed_sqalchemy_string("postgresql")
elif sql_type == "MySQL":
return self._credentialed_sqalchemy_string("mysql+pymysql")
elif sql_type == "SQLite":
return "sqlite:///" + s.configuration["sql_file"].replace("\\", "/")
else:
raise ValueError(
f"Invalid SQL type {sql_type} or SQLalchemy with sql_connection_string=None -- This should have been caught at input processing, please contact the "
f"developers"
)
else:
raise ValueError(
f"Invalid SQL type {sql_type} -- This should have been caught at input processing, please contact the "
f"developers"
)
return s.configuration["sql_connection_string"]

@property
def sql_view_connection_string(self) -> Union[str, None]:
Expand Down
64 changes: 40 additions & 24 deletions pyiron_base/state/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class Settings(metaclass=Singleton):
resource_paths / RESOURCE_PATHS / PYIRONRESOURCEPATHS (list):
project_paths / PROJECT_PATHS / PYIRONPROJECTPATHS (list):
connection_timeout / CONNECTION_TIMEOUT / PYIRONCONNECTIONTIMEOUT (int):
sql_connection_string / CONNECTION / PYIRONSQLCONNECTIONSTRING (str):
sql_connection_string / CONNECTION / PYIRONSQLCONNECTIONSTRING (str|None): If set, this is used to connect to the
database, potentially ignoring other configurations related to the connection to the database.
sql_table_name / JOB_TABLE / PYIRONSQLTABLENAME (str):
sql_file / FILE / PYIRONSQLFILE (str):
sql_host / HOST / PYIRONSQHOST (str):
Expand Down Expand Up @@ -298,32 +299,47 @@ def _valid_sql_types(self) -> List[str]:
def _validate_sql_configuration(self, config: Dict) -> None:
try:
sql_type = config["sql_type"]
if sql_type in ["Postgres", "MySQL"]:
required_keys = ["user", "sql_user_key", "sql_host", "sql_database"]
if not all([k in config.keys() for k in required_keys]):
raise ValueError(
f"For SQL type {sql_type}, {required_keys} are all required but got {config.keys()}"
)
elif sql_type == "SQLite":
sql_file = config["sql_file"]
if sql_file is None:
# SQLite is raising ugly error messages when the database directory does not exist.
raise ValueError(
"For sql_type SQLite, the sql_file must not be None"
)
elif os.path.dirname(sql_file) != "":
os.makedirs(os.path.dirname(sql_file), exist_ok=True)
elif (
sql_type == "SQLalchemy"
and "sql_connection_string" not in config.keys()
):
raise ValueError(
"sql_type was SQLalchemy but did not find a sql_connection_string setting."
)
elif sql_type not in self._valid_sql_types:
if sql_type not in self._valid_sql_types:
raise ValueError(
f"sql_type {sql_type} not recognized, please choose among {self._valid_sql_types}"
)

if "sql_connection_string" not in config.keys():
if sql_type in ["Postgres", "MySQL"]:
required_keys = ["user", "sql_user_key", "sql_host", "sql_database"]
if not all([k in config.keys() for k in required_keys]):
raise ValueError(
f"For SQL type {sql_type}, {required_keys} are all required but got {config.keys()}"
)
elif sql_type == "SQLite":
sql_file = config["sql_file"]
if sql_file is None:
# SQLite is raising ugly error messages when the database directory does not exist.
raise ValueError(
"For sql_type SQLite, the sql_file must not be None"
)
elif os.path.dirname(sql_file) != "":
os.makedirs(os.path.dirname(sql_file), exist_ok=True)
elif sql_type == "SQLalchemy":
raise ValueError(
"sql_type was SQLalchemy but did not find a sql_connection_string setting."
)
elif sql_type != "SQLalchemy":
if any(
[
k in config.keys()
for k in [
"sql_user_key",
"sql_host",
"sql_database",
"sql_file",
]
]
):
logger.warning(
"Found sql settings in the config, which may be ignored due to the sql_connection_string='{sql_connection_string}'."
)

except KeyError:
pass

Expand Down
Loading