@@ -320,6 +320,7 @@ pub fn main() !void {
320320 try ensureParentDir (settings .db_path );
321321 const db = try storage .openFileWithVec (allocator , settings .db_path );
322322 defer storage .close (db );
323+ _ = try storage .initSchema (allocator , db , .{ .embedding_dim = settings .embedding_dim });
323324
324325 var http_client = ollama .StdHttpTransport .init (allocator );
325326 defer http_client .deinit ();
@@ -387,6 +388,9 @@ pub fn main() !void {
387388 const db = try storage .openFileWithVec (allocator , settings .db_path );
388389 defer storage .close (db );
389390
391+ // Always run schema init/migration so older DBs get new columns
392+ _ = try storage .initSchema (allocator , db , .{ .embedding_dim = settings .embedding_dim });
393+
390394 var stderr_buf : [4096 ]u8 = undefined ;
391395 var stderr_writer = std .fs .File .stderr ().writer (& stderr_buf );
392396 const stderr = & stderr_writer .interface ;
@@ -415,9 +419,6 @@ pub fn main() !void {
415419 .model = settings .ollama_model ,
416420 };
417421
418- // Need to init schema before indexing into a fresh DB
419- _ = try storage .initSchema (allocator , db , .{ .embedding_dim = settings .embedding_dim });
420-
421422 _ = try performFullIndex (
422423 allocator ,
423424 db ,
@@ -561,7 +562,7 @@ pub fn main() !void {
561562 const input_text = try readStdin (allocator );
562563 defer allocator .free (input_text );
563564 try runReplaceSymbol (allocator , file_path , pattern , input_text , stdout );
564- tryReindexFile (allocator , settings .db_path , settings .root_path , file_path , registry );
565+ tryReindexFile (allocator , settings .db_path , settings .root_path , file_path , registry , settings . embedding_dim );
565566 try stdout .flush ();
566567 },
567568 .insert_after = > {
@@ -572,7 +573,7 @@ pub fn main() !void {
572573 const input_text = try readStdin (allocator );
573574 defer allocator .free (input_text );
574575 try runInsertAfter (allocator , file_path , pattern , input_text , stdout );
575- tryReindexFile (allocator , settings .db_path , settings .root_path , file_path , registry );
576+ tryReindexFile (allocator , settings .db_path , settings .root_path , file_path , registry , settings . embedding_dim );
576577 try stdout .flush ();
577578 },
578579 .insert_before = > {
@@ -583,7 +584,7 @@ pub fn main() !void {
583584 const input_text = try readStdin (allocator );
584585 defer allocator .free (input_text );
585586 try runInsertBefore (allocator , file_path , pattern , input_text , stdout );
586- tryReindexFile (allocator , settings .db_path , settings .root_path , file_path , registry );
587+ tryReindexFile (allocator , settings .db_path , settings .root_path , file_path , registry , settings . embedding_dim );
587588 try stdout .flush ();
588589 },
589590 .replace_lines = > {
@@ -596,7 +597,7 @@ pub fn main() !void {
596597 const input_text = try readStdin (allocator );
597598 defer allocator .free (input_text );
598599 try runReplaceLines (allocator , file_path , from_ref , to_ref , input_text , stdout );
599- tryReindexFile (allocator , settings .db_path , settings .root_path , file_path , registry );
600+ tryReindexFile (allocator , settings .db_path , settings .root_path , file_path , registry , settings . embedding_dim );
600601 try stdout .flush ();
601602 },
602603 .insert_at = > {
@@ -607,7 +608,7 @@ pub fn main() !void {
607608 const input_text = try readStdin (allocator );
608609 defer allocator .free (input_text );
609610 try runInsertAt (allocator , file_path , ref , input_text , stdout );
610- tryReindexFile (allocator , settings .db_path , settings .root_path , file_path , registry );
611+ tryReindexFile (allocator , settings .db_path , settings .root_path , file_path , registry , settings . embedding_dim );
611612 try stdout .flush ();
612613 },
613614 .replace_content = > {
@@ -619,7 +620,7 @@ pub fn main() !void {
619620 const input_text = try readStdin (allocator );
620621 defer allocator .free (input_text );
621622 try runReplaceContent (allocator , file_path , needle , parsed .regex_mode , parsed .replace_all , input_text , stdout );
622- tryReindexFile (allocator , settings .db_path , settings .root_path , file_path , registry );
623+ tryReindexFile (allocator , settings .db_path , settings .root_path , file_path , registry , settings . embedding_dim );
623624 try stdout .flush ();
624625 },
625626 .references = > {
@@ -637,7 +638,7 @@ pub fn main() !void {
637638 exitWithError ("error: rename requires --file <path>\n " );
638639 const new_name = parsed .rename_to orelse
639640 exitWithError ("error: rename requires --to <new_name>\n " );
640- try runRename (allocator , file_path , pattern , new_name , parsed .output , parsed .dry_run , settings .db_path , settings .root_path , registry , settings .lsp_overrides , stdout );
641+ try runRename (allocator , file_path , pattern , new_name , parsed .output , parsed .dry_run , settings .db_path , settings .root_path , registry , settings .lsp_overrides , settings . embedding_dim , stdout );
641642 try stdout .flush ();
642643 },
643644 .mcp_serve = > {
@@ -739,6 +740,7 @@ pub fn main() !void {
739740 // Open existing DB or create new one (don't destroy existing index)
740741 const db = try storage .openFileWithVec (allocator , settings .db_path );
741742 defer storage .close (db );
743+ _ = try storage .initSchema (allocator , db , .{ .embedding_dim = settings .embedding_dim });
742744
743745 var http_client = ollama .StdHttpTransport .init (allocator );
744746 defer http_client .deinit ();
@@ -1361,7 +1363,7 @@ fn ensureWeightsWithDefaults(path: []const u8) !void {
13611363/// Opens the DB, calls indexer.reindexFile, and closes the DB.
13621364/// If no index exists or any step fails, the edit is still successful —
13631365/// the background watcher will eventually catch up.
1364- fn tryReindexFile (allocator : std.mem.Allocator , db_path : []const u8 , root_path : []const u8 , file_path : []const u8 , registry : plugin.Registry ) void {
1366+ fn tryReindexFile (allocator : std.mem.Allocator , db_path : []const u8 , root_path : []const u8 , file_path : []const u8 , registry : plugin.Registry , embedding_dim : usize ) void {
13651367 var stderr_buf : [4096 ]u8 = undefined ;
13661368 var stderr_writer = std .fs .File .stderr ().writer (& stderr_buf );
13671369 const stderr = & stderr_writer .interface ;
@@ -1372,6 +1374,11 @@ fn tryReindexFile(allocator: std.mem.Allocator, db_path: []const u8, root_path:
13721374 return ;
13731375 };
13741376 defer storage .close (db );
1377+ _ = storage .initSchema (allocator , db , .{ .embedding_dim = embedding_dim }) catch | err | {
1378+ _ = stderr .print ("warning: reindex skipped (schema migration failed): {}\n " , .{err }) catch {};
1379+ _ = stderr .flush () catch {};
1380+ return ;
1381+ };
13751382 const abs_root = std .fs .cwd ().realpathAlloc (allocator , root_path ) catch | err | {
13761383 _ = stderr .print ("warning: reindex skipped (could not resolve root): {}\n " , .{err }) catch {};
13771384 _ = stderr .flush () catch {};
@@ -2418,6 +2425,7 @@ pub fn runRename(
24182425 root_path : []const u8 ,
24192426 registry : plugin.Registry ,
24202427 lsp_overrides : []const config.LspOverride ,
2428+ embedding_dim : usize ,
24212429 writer : * std.Io.Writer ,
24222430) ! void {
24232431 const loc = try locateSymbol (allocator , file_path , pattern ) orelse {
@@ -2562,7 +2570,7 @@ pub fn runRename(
25622570 try writer .print ("warning: failed to apply edits to {s}: {}\n " , .{ path , err });
25632571 continue ;
25642572 };
2565- tryReindexFile (allocator , db_path , root_path , path , registry );
2573+ tryReindexFile (allocator , db_path , root_path , path , registry , embedding_dim );
25662574 }
25672575 }
25682576}
0 commit comments