mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-28 12:54:15 +02:00
Handle changed defaults, refs #467
This commit is contained in:
parent
341f000097
commit
1ac5d4e39d
2 changed files with 16 additions and 7 deletions
|
|
@ -898,7 +898,7 @@ class Database:
|
||||||
# Transform table to match the new definition if table already exists:
|
# Transform table to match the new definition if table already exists:
|
||||||
if transform and self[name].exists():
|
if transform and self[name].exists():
|
||||||
table = cast(Table, self[name])
|
table = cast(Table, self[name])
|
||||||
needs_transform = False
|
should_transform = False
|
||||||
# First add missing columns and figure out columns to drop
|
# First add missing columns and figure out columns to drop
|
||||||
existing_columns = table.columns_dict
|
existing_columns = table.columns_dict
|
||||||
missing_columns = dict(
|
missing_columns = dict(
|
||||||
|
|
@ -913,13 +913,13 @@ class Database:
|
||||||
for col_name, col_type in missing_columns.items():
|
for col_name, col_type in missing_columns.items():
|
||||||
table.add_column(col_name, col_type)
|
table.add_column(col_name, col_type)
|
||||||
if missing_columns or columns_to_drop:
|
if missing_columns or columns_to_drop:
|
||||||
needs_transform = True
|
should_transform = True
|
||||||
# Do we need to change the column order?
|
# Do we need to change the column order?
|
||||||
if (
|
if (
|
||||||
column_order
|
column_order
|
||||||
and list(existing_columns)[: len(column_order)] != column_order
|
and list(existing_columns)[: len(column_order)] != column_order
|
||||||
):
|
):
|
||||||
needs_transform = True
|
should_transform = True
|
||||||
# Has the primary key changed?
|
# Has the primary key changed?
|
||||||
current_pks = table.pks
|
current_pks = table.pks
|
||||||
desired_pk = None
|
desired_pk = None
|
||||||
|
|
@ -928,15 +928,17 @@ class Database:
|
||||||
elif pk:
|
elif pk:
|
||||||
desired_pk = list(pk)
|
desired_pk = list(pk)
|
||||||
if desired_pk and current_pks != desired_pk:
|
if desired_pk and current_pks != desired_pk:
|
||||||
needs_transform = True
|
should_transform = True
|
||||||
# Any not-null changes?
|
# Any not-null changes?
|
||||||
current_not_null = {c.name for c in table.columns if c.notnull}
|
current_not_null = {c.name for c in table.columns if c.notnull}
|
||||||
desired_not_null = set(not_null) if not_null else set()
|
desired_not_null = set(not_null) if not_null else set()
|
||||||
if current_not_null != desired_not_null:
|
if current_not_null != desired_not_null:
|
||||||
needs_transform = True
|
should_transform = True
|
||||||
|
# How about defaults?
|
||||||
|
if defaults and defaults != table.default_values:
|
||||||
|
should_transform = True
|
||||||
# Only run .transform() if there is something to do
|
# Only run .transform() if there is something to do
|
||||||
# TODO: what about not null and defaults?
|
if should_transform:
|
||||||
if needs_transform:
|
|
||||||
table.transform(
|
table.transform(
|
||||||
types=columns,
|
types=columns,
|
||||||
drop=columns_to_drop,
|
drop=columns_to_drop,
|
||||||
|
|
|
||||||
|
|
@ -1204,6 +1204,13 @@ def test_create_if_no_columns(fresh_db):
|
||||||
'CREATE TABLE "demo" (\n [id] INTEGER PRIMARY KEY,\n [name] TEXT NOT NULL\n)',
|
'CREATE TABLE "demo" (\n [id] INTEGER PRIMARY KEY,\n [name] TEXT NOT NULL\n)',
|
||||||
True,
|
True,
|
||||||
),
|
),
|
||||||
|
# Change default values
|
||||||
|
(
|
||||||
|
{"id": int, "name": str},
|
||||||
|
{"pk": "id", "defaults": {"id": 0, "name": "Bob"}},
|
||||||
|
"CREATE TABLE \"demo\" (\n [id] INTEGER PRIMARY KEY DEFAULT 0,\n [name] TEXT DEFAULT 'Bob'\n)",
|
||||||
|
True,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
def test_create_transform(fresh_db, cols, kwargs, expected_schema, should_transform):
|
def test_create_transform(fresh_db, cols, kwargs, expected_schema, should_transform):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue