mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
--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:
parent
4df548e766
commit
6f41c8a2be
13 changed files with 215 additions and 8 deletions
75
tests/test_crossdb.py
Normal file
75
tests/test_crossdb.py
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
from datasette.cli import cli
|
||||
from click.testing import CliRunner
|
||||
import urllib
|
||||
import sqlite3
|
||||
from .fixtures import app_client_two_attached_databases_crossdb_enabled
|
||||
|
||||
|
||||
def test_crossdb_join(app_client_two_attached_databases_crossdb_enabled):
|
||||
app_client = app_client_two_attached_databases_crossdb_enabled
|
||||
sql = """
|
||||
select
|
||||
'extra database' as db,
|
||||
pk,
|
||||
text1,
|
||||
text2
|
||||
from
|
||||
[extra database].searchable
|
||||
union all
|
||||
select
|
||||
'fixtures' as db,
|
||||
pk,
|
||||
text1,
|
||||
text2
|
||||
from
|
||||
fixtures.searchable
|
||||
"""
|
||||
response = app_client.get(
|
||||
"/_memory.json?" + urllib.parse.urlencode({"sql": sql, "_shape": "array"})
|
||||
)
|
||||
assert response.status == 200
|
||||
assert response.json == [
|
||||
{"db": "extra database", "pk": 1, "text1": "barry cat", "text2": "terry dog"},
|
||||
{"db": "extra database", "pk": 2, "text1": "terry dog", "text2": "sara weasel"},
|
||||
{"db": "fixtures", "pk": 1, "text1": "barry cat", "text2": "terry dog"},
|
||||
{"db": "fixtures", "pk": 2, "text1": "terry dog", "text2": "sara weasel"},
|
||||
]
|
||||
|
||||
|
||||
def test_crossdb_warning_if_too_many_databases(tmp_path_factory):
|
||||
db_dir = tmp_path_factory.mktemp("dbs")
|
||||
dbs = []
|
||||
for i in range(11):
|
||||
path = str(db_dir / "db_{}.db".format(i))
|
||||
conn = sqlite3.connect(path)
|
||||
conn.execute("vacuum")
|
||||
dbs.append(path)
|
||||
runner = CliRunner(mix_stderr=False)
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
[
|
||||
"serve",
|
||||
"--crossdb",
|
||||
"--get",
|
||||
"/",
|
||||
]
|
||||
+ dbs,
|
||||
catch_exceptions=False,
|
||||
)
|
||||
assert (
|
||||
"Warning: --crossdb only works with the first 10 attached databases"
|
||||
in result.stderr
|
||||
)
|
||||
|
||||
|
||||
def test_crossdb_attached_database_list_display(
|
||||
app_client_two_attached_databases_crossdb_enabled,
|
||||
):
|
||||
app_client = app_client_two_attached_databases_crossdb_enabled
|
||||
response = app_client.get("/_memory")
|
||||
for fragment in (
|
||||
"databases are attached to this connection",
|
||||
"<li><strong>fixtures</strong> - ",
|
||||
"<li><strong>extra database</strong> - ",
|
||||
):
|
||||
assert fragment in response.text
|
||||
Loading…
Add table
Add a link
Reference in a new issue