-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.sql
More file actions
57 lines (51 loc) · 2.02 KB
/
init.sql
File metadata and controls
57 lines (51 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
-- Enable pgvector extension
CREATE EXTENSION IF NOT EXISTS vector;
-- Create tables for storing codebase information
CREATE TABLE IF NOT EXISTS repositories (
id SERIAL PRIMARY KEY,
repo_name VARCHAR(255) NOT NULL,
repo_url VARCHAR(500) NOT NULL,
clone_path VARCHAR(500),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
status VARCHAR(50) DEFAULT 'pending'
);
CREATE TABLE IF NOT EXISTS code_files (
id SERIAL PRIMARY KEY,
repository_id INTEGER REFERENCES repositories(id) ON DELETE CASCADE,
file_path VARCHAR(500) NOT NULL,
file_name VARCHAR(255) NOT NULL,
file_extension VARCHAR(50),
file_size INTEGER,
content_hash VARCHAR(64),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS code_chunks (
id SERIAL PRIMARY KEY,
file_id INTEGER REFERENCES code_files(id) ON DELETE CASCADE,
chunk_index INTEGER NOT NULL,
content TEXT NOT NULL,
start_line INTEGER NOT NULL,
end_line INTEGER NOT NULL,
token_count INTEGER,
embedding vector(1536), -- OpenAI embedding dimension
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_repositories_repo_name ON repositories(repo_name);
CREATE INDEX IF NOT EXISTS idx_code_files_repository_id ON code_files(repository_id);
CREATE INDEX IF NOT EXISTS idx_code_files_file_path ON code_files(file_path);
CREATE INDEX IF NOT EXISTS idx_code_chunks_file_id ON code_chunks(file_id);
CREATE INDEX IF NOT EXISTS idx_code_chunks_embedding ON code_chunks USING ivfflat (embedding vector_cosine_ops);
-- Create function to update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ language 'plpgsql';
-- Create trigger for repositories table
CREATE TRIGGER update_repositories_updated_at
BEFORE UPDATE ON repositories
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();