This repository has been archived on 2026-03-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
labmcp/internal/database/schema.go
Torjus Håkestad d9aab773c6 feat(database): add file size metadata and range parameters
- Add byte_size and line_count columns to files table
- Increment SchemaVersion to 2 (requires re-indexing)
- Add DeclarationWithMetadata, FileRange, FileResult types
- Add GetDeclarationsWithMetadata method for file metadata lookup
- Add GetFileWithRange method for paginated file retrieval
- Implement countLines and applyLineRange helpers

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-04 01:30:39 +01:00

105 lines
3.1 KiB
Go

package database
// SchemaVersion is the current database schema version.
// When this changes, the database will be dropped and recreated.
const SchemaVersion = 2
// Common SQL statements shared between implementations.
const (
// SchemaInfoTable creates the schema version tracking table.
SchemaInfoTable = `
CREATE TABLE IF NOT EXISTS schema_info (
version INTEGER NOT NULL
)`
// RevisionsTable creates the revisions table.
RevisionsTable = `
CREATE TABLE IF NOT EXISTS revisions (
id INTEGER PRIMARY KEY,
git_hash TEXT NOT NULL UNIQUE,
channel_name TEXT,
commit_date TIMESTAMP,
indexed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
option_count INTEGER NOT NULL DEFAULT 0
)`
// OptionsTable creates the options table.
OptionsTable = `
CREATE TABLE IF NOT EXISTS options (
id INTEGER PRIMARY KEY,
revision_id INTEGER NOT NULL REFERENCES revisions(id) ON DELETE CASCADE,
name TEXT NOT NULL,
parent_path TEXT NOT NULL,
type TEXT,
default_value TEXT,
example TEXT,
description TEXT,
read_only BOOLEAN NOT NULL DEFAULT FALSE
)`
// DeclarationsTable creates the declarations table.
DeclarationsTable = `
CREATE TABLE IF NOT EXISTS declarations (
id INTEGER PRIMARY KEY,
option_id INTEGER NOT NULL REFERENCES options(id) ON DELETE CASCADE,
file_path TEXT NOT NULL,
line INTEGER
)`
// FilesTable creates the files table.
FilesTable = `
CREATE TABLE IF NOT EXISTS files (
id INTEGER PRIMARY KEY,
revision_id INTEGER NOT NULL REFERENCES revisions(id) ON DELETE CASCADE,
file_path TEXT NOT NULL,
extension TEXT,
content TEXT NOT NULL,
byte_size INTEGER NOT NULL DEFAULT 0,
line_count INTEGER NOT NULL DEFAULT 0
)`
)
// Index creation statements.
const (
// IndexOptionsRevisionName creates an index on options(revision_id, name).
IndexOptionsRevisionName = `
CREATE INDEX IF NOT EXISTS idx_options_revision_name
ON options(revision_id, name)`
// IndexOptionsRevisionParent creates an index on options(revision_id, parent_path).
IndexOptionsRevisionParent = `
CREATE INDEX IF NOT EXISTS idx_options_revision_parent
ON options(revision_id, parent_path)`
// IndexFilesRevisionPath creates an index on files(revision_id, file_path).
IndexFilesRevisionPath = `
CREATE UNIQUE INDEX IF NOT EXISTS idx_files_revision_path
ON files(revision_id, file_path)`
// IndexDeclarationsOption creates an index on declarations(option_id).
IndexDeclarationsOption = `
CREATE INDEX IF NOT EXISTS idx_declarations_option
ON declarations(option_id)`
)
// Drop statements for schema recreation.
const (
DropSchemaInfo = `DROP TABLE IF EXISTS schema_info`
DropDeclarations = `DROP TABLE IF EXISTS declarations`
DropOptions = `DROP TABLE IF EXISTS options`
DropFiles = `DROP TABLE IF EXISTS files`
DropRevisions = `DROP TABLE IF EXISTS revisions`
)
// ParentPath extracts the parent path from an option name.
// For example, "services.nginx.enable" returns "services.nginx".
// Top-level options return an empty string.
func ParentPath(name string) string {
for i := len(name) - 1; i >= 0; i-- {
if name[i] == '.' {
return name[:i]
}
}
return ""
}