Add nullable columns without a default

Deploying knowledge bases showed the migration runner doing the wrong thing:

    ALTER TABLE "documents" ADD COLUMN "base_id" VARCHAR(32) DEFAULT ''

`base_id` is nullable and its absent value is NULL, but the runner derived a
default from the column type and backfilled every existing row with the empty
string. Nothing then matched `base_id IS NULL`, so the startup sweep that files
pre-bases documents into a default base would have skipped all of them and the
documents would have stayed invisible.

Nobody lost anything -- the live instance had no documents yet -- but the fault
is general: any nullable column added from here would arrive as "" rather than
NULL, and every "is this set?" check would be wrong about the rows that predate
it. So a default is now emitted only for NOT NULL columns, where SQLite requires
one.

The sweep also accepts "" as meaning unfiled, since a deployment that upgraded
through the previous release has rows holding it.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jaroslav Beneš
2026-07-21 20:03:01 +02:00
parent a0f733063a
commit 2f978d84d1
5 changed files with 71 additions and 5 deletions
+31
View File
@@ -200,3 +200,34 @@ def test_other_sessions_are_revoked_but_this_one_survives(client: TestClient, db
assert other.get("/chat", follow_redirects=False).status_code == 303
# ...and the tab that made the change is still in.
assert client.get("/chat", follow_redirects=False).status_code == 200
# --- Adding a column to a table that already has rows -------------------------
def test_a_nullable_column_is_added_without_a_default(db):
"""Backfilling one would give existing rows a value the model does not treat
as absent: an added foreign key arrives as "" rather than NULL, and every
"is this set?" check downstream is then wrong about the old rows."""
from sqlalchemy import Column, String
from lembas.db.migrations import _add_column_sql
from lembas.db.models import Document
sql = _add_column_sql(
Document.__table__, Column("later", String(32), nullable=True), db.bind.dialect
)
assert "DEFAULT" not in sql
def test_a_not_null_column_still_gets_one(db):
"""SQLite refuses NOT NULL with no default, so there it is unavoidable."""
from sqlalchemy import Column, String
from lembas.db.migrations import _add_column_sql
from lembas.db.models import Document
sql = _add_column_sql(
Document.__table__,
Column("later", String(32), nullable=False, default=""),
db.bind.dialect,
)
assert "NOT NULL" in sql and "DEFAULT" in sql