Elffuss BraidHub

kikocis/continue public

main · 3.2 KiBRawPermalink
1import { type Database } from "sqlite";
2import { type Table } from "vectordb";
3
4import { editConfigFile, migrate } from "../../util/paths.js";
5
6import DocsService, { SqliteDocsRow } from "./DocsService.js";
7
8export async function runLanceMigrations(table: Table) {
9 await new Promise((resolve) => {
10 void migrate(
11 "rename_baseurl_column_for_lance_docs",
12 async () => {
13 try {
14 const schema = await table.schema;
15
16 if (schema.fields.some((field: any) => field.name === "baseurl")) {
17 await table.alterColumns([{ path: "baseurl", rename: "starturl" }]);
18 }
19 } finally {
20 resolve(undefined);
21 }
22 },
23 () => resolve(undefined),
24 );
25 });
26}
27
28export async function runSqliteMigrations(db: Database) {
29 await new Promise((resolve) => {
30 void migrate(
31 "sqlite_modify_docs_columns_and_copy_to_config",
32 async () => {
33 try {
34 const pragma = await db.all(
35 `PRAGMA table_info(${DocsService.sqlitebTableName});`,
36 );
37
38 const hasFaviconCol = pragma.some(
39 (pragma) => pragma.name === "favicon",
40 );
41 if (!hasFaviconCol) {
42 await db.exec(
43 `ALTER TABLE ${DocsService.sqlitebTableName} ADD COLUMN favicon BLOB;`,
44 );
45 }
46
47 const hasBaseUrlCol = pragma.some(
48 (pragma) => pragma.name === "baseUrl",
49 );
50 if (hasBaseUrlCol) {
51 await db.exec(
52 `ALTER TABLE ${DocsService.sqlitebTableName} RENAME COLUMN baseUrl TO startUrl;`,
53 );
54 }
55
56 const needsToUpdateConfig = !hasFaviconCol || hasBaseUrlCol;
57 if (needsToUpdateConfig) {
58 const sqliteDocs = await db.all<
59 Array<Pick<SqliteDocsRow, "title" | "startUrl">>
60 >(`SELECT title, startUrl FROM ${DocsService.sqlitebTableName}`);
61 editConfigFile(
62 (config) => ({
63 ...config,
64 docs: [...(config.docs || []), ...sqliteDocs],
65 }),
66 (config) => ({
67 ...config,
68 docs: [
69 ...(config.docs || []),
70 ...sqliteDocs.map((doc) => ({
71 name: doc.title,
72 startUrl: doc.startUrl,
73 })),
74 ],
75 }),
76 );
77 }
78 } finally {
79 resolve(undefined);
80 }
81 },
82 () => resolve(undefined),
83 );
84 });
85
86 await new Promise((resolve) => {
87 void migrate(
88 "sqlite_delete_docs_with_no_embeddingsProviderId",
89 async () => {
90 try {
91 const pragma = await db.all(
92 `PRAGMA table_info(${DocsService.sqlitebTableName});`,
93 );
94 const hasEmbeddingsProviderColumn = pragma.some(
95 (pragma) => pragma.name === "embeddingsProviderId",
96 );
97 if (!hasEmbeddingsProviderColumn) {
98 // gotta just delete in this case since old docs will be unusable anyway
99 await db.exec(`DROP TABLE ${DocsService.sqlitebTableName};`);
100 }
101 } finally {
102 resolve(undefined);
103 }
104 },
105 () => resolve(undefined),
106 );
107 });
108}