@esneiderbravo/speclaw 0.1.5 → 0.1.6
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.
- package/dist/modules/compass/db.js +46 -4
- package/package.json +1 -1
|
@@ -49,13 +49,53 @@ CREATE TABLE IF NOT EXISTS node_embeddings (
|
|
|
49
49
|
);
|
|
50
50
|
`;
|
|
51
51
|
/** Schema version stamped into the `meta` table on first creation. */
|
|
52
|
-
export const SCHEMA_VERSION = "
|
|
52
|
+
export const SCHEMA_VERSION = "3";
|
|
53
|
+
/** The stamped schema version, or null if the db predates versioning / has no meta table. */
|
|
54
|
+
function readSchemaVersion(db) {
|
|
55
|
+
try {
|
|
56
|
+
const row = db.prepare("SELECT value FROM meta WHERE key = 'schema_version'").get();
|
|
57
|
+
return row ? String(row.value) : null;
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return null; // meta table doesn't exist yet
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Decide whether an existing database is from an incompatible schema and must be
|
|
65
|
+
* rebuilt. A fresh database (no tables) needs no reset — the schema will create
|
|
66
|
+
* them. An existing one is stale if its stamped version differs from the current
|
|
67
|
+
* one, or if the `edges` table is missing a column the current code writes to
|
|
68
|
+
* (guards against past schema changes that weren't version-bumped).
|
|
69
|
+
*/
|
|
70
|
+
function isStale(db) {
|
|
71
|
+
const hasEdges = db
|
|
72
|
+
.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'edges'")
|
|
73
|
+
.get();
|
|
74
|
+
if (!hasEdges)
|
|
75
|
+
return false;
|
|
76
|
+
if (readSchemaVersion(db) !== SCHEMA_VERSION)
|
|
77
|
+
return true;
|
|
78
|
+
const cols = db.prepare("PRAGMA table_info(edges)").all().map((c) => c.name);
|
|
79
|
+
return !cols.includes("src_node_id") || !cols.includes("dst_node_id");
|
|
80
|
+
}
|
|
81
|
+
/** Drop every table (children first) so the current schema can be recreated cleanly. */
|
|
82
|
+
function resetSchema(db) {
|
|
83
|
+
db.exec(`
|
|
84
|
+
DROP TABLE IF EXISTS node_embeddings;
|
|
85
|
+
DROP TABLE IF EXISTS edges;
|
|
86
|
+
DROP TABLE IF EXISTS nodes;
|
|
87
|
+
DROP TABLE IF EXISTS files;
|
|
88
|
+
DROP TABLE IF EXISTS meta;
|
|
89
|
+
`);
|
|
90
|
+
}
|
|
53
91
|
/**
|
|
54
92
|
* Open (creating if needed) the index database at `<projectPath>/.speclaw/index.db`.
|
|
55
93
|
*
|
|
56
|
-
* Ensures the `.speclaw` directory exists,
|
|
57
|
-
*
|
|
58
|
-
*
|
|
94
|
+
* Ensures the `.speclaw` directory exists, enables WAL journaling and foreign
|
|
95
|
+
* keys, and applies the schema. If an existing database is from an incompatible
|
|
96
|
+
* schema (e.g. after a speclaw upgrade), it is dropped and rebuilt — `.speclaw`
|
|
97
|
+
* is fully regenerable, so the next index just repopulates it. The schema
|
|
98
|
+
* version is stamped on a fresh (or freshly reset) database.
|
|
59
99
|
*
|
|
60
100
|
* @param projectPath - Absolute path to the project root.
|
|
61
101
|
* @returns An open connection to the index database.
|
|
@@ -65,6 +105,8 @@ export function openDb(projectPath) {
|
|
|
65
105
|
fs.mkdirSync(dir, { recursive: true });
|
|
66
106
|
const db = new DatabaseSync(path.join(dir, "index.db"));
|
|
67
107
|
db.exec("PRAGMA journal_mode = WAL; PRAGMA foreign_keys = ON;");
|
|
108
|
+
if (isStale(db))
|
|
109
|
+
resetSchema(db);
|
|
68
110
|
db.exec(SCHEMA);
|
|
69
111
|
const row = db
|
|
70
112
|
.prepare("SELECT value FROM meta WHERE key = 'schema_version'")
|