@ppagent/memory 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -343,9 +343,40 @@ var SqliteProvider = class {
343
343
  const ftsCols = def.indexes.filter((i) => i.kind === "fts").map((i) => i.column);
344
344
  if (ftsCols.length > 0) {
345
345
  this.ftsColumns.set(def.name, ftsCols);
346
+ const ftsTable = `${def.name}_fts`;
347
+ const exists = this.db.prepare("SELECT 1 AS found FROM sqlite_master WHERE type='table' AND name = ?").get(ftsTable);
348
+ if (exists) {
349
+ const actual = this.db.pragma(`table_info("${ftsTable}")`).map((row) => row.name).filter((name) => !["rank"].includes(name));
350
+ if (actual.join("\0") !== ftsCols.join("\0")) {
351
+ this.db.exec(`DROP TABLE "${ftsTable}"`);
352
+ }
353
+ }
346
354
  this.db.exec(
347
- `CREATE VIRTUAL TABLE IF NOT EXISTS "${def.name}_fts" USING fts5(${ftsCols.map((c) => `"${c}"`).join(", ")})`
355
+ `CREATE VIRTUAL TABLE IF NOT EXISTS "${ftsTable}" USING fts5(${ftsCols.map((c) => `"${c}"`).join(", ")})`
356
+ );
357
+ const ftsCount = Number(
358
+ this.db.prepare(`SELECT COUNT(*) AS count FROM "${ftsTable}"`).get().count
359
+ );
360
+ const baseCount = Number(
361
+ this.db.prepare(`SELECT COUNT(*) AS count FROM "${def.name}"`).get().count
348
362
  );
363
+ if (ftsCount !== baseCount) {
364
+ this.db.exec(`DELETE FROM "${ftsTable}"`);
365
+ const sourceRows = this.db.prepare(
366
+ `SELECT rowid, ${ftsCols.map((column) => `"${column}"`).join(", ")} FROM "${def.name}"`
367
+ ).all();
368
+ const insert = this.db.prepare(
369
+ `INSERT INTO "${ftsTable}" (rowid, ${ftsCols.map((column) => `"${column}"`).join(", ")}) VALUES (${["?", ...ftsCols.map(() => "?")].join(", ")})`
370
+ );
371
+ this.db.transaction(() => {
372
+ for (const row of sourceRows) {
373
+ insert.run(
374
+ row.rowid,
375
+ ...ftsCols.map((column) => tokenizeForIndex(String(row[column] ?? "")))
376
+ );
377
+ }
378
+ })();
379
+ }
349
380
  }
350
381
  }
351
382
  mustDef(table) {