Skip to content

Commit

Permalink
Interpret filepaths as SQLite URIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielballan committed Oct 2, 2024
1 parent 1f1efff commit 7679190
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
10 changes: 10 additions & 0 deletions tiled/_tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,13 @@ def test_ensure_specified_sql_driver():
ensure_specified_sql_driver("sqlite:///test.db")
== "sqlite+aiosqlite:///test.db"
)
# Filepaths are implicitly SQLite databases.
# Relative path
assert ensure_specified_sql_driver("test.db") == "sqlite+aiosqlite:///test.db"
# Relative path anchored to .
assert ensure_specified_sql_driver("./test.db") == "sqlite+aiosqlite:///test.db"
# Absolute path
assert (
ensure_specified_sql_driver("/tmp/test.db")
== "sqlite+aiosqlite:////tmp/test.db"
)
4 changes: 0 additions & 4 deletions tiled/catalog/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
from ..server.schemas import Asset, DataSource, Management, Revision, Spec
from ..structures.core import StructureFamily
from ..utils import (
SCHEME_PATTERN,
UNCHANGED,
Conflicts,
OneShotCachedMap,
Expand Down Expand Up @@ -1367,9 +1366,6 @@ def from_uri(
stderr = process.stderr.decode()
logging.info(f"Subprocess stdout: {stdout}")
logging.error(f"Subprocess stderr: {stderr}")
if not SCHEME_PATTERN.match(uri):
# Interpret URI as filepath.
uri = f"sqlite+aiosqlite:///{uri}"

parsed_url = make_url(uri)
if (parsed_url.get_dialect().name == "sqlite") and (
Expand Down
5 changes: 1 addition & 4 deletions tiled/commandline/_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ def init(
from ..alembic_utils import UninitializedDatabase, check_database, stamp_head
from ..catalog.alembic_constants import ALEMBIC_DIR, ALEMBIC_INI_TEMPLATE_PATH
from ..catalog.core import ALL_REVISIONS, REQUIRED_REVISION, initialize_database
from ..utils import SCHEME_PATTERN, ensure_specified_sql_driver
from ..utils import ensure_specified_sql_driver

if not SCHEME_PATTERN.match(database):
# Interpret URI as filepath.
database = f"sqlite+aiosqlite:///{database}"
database = ensure_specified_sql_driver(database)

async def do_setup():
Expand Down
4 changes: 4 additions & 0 deletions tiled/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,11 @@ def ensure_specified_sql_driver(uri: str) -> str:
'sqlite://...' -> 'sqlite+aiosqlite://...'
'postgresql+asyncpg://...' -> 'postgresql+asynpg://...'
'postgresql+my_custom_driver://...' -> 'postgresql+my_custom_driver://...'
'/path/to/file.db' -> 'sqlite+aiosqlite:////path/to/file.db'
"""
if not SCHEME_PATTERN.match(uri):
# Interpret URI as filepath.
uri = f"sqlite+aiosqlite:///{Path(uri)}"
scheme, rest = uri.split(":", 1)
new_scheme = SCHEME_TO_SCHEME_PLUS_DRIVER.get(scheme, scheme)
return ":".join([new_scheme, rest])
Expand Down

0 comments on commit 7679190

Please sign in to comment.