mirror of
https://github.com/DeusData/codebase-memory-mcp.git
synced 2026-12-04 04:37:20 +08:00
Valid SQLite migration files were reported parse_partial because the
vendored SQL grammar (DerekStride/tree-sitter-sql @ 851e9cb257ba) rejects
SQLite syntax they use: PRAGMA, AUTOINCREMENT, a trigger's BEGIN ... END
body, a column named `key` after the first column, WITHOUT ROWID, an
unparenthesized trigger WHEN, INSERT OR IGNORE, and CREATE VIRTUAL TABLE
... USING fts5(...). Each became an ERROR node; the trigger's error region
in atuin's ai_session schema also swallowed the CREATE TABLE after it.
The `key` case is lexical: after a comma inside a column list the grammar
also accepts MySQL's `KEY name (cols)` index, so `key` lexes as that
keyword there and `key TEXT NOT NULL` became a malformed index.
Fix: sql becomes a self-maintained fork of the pin we already vendored,
following the pascal model. tools/tree-sitter-sql/ holds upstream's
grammar.js + grammar/ (blob-identical to the pin and to upstream main @
3c99273f9b32, where its corpus lives) and one patch,
patches/sqlite-dialect.patch, touching grammar/ only:
- PRAGMA [schema.]name [= value | (value)] (new pragma_statement)
- AUTOINCREMENT as keyword_auto_increment, upstream's own change
(c042cde1e2a6, 2026-11-08, after our pin)
- `key` accepted as a column name; the type keywords that take no
arguments are accepted as a MySQL index name so `KEY name (name)` stays
an index
- WITHOUT ROWID
- a trigger body as the existing block node, after an optional
unparenthesized WHEN (precedence keeps Postgres `WHEN (...) EXECUTE
FUNCTION` trees unchanged)
- INSERT OR {ABORT | FAIL | IGNORE | REPLACE | ROLLBACK}
- CREATE VIRTUAL TABLE [IF NOT EXISTS] name USING module(args), aliased
to create_table (new module_argument node), so the table is extracted
as a Table like any other
Upstream node types keep their shape: upstream's 511 corpus cases pass
unchanged; test/corpus/sqlite.txt adds 11 (9 fail on the unpatched
grammar, 2 controls pass on both). The vendored scanner.c is kept as is
(it carries our start_tag leak fix 0e61867d9, now listed under the
MANIFEST's "Local source patches"; upstream fixed the same leak after our
pin in ee2f84f9b857).
Provenance: without the patch, tree-sitter-cli 0.25.10 regenerates
upstream's shipped parser.c byte-for-byte except line 1 (upstream's
generator stamp has no version), and grammar.json, node-types.json and
tree_sitter/*.h byte-identical. Patched: three generations are
byte-identical (parser.c SHA-256 3c64e5c4...7887); ABI 15; states 30,622
-> 31,081, symbols 770 -> 782; 0 non-ASCII bytes, no string literal with
a space, one comment, the same call-position identifiers as before. Node
types only gain entries (pragma_statement, module_argument, four
keywords; create_table gains a `module` field).
Proof, indexed from scratch with isolated caches (main 6d34ca15 vs this
change): dani-garcia/vaultwarden @ 58a980f8 migrations/sqlite (116
files) parse_partial 3 -> 0, graph identical (303 nodes, 446 edges);
atuinsh/atuin @ 2535161e *.sql (69 files) 19 -> 7: all 12 valid SQLite
files clean; the 7 left are the invalid `ALTER TABLE ... DROP INDEX`
down migration and the 2 MySQL + 4 Postgres files, same ranges as
before; graph +2 Table nodes (the fts5 table and the table the trigger
error swallowed), +2 DEFINES, +3 USAGE lineage edges. No newly flagged
file. Control: helm @ 9b88efd6 indexes to an identical graph (12,251
nodes, 39,561 edges).
Registered like pascal: MANIFEST.md fork rows + notes (ABI tally
recounted, unchanged), the audit FORKS map, THIRD_PARTY.md, vendored
checksums. LICENSE stays upstream's MIT byte-identical.
Tests: parse_coverage carries the two SQLite tests from #2246 as written
(sqlite_valid_migration_not_flagged, red before this change, and the
sqlite_malformed_still_flagged control) and adds `key` columns, WITHOUT
ROWID, a WHEN trigger with INSERT OR IGNORE plus an fts5 table (asserted
as a Table), PRAGMA forms, and two controls: MySQL's KEY index still
clean, and ALTER TABLE ... DROP INDEX still flagged.
Distilled from #2246.
Co-authored-by: Kerf <kerfern@hotmail.com>
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
207 lines
5.1 KiB
JavaScript
207 lines
5.1 KiB
JavaScript
import { comma_list, optional_parenthesis, paren_list, wrapped_in_parenthesis } from "./helpers.js";
|
|
|
|
export default {
|
|
|
|
_column_list: $ => paren_list(alias($._column, $.column), true),
|
|
|
|
_column: $ => choice(
|
|
$.identifier,
|
|
alias($._literal_string, $.literal),
|
|
),
|
|
|
|
_key_value_pair: $ => seq(
|
|
field('key',$.identifier),
|
|
'=',
|
|
field('value', alias($._literal_string, $.literal)),
|
|
),
|
|
|
|
column_definitions: $ => seq(
|
|
'(',
|
|
comma_list($.column_definition, true),
|
|
optional($.constraints),
|
|
')',
|
|
),
|
|
|
|
column_definition: $ => prec.left(seq(
|
|
// cbm fork (#2246): SQLite does not reserve `key`, so it is a common column
|
|
// name, but after the first column it lexes as the keyword of MySQL's
|
|
// `KEY name (cols)` index. Accept that keyword as a column name too.
|
|
field('name', choice($._column, alias($.keyword_key, $.identifier))),
|
|
field('type', $._type),
|
|
repeat($._column_constraint),
|
|
)),
|
|
|
|
_column_comment: $ => seq(
|
|
$.keyword_comment,
|
|
alias($._literal_string, $.literal)
|
|
),
|
|
|
|
_column_constraint: $ => prec.left(choice(
|
|
choice(
|
|
$.keyword_null,
|
|
$._not_null,
|
|
),
|
|
seq(
|
|
$.keyword_references,
|
|
$.object_reference,
|
|
paren_list($.identifier, true),
|
|
repeat(
|
|
seq(
|
|
$.keyword_on,
|
|
choice($.keyword_delete, $.keyword_update),
|
|
choice(
|
|
seq($.keyword_no, $.keyword_action),
|
|
$.keyword_restrict,
|
|
$.keyword_cascade,
|
|
seq(
|
|
$.keyword_set,
|
|
choice($.keyword_null, $.keyword_default),
|
|
optional(paren_list($.identifier, true))
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
$._default_expression,
|
|
$._primary_key,
|
|
$.keyword_auto_increment,
|
|
$.direction,
|
|
$._column_comment,
|
|
$._check_constraint,
|
|
seq(
|
|
optional(seq($.keyword_generated, $.keyword_always)),
|
|
$.keyword_as,
|
|
$._expression,
|
|
),
|
|
choice(
|
|
$.keyword_stored,
|
|
$.keyword_virtual,
|
|
),
|
|
$.keyword_unique
|
|
)),
|
|
|
|
_check_constraint: $ => seq(
|
|
optional(
|
|
seq(
|
|
$.keyword_constraint,
|
|
$.literal
|
|
)
|
|
),
|
|
$.keyword_check,
|
|
wrapped_in_parenthesis($.binary_expression)
|
|
),
|
|
|
|
_default_expression: $ => seq(
|
|
$.keyword_default,
|
|
optional_parenthesis($._inner_default_expression),
|
|
),
|
|
_inner_default_expression: $ => choice(
|
|
$.literal,
|
|
$.list,
|
|
$.cast,
|
|
$.binary_expression,
|
|
$.unary_expression,
|
|
$.array,
|
|
$.invocation,
|
|
$.keyword_current_timestamp,
|
|
alias($.implicit_cast, $.cast),
|
|
),
|
|
|
|
constraints: $ => seq(
|
|
',',
|
|
$.constraint,
|
|
repeat(
|
|
seq(',', $.constraint),
|
|
),
|
|
),
|
|
|
|
constraint: $ => choice(
|
|
$._constraint_literal,
|
|
$._key_constraint,
|
|
$._primary_key_constraint,
|
|
$._check_constraint
|
|
),
|
|
|
|
_constraint_literal: $ => seq(
|
|
$.keyword_constraint,
|
|
field('name', $.identifier),
|
|
choice(
|
|
seq(
|
|
$._primary_key,
|
|
$.ordered_columns,
|
|
),
|
|
seq(
|
|
$._check_constraint
|
|
)
|
|
)
|
|
),
|
|
|
|
_primary_key_constraint: $ => seq(
|
|
$._primary_key,
|
|
$.ordered_columns,
|
|
),
|
|
|
|
_key_constraint: $ => seq(
|
|
choice(
|
|
seq(
|
|
$.keyword_unique,
|
|
optional(
|
|
choice(
|
|
$.keyword_index,
|
|
$.keyword_key,
|
|
seq($.keyword_nulls, optional($.keyword_not), $.keyword_distinct),
|
|
),
|
|
),
|
|
),
|
|
seq(optional($.keyword_foreign), $.keyword_key, optional($._if_not_exists)),
|
|
$.keyword_index,
|
|
),
|
|
// cbm fork (#2246): with `key` also a column name (column_definition), a
|
|
// type keyword after KEY now lexes as that type, so accept the types that
|
|
// take no arguments as index names too (MySQL `KEY name (name)`). A type
|
|
// that takes arguments (int, varchar, time, ...) is not in the list.
|
|
optional(field('name', choice(
|
|
$.identifier,
|
|
...[
|
|
'boolean', 'image', 'smallserial', 'serial', 'bigserial', 'money',
|
|
'smallmoney', 'string', 'text', 'uuid', 'json', 'jsonb', 'xml', 'bytea',
|
|
'inet', 'date', 'datetime', 'datetime2', 'smalldatetime', 'timestamptz',
|
|
'interval', 'geometry', 'geography', 'box2d', 'box3d', 'oid', 'name',
|
|
'regclass', 'regnamespace', 'regproc', 'regtype',
|
|
].map((type) => alias($['keyword_' + type], $.identifier)),
|
|
))),
|
|
$.ordered_columns,
|
|
optional(
|
|
seq(
|
|
$.keyword_references,
|
|
$.object_reference,
|
|
paren_list($.identifier, true),
|
|
repeat(
|
|
seq(
|
|
$.keyword_on,
|
|
choice($.keyword_delete, $.keyword_update),
|
|
choice(
|
|
seq($.keyword_no, $.keyword_action),
|
|
$.keyword_restrict,
|
|
$.keyword_cascade,
|
|
seq(
|
|
$.keyword_set,
|
|
choice($.keyword_null, $.keyword_default),
|
|
optional(paren_list($.identifier, true))
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
|
|
ordered_columns: $ => paren_list(alias($.ordered_column, $.column), true),
|
|
|
|
ordered_column: $ => seq(
|
|
field('name', $._column),
|
|
optional($.direction),
|
|
),
|
|
|
|
};
|