Fixed remaining code and docs for new block=True default, closes #1579

This commit is contained in:
Simon Willison 2021-12-23 11:18:20 -08:00
commit 8c401ee0f0
6 changed files with 19 additions and 39 deletions

View file

@ -367,7 +367,6 @@ class Datasette:
VALUES (?, ?, ?, ?)
""",
[database_name, str(db.path), db.is_memory, schema_version],
block=True,
)
await populate_schema_tables(internal_db, db)

View file

@ -62,7 +62,7 @@ async def init_internal_db(db):
);
"""
).strip()
await db.execute_write_script(create_tables_sql, block=True)
await db.execute_write_script(create_tables_sql)
async def populate_schema_tables(internal_db, db):
@ -76,7 +76,7 @@ async def populate_schema_tables(internal_db, db):
)
conn.execute("DELETE FROM indexes WHERE database_name = ?", [database_name])
await internal_db.execute_write_fn(delete_everything, block=True)
await internal_db.execute_write_fn(delete_everything)
tables = (await db.execute("select * from sqlite_master WHERE type = 'table'")).rows
@ -137,7 +137,6 @@ async def populate_schema_tables(internal_db, db):
values (?, ?, ?, ?)
""",
tables_to_insert,
block=True,
)
await internal_db.execute_write_many(
"""
@ -148,7 +147,6 @@ async def populate_schema_tables(internal_db, db):
)
""",
columns_to_insert,
block=True,
)
await internal_db.execute_write_many(
"""
@ -159,7 +157,6 @@ async def populate_schema_tables(internal_db, db):
)
""",
foreign_keys_to_insert,
block=True,
)
await internal_db.execute_write_many(
"""
@ -170,5 +167,4 @@ async def populate_schema_tables(internal_db, db):
)
""",
indexes_to_insert,
block=True,
)

View file

@ -279,7 +279,7 @@ class QueryView(DataView):
ok = None
try:
cursor = await self.ds.databases[database].execute_write(
sql, params_for_query, block=True
sql, params_for_query
)
message = metadata.get(
"on_success_message"

View file

@ -770,7 +770,7 @@ Or you can return an async function which will be awaited on startup. Use this o
if "my_table" not in await db.table_names():
await db.execute_write("""
create table my_table (mycol text)
""", block=True)
""")
return inner
Potential use-cases:

View file

@ -408,16 +408,14 @@ async def test_array_facet_results(app_client):
async def test_array_facet_handle_duplicate_tags():
ds = Datasette([], memory=True)
db = ds.add_database(Database(ds, memory_name="test_array_facet"))
await db.execute_write("create table otters(name text, tags text)", block=True)
await db.execute_write("create table otters(name text, tags text)")
for name, tags in (
("Charles", ["friendly", "cunning", "friendly"]),
("Shaun", ["cunning", "empathetic", "friendly"]),
("Tracy", ["empathetic", "eager"]),
):
await db.execute_write(
"insert into otters (name, tags) values (?, ?)",
[name, json.dumps(tags)],
block=True,
"insert into otters (name, tags) values (?, ?)", [name, json.dumps(tags)]
)
response = await ds.client.get("/test_array_facet/otters.json?_facet_array=tags")
@ -516,11 +514,9 @@ async def test_date_facet_results(app_client):
async def test_json_array_with_blanks_and_nulls():
ds = Datasette([], memory=True)
db = ds.add_database(Database(ds, memory_name="test_json_array"))
await db.execute_write("create table foo(json_column text)", block=True)
await db.execute_write("create table foo(json_column text)")
for value in ('["a", "b", "c"]', '["a", "b"]', "", None):
await db.execute_write(
"insert into foo (json_column) values (?)", [value], block=True
)
await db.execute_write("insert into foo (json_column) values (?)", [value])
response = await ds.client.get("/test_json_array/foo.json")
data = response.json()
assert data["suggested_facets"] == [
@ -536,15 +532,12 @@ async def test_json_array_with_blanks_and_nulls():
async def test_facet_size():
ds = Datasette([], memory=True, settings={"max_returned_rows": 50})
db = ds.add_database(Database(ds, memory_name="test_facet_size"))
await db.execute_write(
"create table neighbourhoods(city text, neighbourhood text)", block=True
)
await db.execute_write("create table neighbourhoods(city text, neighbourhood text)")
for i in range(1, 51):
for j in range(1, 4):
await db.execute_write(
"insert into neighbourhoods (city, neighbourhood) values (?, ?)",
["City {}".format(i), "Neighbourhood {}".format(j)],
block=True,
)
response = await ds.client.get("/test_facet_size/neighbourhoods.json")
data = response.json()

View file

@ -377,9 +377,7 @@ async def test_table_names(db):
@pytest.mark.asyncio
async def test_execute_write_block_true(db):
await db.execute_write(
"update roadside_attractions set name = ? where pk = ?",
["Mystery!", 1],
block=True,
"update roadside_attractions set name = ? where pk = ?", ["Mystery!", 1]
)
rows = await db.execute("select name from roadside_attractions where pk = 1")
assert "Mystery!" == rows.rows[0][0]
@ -399,8 +397,7 @@ async def test_execute_write_block_false(db):
@pytest.mark.asyncio
async def test_execute_write_script(db):
await db.execute_write_script(
"create table foo (id integer primary key); create table bar (id integer primary key); ",
block=True,
"create table foo (id integer primary key); create table bar (id integer primary key);"
)
table_names = await db.table_names()
assert {"foo", "bar"}.issubset(table_names)
@ -408,14 +405,9 @@ async def test_execute_write_script(db):
@pytest.mark.asyncio
async def test_execute_write_many(db):
await db.execute_write_script(
"create table foomany (id integer primary key)",
block=True,
)
await db.execute_write_script("create table foomany (id integer primary key)")
await db.execute_write_many(
"insert into foomany (id) values (?)",
[(1,), (10,), (100,)],
block=True,
"insert into foomany (id) values (?)", [(1,), (10,), (100,)]
)
result = await db.execute("select * from foomany")
assert [r[0] for r in result.rows] == [1, 10, 100]
@ -424,7 +416,7 @@ async def test_execute_write_many(db):
@pytest.mark.asyncio
async def test_execute_write_has_correctly_prepared_connection(db):
# The sleep() function is only available if ds._prepare_connection() was called
await db.execute_write("select sleep(0.01)", block=True)
await db.execute_write("select sleep(0.01)")
@pytest.mark.asyncio
@ -447,7 +439,7 @@ async def test_execute_write_fn_block_true(db):
row = conn.execute("select count(*) from roadside_attractions").fetchone()
return row[0]
new_count = await db.execute_write_fn(write_fn, block=True)
new_count = await db.execute_write_fn(write_fn)
assert 3 == new_count
@ -457,7 +449,7 @@ async def test_execute_write_fn_exception(db):
assert False
with pytest.raises(AssertionError):
await db.execute_write_fn(write_fn, block=True)
await db.execute_write_fn(write_fn)
@pytest.mark.asyncio
@ -472,7 +464,7 @@ async def test_execute_write_fn_connection_exception(tmpdir, app_client):
assert False
with pytest.raises(AssertionError):
await db.execute_write_fn(write_fn, block=True)
await db.execute_write_fn(write_fn)
app_client.ds.remove_database("immutable-db")
@ -513,7 +505,7 @@ async def test_database_memory_name(app_client):
table_names = await db.table_names()
assert table_names == []
# Now create a table in foo
await foo1.execute_write("create table foo (t text)", block=True)
await foo1.execute_write("create table foo (t text)")
assert await foo1.table_names() == ["foo"]
assert await foo2.table_names() == ["foo"]
assert await bar1.table_names() == []
@ -528,5 +520,5 @@ async def test_in_memory_databases_forbid_writes(app_client):
await db.execute("create table foo (t text)")
assert await db.table_names() == []
# Using db.execute_write() should work:
await db.execute_write("create table foo (t text)", block=True)
await db.execute_write("create table foo (t text)")
assert await db.table_names() == ["foo"]