Skip to content

Commit 6a45683

Browse files
authored
Fix _decode_default_value to unescape doubled single quotes in string defaults (#811)
* Fix _decode_default_value to unescape doubled single quotes in string defaults SQLite stores string defaults with single quotes doubled (e.g. DEFAULT 'O''Brien' is stored as the literal "'O''Brien'" in sqlite_master). The previous code stripped the outer quotes with value[1:-1] but never converted '' back to ', so default_values returned the raw escaped form instead of the true string value. * Test for doubled single quotes in string defaults
1 parent a7b7349 commit 6a45683

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎sqlite_utils/db.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5108,8 +5108,8 @@ def resolve_extracts(
51085108

51095109
def _decode_default_value(value: str) -> object:
51105110
if value.startswith("'") and value.endswith("'"):
5111-
# It's a string
5112-
return value[1:-1]
5111+
# It's a string; unescape doubled single quotes
5112+
return value[1:-1].replace("''", "'")
51135113
if value.isdigit():
51145114
# It's an integer
51155115
return int(value)

‎tests/test_introspect.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ def test_table_strict(fresh_db, create_table, expected_strict):
312312
1,
313313
1.3,
314314
"foo",
315+
"O'Brien",
315316
True,
316317
b"binary",
317318
),
@@ -324,6 +325,16 @@ def test_table_default_values(fresh_db, value):
324325
assert default_values == {"value": value}
325326

326327

328+
def test_table_default_values_escaped_quotes(fresh_db):
329+
# SQLite stores string defaults with single quotes doubled, so
330+
# introspection needs to unescape them again
331+
fresh_db.execute(
332+
"create table t (id integer primary key, name text default 'O''Brien')"
333+
)
334+
assert "default 'O''Brien'" in fresh_db["t"].schema
335+
assert fresh_db["t"].default_values == {"name": "O'Brien"}
336+
337+
327338
def test_pks_use_primary_key_declaration_order(fresh_db):
328339
# PRIMARY KEY (a, b) declared against columns stored in order (b, a) -
329340
# pks must follow the declaration order, which is what SQLite uses to

0 commit comments

Comments
 (0)