Skip to content

Commit bc37fb2

Browse files
author
xueyulinn
committed
enable more robust code_references_col_range_check constraint
1 parent 2712706 commit bc37fb2

1 file changed

Lines changed: 61 additions & 52 deletions

File tree

codehawk/postgresql/db_init.sql

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,57 @@
1-
CREATE TABLE IF NOT EXISTS code_chunks (
2-
id BIGSERIAL PRIMARY KEY,
3-
repo_id TEXT NOT NULL,
4-
commit_sha TEXT NOT NULL,
5-
file_path TEXT NOT NULL,
6-
language TEXT NOT NULL DEFAULT 'python',
7-
symbol TEXT NOT NULL,
8-
qualified_symbol TEXT NOT NULL,
9-
symbol_type TEXT NOT NULL,
10-
parent_symbol TEXT,
11-
start_line INTEGER NOT NULL,
12-
end_line INTEGER NOT NULL,
13-
start_col INTEGER,
14-
end_col INTEGER,
15-
signature TEXT,
16-
docstring TEXT,
17-
content TEXT NOT NULL,
18-
content_hash TEXT NOT NULL,
19-
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
20-
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
21-
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
22-
CONSTRAINT code_chunks_line_range_check CHECK (start_line <= end_line),
23-
CONSTRAINT code_chunks_col_range_check CHECK (
24-
start_col IS NULL
25-
OR end_col IS NULL
26-
OR start_col <= end_col
27-
),
28-
CONSTRAINT code_chunks_symbol_type_check CHECK (
29-
symbol_type IN ('module', 'class', 'function', 'method', 'async_function', 'async_method')
30-
),
31-
CONSTRAINT code_chunks_unique_content UNIQUE (
32-
repo_id,
33-
commit_sha,
34-
file_path,
35-
qualified_symbol,
36-
start_line,
37-
end_line,
38-
content_hash
39-
)
40-
);
41-
42-
CREATE INDEX IF NOT EXISTS code_chunks_repo_commit_idx
43-
ON code_chunks (repo_id, commit_sha);
44-
45-
CREATE INDEX IF NOT EXISTS code_chunks_file_range_idx
46-
ON code_chunks (repo_id, commit_sha, file_path, start_line, end_line);
47-
48-
CREATE INDEX IF NOT EXISTS code_chunks_symbol_idx
49-
ON code_chunks (repo_id, commit_sha, qualified_symbol);
50-
1+
CREATE TABLE IF NOT EXISTS code_chunks (
2+
id BIGSERIAL PRIMARY KEY,
3+
repo_id TEXT NOT NULL,
4+
commit_sha TEXT NOT NULL,
5+
file_path TEXT NOT NULL,
6+
language TEXT NOT NULL DEFAULT 'python',
7+
symbol TEXT NOT NULL,
8+
qualified_symbol TEXT NOT NULL,
9+
symbol_type TEXT NOT NULL,
10+
parent_symbol TEXT,
11+
start_line INTEGER NOT NULL,
12+
end_line INTEGER NOT NULL,
13+
start_col INTEGER,
14+
end_col INTEGER,
15+
signature TEXT,
16+
docstring TEXT,
17+
content TEXT NOT NULL,
18+
content_hash TEXT NOT NULL,
19+
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
20+
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
21+
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
22+
CONSTRAINT code_chunks_line_range_check CHECK (start_line <= end_line),
23+
CONSTRAINT code_chunks_col_range_check CHECK (
24+
start_col IS NULL
25+
OR end_col IS NULL
26+
OR start_line < end_line
27+
OR (
28+
start_line == end_line AND
29+
start_col <= end_col
30+
)
31+
),
32+
CONSTRAINT code_chunks_symbol_type_check CHECK (
33+
symbol_type IN ('module', 'class', 'function', 'method', 'async_function', 'async_method')
34+
),
35+
CONSTRAINT code_chunks_unique_content UNIQUE (
36+
repo_id,
37+
commit_sha,
38+
file_path,
39+
qualified_symbol,
40+
start_line,
41+
end_line,
42+
content_hash
43+
)
44+
);
45+
46+
CREATE INDEX IF NOT EXISTS code_chunks_repo_commit_idx
47+
ON code_chunks (repo_id, commit_sha);
48+
49+
CREATE INDEX IF NOT EXISTS code_chunks_file_range_idx
50+
ON code_chunks (repo_id, commit_sha, file_path, start_line, end_line);
51+
52+
CREATE INDEX IF NOT EXISTS code_chunks_symbol_idx
53+
ON code_chunks (repo_id, commit_sha, qualified_symbol);
54+
5155
CREATE INDEX IF NOT EXISTS code_chunks_content_hash_idx
5256
ON code_chunks (content_hash);
5357

@@ -78,7 +82,12 @@ CREATE TABLE IF NOT EXISTS code_references (
7882
CONSTRAINT code_references_col_range_check CHECK (
7983
start_col IS NULL
8084
OR end_col IS NULL
81-
OR start_col <= end_col
85+
OR end_line IS NULL
86+
OR start_line < end_line
87+
OR (
88+
start_line = end_line AND
89+
start_col <= end_col
90+
)
8291
),
8392
CONSTRAINT code_references_type_check CHECK (
8493
reference_type IN (
@@ -118,4 +127,4 @@ CREATE INDEX IF NOT EXISTS code_references_resolved_symbol_idx
118127
ON code_references (repo_id, commit_sha, resolved_symbol);
119128

120129
CREATE INDEX IF NOT EXISTS code_references_target_chunk_idx
121-
ON code_references (target_chunk_id);
130+
ON code_references (target_chunk_id);

0 commit comments

Comments
 (0)