Suggest --load-extension=spatialite, closes #1115

This commit is contained in:
Simon Willison 2020-11-29 12:13:16 -08:00
commit 09033c08be
2 changed files with 32 additions and 9 deletions

View file

@ -16,6 +16,7 @@ from .app import Datasette, DEFAULT_SETTINGS, SETTINGS, pm
from .utils import ( from .utils import (
StartupError, StartupError,
check_connection, check_connection,
find_spatialite,
parse_metadata, parse_metadata,
ConnectionProblem, ConnectionProblem,
SpatialiteConnectionProblem, SpatialiteConnectionProblem,
@ -537,10 +538,17 @@ async def check_databases(ds):
try: try:
await database.execute_fn(check_connection) await database.execute_fn(check_connection)
except SpatialiteConnectionProblem: except SpatialiteConnectionProblem:
suggestion = ""
try:
find_spatialite()
suggestion = "\n\nTry adding the --load-extension=spatialite option."
except SpatialiteNotFound:
pass
raise click.UsageError( raise click.UsageError(
"It looks like you're trying to load a SpatiaLite" "It looks like you're trying to load a SpatiaLite"
" database without first loading the SpatiaLite module." + " database without first loading the SpatiaLite module."
"\n\nRead more: https://docs.datasette.io/en/stable/spatialite.html" + suggestion
+ "\n\nRead more: https://docs.datasette.io/en/stable/spatialite.html"
) )
except ConnectionProblem as e: except ConnectionProblem as e:
raise click.UsageError( raise click.UsageError(

View file

@ -59,13 +59,28 @@ def test_serve_with_inspect_file_prepopulates_table_counts_cache():
assert {"hithere": 44} == db.cached_table_counts assert {"hithere": 44} == db.cached_table_counts
def test_spatialite_error_if_attempt_to_open_spatialite(): @pytest.mark.parametrize(
runner = CliRunner() "spatialite_paths,should_suggest_load_extension",
result = runner.invoke( (
cli, ["serve", str(pathlib.Path(__file__).parent / "spatialite.db")] ([], False),
) (["/tmp"], True),
assert result.exit_code != 0 ),
assert "trying to load a SpatiaLite database" in result.output )
def test_spatialite_error_if_attempt_to_open_spatialite(
spatialite_paths, should_suggest_load_extension
):
with mock.patch("datasette.utils.SPATIALITE_PATHS", spatialite_paths):
runner = CliRunner()
result = runner.invoke(
cli, ["serve", str(pathlib.Path(__file__).parent / "spatialite.db")]
)
assert result.exit_code != 0
assert "It looks like you're trying to load a SpatiaLite" in result.output
suggestion = "--load-extension=spatialite"
if should_suggest_load_extension:
assert suggestion in result.output
else:
assert suggestion not in result.output
@mock.patch("datasette.utils.SPATIALITE_PATHS", ["/does/not/exist"]) @mock.patch("datasette.utils.SPATIALITE_PATHS", ["/does/not/exist"])