mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
generated_columns table in fixtures.py, closes #1119
This commit is contained in:
parent
461670a0b8
commit
17cbbb1f7f
6 changed files with 93 additions and 65 deletions
|
|
@ -1,5 +1,5 @@
|
|||
from datasette.app import Datasette
|
||||
from datasette.utils.sqlite import sqlite3
|
||||
from datasette.utils.sqlite import sqlite3, sqlite_version, supports_generated_columns
|
||||
from datasette.utils.testing import TestClient
|
||||
import click
|
||||
import contextlib
|
||||
|
|
@ -116,6 +116,8 @@ def make_app_client(
|
|||
immutables = []
|
||||
conn = sqlite3.connect(filepath)
|
||||
conn.executescript(TABLES)
|
||||
if supports_generated_columns():
|
||||
conn.executescript(GENERATED_COLUMNS_SQL)
|
||||
for sql, params in TABLE_PARAMETERIZED_SQL:
|
||||
with conn:
|
||||
conn.execute(sql, params)
|
||||
|
|
@ -699,6 +701,18 @@ INSERT INTO "searchable_fts" (rowid, text1, text2)
|
|||
SELECT rowid, text1, text2 FROM searchable;
|
||||
"""
|
||||
|
||||
GENERATED_COLUMNS_SQL = """
|
||||
CREATE TABLE generated_columns (
|
||||
body TEXT,
|
||||
id INT GENERATED ALWAYS AS (json_extract(body, '$.number')) STORED,
|
||||
consideration INT GENERATED ALWAYS AS (json_extract(body, '$.string')) STORED
|
||||
);
|
||||
INSERT INTO generated_columns (body) VALUES ('{
|
||||
"number": 1,
|
||||
"string": "This is a string"
|
||||
}');
|
||||
"""
|
||||
|
||||
|
||||
def assert_permissions_checked(datasette, actions):
|
||||
# actions is a list of "action" or (action, resource) tuples
|
||||
|
|
@ -754,6 +768,9 @@ def cli(db_filename, metadata, plugins_path, recreate):
|
|||
for sql, params in TABLE_PARAMETERIZED_SQL:
|
||||
with conn:
|
||||
conn.execute(sql, params)
|
||||
if supports_generated_columns():
|
||||
with conn:
|
||||
conn.executescript(GENERATED_COLUMNS_SQL)
|
||||
print(f"Test tables written to {db_filename}")
|
||||
if metadata:
|
||||
open(metadata, "w").write(json.dumps(METADATA, indent=4))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue