mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Support for generated columns, closes #1116
This commit is contained in:
parent
c745c2715a
commit
37f87b5e52
3 changed files with 76 additions and 8 deletions
|
|
@ -64,7 +64,7 @@ HASH_LENGTH = 7
|
||||||
|
|
||||||
# Can replace this with Column from sqlite_utils when I add that dependency
|
# Can replace this with Column from sqlite_utils when I add that dependency
|
||||||
Column = namedtuple(
|
Column = namedtuple(
|
||||||
"Column", ("cid", "name", "type", "notnull", "default_value", "is_pk")
|
"Column", ("cid", "name", "type", "notnull", "default_value", "is_pk", "hidden")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -460,11 +460,11 @@ def detect_primary_keys(conn, table):
|
||||||
" Figure out primary keys for a table. "
|
" Figure out primary keys for a table. "
|
||||||
table_info_rows = [
|
table_info_rows = [
|
||||||
row
|
row
|
||||||
for row in conn.execute(f'PRAGMA table_info("{table}")').fetchall()
|
for row in conn.execute(f'PRAGMA table_xinfo("{table}")').fetchall()
|
||||||
if row[-1]
|
if row["pk"]
|
||||||
]
|
]
|
||||||
table_info_rows.sort(key=lambda row: row[-1])
|
table_info_rows.sort(key=lambda row: row["pk"])
|
||||||
return [str(r[1]) for r in table_info_rows]
|
return [str(r["name"]) for r in table_info_rows]
|
||||||
|
|
||||||
|
|
||||||
def get_outbound_foreign_keys(conn, table):
|
def get_outbound_foreign_keys(conn, table):
|
||||||
|
|
@ -572,7 +572,7 @@ def table_columns(conn, table):
|
||||||
def table_column_details(conn, table):
|
def table_column_details(conn, table):
|
||||||
return [
|
return [
|
||||||
Column(*r)
|
Column(*r)
|
||||||
for r in conn.execute(f"PRAGMA table_info({escape_sqlite(table)});").fetchall()
|
for r in conn.execute(f"PRAGMA table_xinfo({escape_sqlite(table)});").fetchall()
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
|
from datasette.app import Datasette
|
||||||
from datasette.plugins import DEFAULT_PLUGINS
|
from datasette.plugins import DEFAULT_PLUGINS
|
||||||
from datasette.utils import detect_json1
|
from datasette.utils import detect_json1, sqlite3
|
||||||
from datasette.version import __version__
|
from datasette.version import __version__
|
||||||
from .fixtures import ( # noqa
|
from .fixtures import ( # noqa
|
||||||
app_client,
|
app_client,
|
||||||
|
|
@ -514,7 +515,14 @@ def test_database_page(app_client):
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "searchable_fts",
|
"name": "searchable_fts",
|
||||||
"columns": ["text1", "text2", "name with . and spaces"],
|
"columns": [
|
||||||
|
"text1",
|
||||||
|
"text2",
|
||||||
|
"name with . and spaces",
|
||||||
|
"searchable_fts",
|
||||||
|
"docid",
|
||||||
|
"__langid",
|
||||||
|
],
|
||||||
"primary_keys": [],
|
"primary_keys": [],
|
||||||
"count": 2,
|
"count": 2,
|
||||||
"hidden": True,
|
"hidden": True,
|
||||||
|
|
@ -1913,3 +1921,46 @@ def test_paginate_using_link_header(app_client, qs):
|
||||||
else:
|
else:
|
||||||
path = None
|
path = None
|
||||||
assert num_pages == 21
|
assert num_pages == 21
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
tuple(
|
||||||
|
map(
|
||||||
|
int,
|
||||||
|
sqlite3.connect(":memory:")
|
||||||
|
.execute("select sqlite_version()")
|
||||||
|
.fetchone()[0]
|
||||||
|
.split("."),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
< (3, 31, 0),
|
||||||
|
reason="generated columns were added in SQLite 3.31.0",
|
||||||
|
)
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_generated_columns_are_visible_in_datasette(tmp_path_factory):
|
||||||
|
db_directory = tmp_path_factory.mktemp("dbs")
|
||||||
|
db_path = db_directory / "test.db"
|
||||||
|
conn = sqlite3.connect(str(db_path))
|
||||||
|
conn.executescript(
|
||||||
|
"""
|
||||||
|
CREATE TABLE deeds (
|
||||||
|
body TEXT,
|
||||||
|
id INT GENERATED ALWAYS AS (json_extract(body, '$.id')) STORED,
|
||||||
|
consideration INT GENERATED ALWAYS AS (json_extract(body, '$.consideration')) STORED
|
||||||
|
);
|
||||||
|
INSERT INTO deeds (body) VALUES ('{
|
||||||
|
"id": 1,
|
||||||
|
"consideration": "This is the consideration"
|
||||||
|
}');
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
datasette = Datasette([db_path])
|
||||||
|
response = await datasette.client.get("/test/deeds.json?_shape=array")
|
||||||
|
assert response.json() == [
|
||||||
|
{
|
||||||
|
"rowid": 1,
|
||||||
|
"body": '{\n "id": 1,\n "consideration": "This is the consideration"\n }',
|
||||||
|
"id": 1,
|
||||||
|
"consideration": "This is the consideration",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
|
||||||
|
|
@ -120,6 +120,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=1,
|
is_pk=1,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
Column(
|
Column(
|
||||||
cid=1,
|
cid=1,
|
||||||
|
|
@ -128,6 +129,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=0,
|
is_pk=0,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
Column(
|
Column(
|
||||||
cid=2,
|
cid=2,
|
||||||
|
|
@ -136,6 +138,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=0,
|
is_pk=0,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
Column(
|
Column(
|
||||||
cid=3,
|
cid=3,
|
||||||
|
|
@ -144,6 +147,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=0,
|
is_pk=0,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
Column(
|
Column(
|
||||||
cid=4,
|
cid=4,
|
||||||
|
|
@ -152,6 +156,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=0,
|
is_pk=0,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
Column(
|
Column(
|
||||||
cid=5,
|
cid=5,
|
||||||
|
|
@ -160,6 +165,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=0,
|
is_pk=0,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
Column(
|
Column(
|
||||||
cid=6,
|
cid=6,
|
||||||
|
|
@ -168,6 +174,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=0,
|
is_pk=0,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
Column(
|
Column(
|
||||||
cid=7,
|
cid=7,
|
||||||
|
|
@ -176,6 +183,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=0,
|
is_pk=0,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
Column(
|
Column(
|
||||||
cid=8,
|
cid=8,
|
||||||
|
|
@ -184,6 +192,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=0,
|
is_pk=0,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
Column(
|
Column(
|
||||||
cid=9,
|
cid=9,
|
||||||
|
|
@ -192,6 +201,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=0,
|
is_pk=0,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|
@ -205,6 +215,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=1,
|
is_pk=1,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
Column(
|
Column(
|
||||||
cid=1,
|
cid=1,
|
||||||
|
|
@ -213,6 +224,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=2,
|
is_pk=2,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
Column(
|
Column(
|
||||||
cid=2,
|
cid=2,
|
||||||
|
|
@ -221,6 +233,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=0,
|
is_pk=0,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
Column(
|
Column(
|
||||||
cid=3,
|
cid=3,
|
||||||
|
|
@ -229,6 +242,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=0,
|
is_pk=0,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
Column(
|
Column(
|
||||||
cid=4,
|
cid=4,
|
||||||
|
|
@ -237,6 +251,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=0,
|
is_pk=0,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
Column(
|
Column(
|
||||||
cid=5,
|
cid=5,
|
||||||
|
|
@ -245,6 +260,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=0,
|
is_pk=0,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
Column(
|
Column(
|
||||||
cid=6,
|
cid=6,
|
||||||
|
|
@ -253,6 +269,7 @@ async def test_table_columns(db, table, expected):
|
||||||
notnull=0,
|
notnull=0,
|
||||||
default_value=None,
|
default_value=None,
|
||||||
is_pk=0,
|
is_pk=0,
|
||||||
|
hidden=0,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue