Skip to content

Commit 5c477c1

Browse files
AliceLJYclaude
andcommitted
fix: migrate legacy tables by adding missing scope/timestamp/metadata columns (#326)
Previously doInitialize() only logged a warning when detecting missing columns but never actually added them, causing stats/list/search/update/delete to fail with "No field named scope" on databases upgraded from legacy memory-lancedb. Now uses table.schema() to detect missing columns and table.addColumns() to add them with sensible defaults (scope='global', timestamp=0.0, metadata='{}'). Handles concurrent initialization race via "already exists" error detection. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 699b0f2 commit 5c477c1

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/store.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,39 @@ export class MemoryStore {
258258
try {
259259
table = await db.openTable(TABLE_NAME);
260260

261-
// Check if we need to add scope column for backward compatibility
261+
// Migrate legacy tables: add missing columns for backward compatibility
262262
try {
263-
const sample = await table.query().limit(1).toArray();
264-
if (sample.length > 0 && !("scope" in sample[0])) {
263+
const schema = await table.schema();
264+
const fieldNames = new Set(schema.fields.map((f: { name: string }) => f.name));
265+
266+
const missingColumns: Array<{ name: string; valueSql: string }> = [];
267+
if (!fieldNames.has("scope")) {
268+
missingColumns.push({ name: "scope", valueSql: "'global'" });
269+
}
270+
if (!fieldNames.has("timestamp")) {
271+
missingColumns.push({ name: "timestamp", valueSql: "CAST(0 AS DOUBLE)" });
272+
}
273+
if (!fieldNames.has("metadata")) {
274+
missingColumns.push({ name: "metadata", valueSql: "'{}'" });
275+
}
276+
277+
if (missingColumns.length > 0) {
265278
console.warn(
266-
"Adding scope column for backward compatibility with existing data",
279+
`memory-lancedb-pro: migrating legacy table — adding columns: ${missingColumns.map((c) => c.name).join(", ")}`,
280+
);
281+
await table.addColumns(missingColumns);
282+
console.log(
283+
`memory-lancedb-pro: migration complete — ${missingColumns.length} column(s) added`,
267284
);
268285
}
269286
} catch (err) {
270-
console.warn("Could not check table schema:", err);
287+
const msg = String(err);
288+
if (msg.includes("already exists")) {
289+
// Concurrent initialization race — another process already added the columns
290+
console.log("memory-lancedb-pro: migration columns already exist (concurrent init)");
291+
} else {
292+
console.warn("memory-lancedb-pro: could not check/migrate table schema:", err);
293+
}
271294
}
272295
} catch (_openErr) {
273296
// Table doesn't exist yet — create it

0 commit comments

Comments
 (0)