Skip to content

Commit 102196e

Browse files
committed
Require explicit non-SQLite driver URLs
1 parent dcfa145 commit 102196e

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

src/openstatspec/sql/profiles.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ def profile_for_url(database_url: str) -> SqlProfile:
4848
)
4949

5050

51+
52+
def validate_connection_url(database_url: str) -> SqlProfile:
53+
profile = profile_for_url(database_url)
54+
scheme = urlparse(database_url).scheme.lower()
55+
if profile is POSTGRESQL and scheme != "postgresql+psycopg":
56+
raise UnsupportedOperationError("PostgreSQL requires an explicit postgresql+psycopg URL.")
57+
if profile is MYSQL and scheme not in {"mysql+pymysql", "mariadb+mariadbconnector"}:
58+
raise UnsupportedOperationError("MySQL/MariaDB requires an explicit mysql+pymysql or mariadb+mariadbconnector URL.")
59+
return profile
60+
5161
def preflight(profile: SqlProfile, variable_count: int) -> None:
5262
if variable_count > profile.max_physical_variables:
5363
raise UnsupportedOperationError(

src/openstatspec/sql/wide.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import Any
77

88
from sqlalchemy import delete, BigInteger, Column, Float, Integer, MetaData, String, Table, Text, create_engine, insert, select
9-
from .profiles import preflight, profile_for_url
9+
from .profiles import preflight, validate_connection_url
1010

1111
_IDENTIFIER = re.compile(r"[^a-zA-Z0-9_]+")
1212

@@ -101,7 +101,7 @@ def create_wide_dataset(
101101
imported_at: str = "",
102102
multiple_response_sets: str = "{}",
103103
) -> dict[str, Any]:
104-
profile = profile_for_url(database_url)
104+
profile = validate_connection_url(database_url)
105105
preflight(profile, len(variables))
106106
engine = create_engine(database_url)
107107
metadata = MetaData()
@@ -163,7 +163,7 @@ def read_wide_dataset(*, database_url: str, dataset_id: str) -> tuple[dict[str,
163163

164164
def validate_wide_dataset(*, database_url: str, dataset_id: str) -> dict[str, Any]:
165165
dataset, variables, rows = read_wide_dataset(database_url=database_url, dataset_id=dataset_id)
166-
profile = profile_for_url(database_url)
166+
profile = validate_connection_url(database_url)
167167
preflight(profile, len(variables))
168168
if not variables:
169169
raise ValueError("A conforming dataset needs at least one source variable.")

0 commit comments

Comments
 (0)