--crossdb option for joining across databases (#1232)

* Test for cross-database join, refs #283
* Warn if --crossdb used with more than 10 DBs, refs #283
* latest.datasette.io demo of --crossdb joins, refs #283
* Show attached databases on /_memory page, refs #283
* Documentation for cross-database queries, refs #283
This commit is contained in:
Simon Willison 2021-02-18 14:09:12 -08:00 committed by GitHub
commit 6f41c8a2be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 215 additions and 8 deletions

View file

@ -12,7 +12,7 @@ from subprocess import call
import sys
from runpy import run_module
import webbrowser
from .app import Datasette, DEFAULT_SETTINGS, SETTINGS, pm
from .app import Datasette, DEFAULT_SETTINGS, SETTINGS, SQLITE_LIMIT_ATTACHED, pm
from .utils import (
StartupError,
check_connection,
@ -410,6 +410,11 @@ def uninstall(packages, yes):
is_flag=True,
help="Create database files if they do not exist",
)
@click.option(
"--crossdb",
is_flag=True,
help="Enable cross-database joins using the /_memory database",
)
@click.option(
"--ssl-keyfile",
help="SSL key file",
@ -442,6 +447,7 @@ def serve(
pdb,
open_browser,
create,
crossdb,
ssl_keyfile,
ssl_certfile,
return_instance=False,
@ -499,6 +505,7 @@ def serve(
secret=secret,
version_note=version_note,
pdb=pdb,
crossdb=crossdb,
)
# if files is a single directory, use that as config_dir=
@ -591,3 +598,15 @@ async def check_databases(ds):
raise click.UsageError(
f"Connection to {database.path} failed check: {str(e.args[0])}"
)
# If --crossdb and more than SQLITE_LIMIT_ATTACHED show warning
if (
ds.crossdb
and len([db for db in ds.databases.values() if not db.is_memory])
> SQLITE_LIMIT_ATTACHED
):
msg = (
"Warning: --crossdb only works with the first {} attached databases".format(
SQLITE_LIMIT_ATTACHED
)
)
click.echo(click.style(msg, bold=True, fg="yellow"), err=True)