Preserve column before/after comments through .transform()

Refs #762

The before comment comes before the column definition - the after
comment is anything after it but before its trailing comma.
This commit is contained in:
Simon Willison 2026-08-11 22:37:13 -07:00
commit b37b8cf8c8
6 changed files with 171 additions and 18 deletions

View file

@ -4,7 +4,13 @@ import hypothesis.strategies as st
import pytest
from hypothesis import given
from sqlite_utils.create_table_parser import Check, ParseError, parse_checks
from sqlite_utils.create_table_parser import (
Check,
ColumnComments,
ParseError,
parse_checks,
parse_column_comments,
)
def test_parse_column_and_table_checks():
@ -46,6 +52,26 @@ def test_comments_are_trivia_not_constraints():
]
def test_parse_comments_owned_by_columns():
sql = """
CREATE TABLE t (
-- Before id
id /* Between name and type */ INTEGER /* After id */,
/* Between column definitions */
value TEXT CHECK(value != '') /* After value */,
/* Before a table constraint, not a column */
CHECK(value != 'forbidden')
)
"""
assert parse_column_comments(sql) == {
"id": ColumnComments(before="-- Before id", after="/* After id */"),
"value": ColumnComments(
before="/* Between column definitions */",
after="/* After value */",
),
}
@pytest.mark.parametrize(
"expression,expected",
[