mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-26 02:44:33 +02:00
not_null support for .create(transform=True), refs #467
This commit is contained in:
parent
9e3d8abc58
commit
341f000097
2 changed files with 14 additions and 2 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue