refactor(schema): consolidate schema definition to single source of truth
This commit is contained in:
13
Makefile
Normal file
13
Makefile
Normal file
@@ -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
|
||||||
@@ -15,27 +15,10 @@ import { existsSync } from "node:fs";
|
|||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import Database from "better-sqlite3";
|
import Database from "better-sqlite3";
|
||||||
|
|
||||||
const SCHEMA = `-- .codexis/index.db schema:
|
// @ts-expect-error - generated file
|
||||||
--
|
import schema from "./schema.sql?raw";
|
||||||
-- files: indexed source files
|
|
||||||
-- id INTEGER PRIMARY KEY
|
const SCHEMA = schema;
|
||||||
-- 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`;
|
|
||||||
|
|
||||||
const DESCRIPTION = `Query the code index database (.codexis/index.db). Run read-only SQL to find symbols, files, and line numbers across the codebase.
|
const DESCRIPTION = `Query the code index database (.codexis/index.db). Run read-only SQL to find symbols, files, and line numbers across the codebase.
|
||||||
|
|
||||||
|
|||||||
34
extension/schema.sql
Normal file
34
extension/schema.sql
Normal file
@@ -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);
|
||||||
@@ -21,6 +21,7 @@
|
|||||||
devShells.default = pkgs.mkShell {
|
devShells.default = pkgs.mkShell {
|
||||||
packages = with pkgs; [
|
packages = with pkgs; [
|
||||||
go
|
go
|
||||||
|
gnumake
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
42
main.go
42
main.go
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"embed"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@@ -85,43 +86,10 @@ func run(root, dbPath string, force bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//go:embed db/schema.sql
|
||||||
|
var schemaSQL string
|
||||||
|
|
||||||
func createSchema(ctx context.Context, sqlDB *sql.DB) error {
|
func createSchema(ctx context.Context, sqlDB *sql.DB) error {
|
||||||
schema := `
|
_, err := sqlDB.ExecContext(ctx, schemaSQL)
|
||||||
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)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user