Skip to content

Commit bfe3f31

Browse files
committed
Match SQLite: strip outer parens from PRAGMA table_info defaults
Turso serializes the default-expression AST directly via expr.to_string(), which preserves user-written outer parens. Real SQLite strips one level of balanced outer parens so DEFAULT (CURRENT_TIMESTAMP) round-trips as CURRENT_TIMESTAMP. Match that behavior in PRAGMA emit. Fixes testCreateTableWithDefaultNowFunction and testCreateTableWithDefaultExpressions (both assert PRAGMA output).
1 parent 173d9d5 commit bfe3f31

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

.github/workflows/phpunit-tests-turso.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,47 @@ jobs:
433433
print('patched delete.rs to allow DELETE FROM sqlite_sequence')
434434
PY_DELETE_SEQ
435435
436+
# PRAGMA table_info/table_xinfo reports the default expression by
437+
# serializing the stored AST back to a string, which keeps any outer
438+
# parens the user wrote. Real SQLite strips one level of outer
439+
# parens for balanced `(expr)` forms, so `DEFAULT (CURRENT_TIMESTAMP)`
440+
# round-trips through PRAGMA as `CURRENT_TIMESTAMP`. Match that.
441+
python3 - <<'PY_PRAGMA_DEFAULT'
442+
p = 'core/translate/pragma.rs'
443+
s = open(p).read()
444+
old = (
445+
" Some(expr) => {\n"
446+
" program.emit_string8(expr.to_string(), base_reg + 4);\n"
447+
" }\n"
448+
)
449+
new = (
450+
" Some(expr) => {\n"
451+
" // Match SQLite: strip a single pair of balanced\n"
452+
" // outer parens from the default expression.\n"
453+
" let raw = expr.to_string();\n"
454+
" let trimmed = raw.trim();\n"
455+
" let stripped: String = if trimmed.starts_with('(') && trimmed.ends_with(')') {\n"
456+
" let inner = &trimmed[1..trimmed.len() - 1];\n"
457+
" let mut depth: i32 = 0;\n"
458+
" let mut outer = true;\n"
459+
" for c in inner.chars() {\n"
460+
" if c == '(' { depth += 1; }\n"
461+
" else if c == ')' {\n"
462+
" if depth == 0 { outer = false; break; }\n"
463+
" depth -= 1;\n"
464+
" }\n"
465+
" }\n"
466+
" if outer && depth == 0 { inner.trim().to_string() }\n"
467+
" else { raw.clone() }\n"
468+
" } else { raw.clone() };\n"
469+
" program.emit_string8(stripped, base_reg + 4);\n"
470+
" }\n"
471+
)
472+
assert old in s, 'PRAGMA table_info default-emit block not found'
473+
open(p, 'w').write(s.replace(old, new, 1))
474+
print('patched PRAGMA table_info to strip outer parens from default')
475+
PY_PRAGMA_DEFAULT
476+
436477
echo '--- Patched stub! macro ---'
437478
sed -n '/macro_rules! stub/,/^}$/p' sqlite3/src/lib.rs
438479

0 commit comments

Comments
 (0)