diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5ab6520 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +.PHONY: all build generate-schema + +all: build + +build: + go build -o codexis . + +generate-schema: + @sed 's/^/-- /' db/schema.sql > extension/schema.sql + @echo "Generated extension/schema.sql" + +clean: + rm -f codexis extension/schema.sql diff --git a/extension/codexis.ts b/extension/codexis.ts index 169a508..b94d9b4 100644 --- a/extension/codexis.ts +++ b/extension/codexis.ts @@ -15,27 +15,10 @@ import { existsSync } from "node:fs"; import { join } from "node:path"; import Database from "better-sqlite3"; -const SCHEMA = `-- .codexis/index.db schema: --- --- files: indexed source files --- id INTEGER PRIMARY KEY --- path TEXT NOT NULL UNIQUE -- relative to repo root --- language TEXT NOT NULL -- e.g. 'go', 'typescript', 'python', 'tsx', 'proto' --- package TEXT -- package/module name (from AST or directory) --- hash TEXT NOT NULL -- sha256, for incremental indexing --- indexed_at DATETIME --- --- symbols: definitions extracted via tree-sitter --- id INTEGER PRIMARY KEY --- file_id INTEGER NOT NULL REFERENCES files(id) --- name TEXT NOT NULL --- kind TEXT NOT NULL -- one of: 'function','method','class','type','interface','constant','variable','constructor' --- line INTEGER NOT NULL -- 1-indexed --- line_end INTEGER -- end of definition body --- col INTEGER --- col_end INTEGER --- exported BOOLEAN -- language-specific visibility --- parent_id INTEGER REFERENCES symbols(id) -- e.g. method→class, field→struct`; +// @ts-expect-error - generated file +import schema from "./schema.sql?raw"; + +const SCHEMA = schema; const DESCRIPTION = `Query the code index database (.codexis/index.db). Run read-only SQL to find symbols, files, and line numbers across the codebase. diff --git a/extension/schema.sql b/extension/schema.sql new file mode 100644 index 0000000..80ddeb0 --- /dev/null +++ b/extension/schema.sql @@ -0,0 +1,34 @@ +-- CREATE TABLE files ( +-- id INTEGER PRIMARY KEY, +-- path TEXT NOT NULL UNIQUE, +-- language TEXT NOT NULL, +-- package TEXT, +-- hash TEXT NOT NULL, +-- indexed_at DATETIME DEFAULT CURRENT_TIMESTAMP +-- ); +-- +-- CREATE TABLE symbols ( +-- id INTEGER PRIMARY KEY, +-- file_id INTEGER NOT NULL REFERENCES files(id) ON DELETE CASCADE, +-- name TEXT NOT NULL, +-- kind TEXT NOT NULL CHECK(kind IN ( +-- 'function', 'method', 'class', 'type', +-- 'interface', 'constant', 'variable', 'constructor' +-- )), +-- line INTEGER NOT NULL, +-- line_end INTEGER, +-- col INTEGER, +-- col_end INTEGER, +-- exported BOOLEAN, +-- parent_id INTEGER REFERENCES symbols(id), +-- UNIQUE(file_id, name, kind, line) +-- ); +-- +-- CREATE INDEX idx_symbols_name ON symbols(name); +-- CREATE INDEX idx_symbols_kind ON symbols(kind); +-- CREATE INDEX idx_symbols_file_line ON symbols(file_id, line); +-- CREATE INDEX idx_symbols_parent ON symbols(parent_id); +-- CREATE INDEX idx_symbols_exported ON symbols(exported, kind); +-- CREATE INDEX idx_files_path ON files(path); +-- CREATE INDEX idx_files_language ON files(language); +-- CREATE INDEX idx_files_package ON files(package); diff --git a/flake.nix b/flake.nix index 2c68b59..85e8537 100644 --- a/flake.nix +++ b/flake.nix @@ -21,6 +21,7 @@ devShells.default = pkgs.mkShell { packages = with pkgs; [ go + gnumake ]; }; } diff --git a/main.go b/main.go index e4c5887..7b0ee38 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "context" "database/sql" + "embed" "flag" "fmt" "os" @@ -85,43 +86,10 @@ func run(root, dbPath string, force bool) error { return nil } +//go:embed db/schema.sql +var schemaSQL string + func createSchema(ctx context.Context, sqlDB *sql.DB) error { - schema := ` -CREATE TABLE IF NOT EXISTS files ( - id INTEGER PRIMARY KEY, - path TEXT NOT NULL UNIQUE, - language TEXT NOT NULL, - package TEXT, - hash TEXT NOT NULL, - indexed_at DATETIME DEFAULT CURRENT_TIMESTAMP -); - -CREATE TABLE IF NOT EXISTS symbols ( - id INTEGER PRIMARY KEY, - file_id INTEGER NOT NULL REFERENCES files(id) ON DELETE CASCADE, - name TEXT NOT NULL, - kind TEXT NOT NULL CHECK(kind IN ( - 'function', 'method', 'class', 'type', - 'interface', 'constant', 'variable', 'constructor' - )), - line INTEGER NOT NULL, - line_end INTEGER, - col INTEGER, - col_end INTEGER, - exported BOOLEAN, - parent_id INTEGER REFERENCES symbols(id), - UNIQUE(file_id, name, kind, line) -); - -CREATE INDEX IF NOT EXISTS idx_symbols_name ON symbols(name); -CREATE INDEX IF NOT EXISTS idx_symbols_kind ON symbols(kind); -CREATE INDEX IF NOT EXISTS idx_symbols_file_line ON symbols(file_id, line); -CREATE INDEX IF NOT EXISTS idx_symbols_parent ON symbols(parent_id); -CREATE INDEX IF NOT EXISTS idx_symbols_exported ON symbols(exported, kind); -CREATE INDEX IF NOT EXISTS idx_files_path ON files(path); -CREATE INDEX IF NOT EXISTS idx_files_language ON files(language); -CREATE INDEX IF NOT EXISTS idx_files_package ON files(package); -` - _, err := sqlDB.ExecContext(ctx, schema) + _, err := sqlDB.ExecContext(ctx, schemaSQL) return err }