not_null support for .create(transform=True), refs #467

This commit is contained in:
Simon Willison 2022-08-27 15:42:47 -07:00
commit 341f000097
2 changed files with 14 additions and 2 deletions

View file

@ -929,6 +929,11 @@ class Database:
desired_pk = list(pk)
if desired_pk and current_pks != desired_pk:
needs_transform = True
# Any not-null changes?
current_not_null = {c.name for c in table.columns if c.notnull}
desired_not_null = set(not_null) if not_null else set()
if current_not_null != desired_not_null:
needs_transform = True
# Only run .transform() if there is something to do
# TODO: what about not null and defaults?
if needs_transform:

View file

@ -1197,6 +1197,13 @@ def test_create_if_no_columns(fresh_db):
"CREATE TABLE [demo] (\n [id] INTEGER PRIMARY KEY,\n [name] TEXT\n)",
False,
),
# Change not null
(
{"id": int, "name": str},
{"pk": "id", "not_null": {"name"}},
'CREATE TABLE "demo" (\n [id] INTEGER PRIMARY KEY,\n [name] TEXT NOT NULL\n)',
True,
),
),
)
def test_create_transform(fresh_db, cols, kwargs, expected_schema, should_transform):
@ -1205,8 +1212,8 @@ def test_create_transform(fresh_db, cols, kwargs, expected_schema, should_transf
traces = []
with fresh_db.tracer(lambda sql, parameters: traces.append((sql, parameters))):
fresh_db["demo"].create(cols, **kwargs, transform=True)
new_schema = fresh_db["demo"].schema
assert new_schema == expected_schema, repr(new_schema)
at_least_one_create_table = any(sql.startswith("CREATE TABLE") for sql, _ in traces)
assert should_transform == at_least_one_create_table
new_schema = fresh_db["demo"].schema
assert new_schema == expected_schema, repr(new_schema)
assert fresh_db["demo"].count == 1