Fix vector table errors
This commit is contained in:
Vendored
+2
@@ -6,6 +6,7 @@ import { select } from "@inquirer/prompts";
|
||||
import { Database } from "bun:sqlite";
|
||||
import listTables from "./list-tables";
|
||||
import runSQL from "./run-sql";
|
||||
import * as sqliteVec from "sqlite-vec";
|
||||
export default function () {
|
||||
return new Command("admin")
|
||||
.description("View Tables and Data, Run SQL Queries, Etc.")
|
||||
@@ -13,6 +14,7 @@ export default function () {
|
||||
const { config } = await init();
|
||||
const { db_file_path } = grabDBDir({ config });
|
||||
const db = new Database(db_file_path);
|
||||
sqliteVec.load(db);
|
||||
console.log(chalk.bold(chalk.blue("\nBun SQLite Admin\n")));
|
||||
try {
|
||||
while (true) {
|
||||
|
||||
+1
@@ -44,6 +44,7 @@ declare class SQLiteSchemaManager {
|
||||
* Add a new column to existing table
|
||||
*/
|
||||
private addColumn;
|
||||
private checkIfTableExists;
|
||||
/**
|
||||
* Recreate table (for complex schema changes)
|
||||
*/
|
||||
|
||||
Vendored
+24
-13
@@ -102,13 +102,10 @@ class SQLiteSchemaManager {
|
||||
await this.createTable(table);
|
||||
this.insertDbManagerTable(table.tableName);
|
||||
}
|
||||
else if (!table.isVector) {
|
||||
else {
|
||||
// Update existing table
|
||||
await this.updateTable(table);
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
// Sync indexes
|
||||
await this.syncIndexes(table);
|
||||
}
|
||||
@@ -133,9 +130,9 @@ class SQLiteSchemaManager {
|
||||
const columns = [];
|
||||
const foreignKeys = [];
|
||||
for (const field of new_table.fields) {
|
||||
const columnDef = this.buildColumnDefinition(field);
|
||||
const columnDef = this.buildColumnDefinition(field, table.isVector);
|
||||
columns.push(columnDef);
|
||||
if (field.foreignKey) {
|
||||
if (field.foreignKey && !table.isVector) {
|
||||
foreignKeys.push(this.buildForeignKeyConstraint(field));
|
||||
}
|
||||
}
|
||||
@@ -224,6 +221,12 @@ class SQLiteSchemaManager {
|
||||
const sql = `ALTER TABLE "${tableName}" ADD COLUMN ${cleanDef}`;
|
||||
this.db.run(sql);
|
||||
}
|
||||
checkIfTableExists(table) {
|
||||
const tableExists = this.db
|
||||
.query(`SELECT name FROM sqlite_master WHERE type='table' AND name=?`)
|
||||
.get(table);
|
||||
return Boolean(tableExists?.name);
|
||||
}
|
||||
/**
|
||||
* Recreate table (for complex schema changes)
|
||||
*/
|
||||
@@ -232,11 +235,14 @@ class SQLiteSchemaManager {
|
||||
if (!this.recreate_vector_table) {
|
||||
return;
|
||||
}
|
||||
console.log(`Recreating vector table: ${table.tableName}`);
|
||||
const existingRows = this.db
|
||||
.query(`SELECT * FROM "${table.tableName}"`)
|
||||
.all();
|
||||
this.db.run(`DROP TABLE "${table.tableName}"`);
|
||||
const does_table_exist = this.checkIfTableExists(table.tableName);
|
||||
let existingRows = [];
|
||||
if (does_table_exist) {
|
||||
existingRows = this.db
|
||||
.query(`SELECT * FROM "${table.tableName}"`)
|
||||
.all();
|
||||
this.db.run(`DROP TABLE "${table.tableName}"`);
|
||||
}
|
||||
await this.createTable(table);
|
||||
if (existingRows.length > 0) {
|
||||
for (let i = 0; i < existingRows.length; i++) {
|
||||
@@ -274,7 +280,7 @@ class SQLiteSchemaManager {
|
||||
/**
|
||||
* Build column definition SQL
|
||||
*/
|
||||
buildColumnDefinition(field) {
|
||||
buildColumnDefinition(field, is_vector) {
|
||||
if (!field.fieldName) {
|
||||
throw new Error("Field name is required");
|
||||
}
|
||||
@@ -284,7 +290,12 @@ class SQLiteSchemaManager {
|
||||
const parts = [fieldName];
|
||||
// Data type mapping
|
||||
const dataType = this.mapDataType(field);
|
||||
parts.push(dataType);
|
||||
if (dataType == "BLOB") {
|
||||
parts.push("FLOAT[128]");
|
||||
}
|
||||
else {
|
||||
parts.push(dataType);
|
||||
}
|
||||
// Primary key
|
||||
if (field.primaryKey) {
|
||||
parts.push("PRIMARY KEY");
|
||||
|
||||
Vendored
+3
@@ -85,6 +85,9 @@ export interface BUN_SQLITE_TableSchemaType {
|
||||
* If this is a vector table
|
||||
*/
|
||||
isVector?: boolean;
|
||||
/**
|
||||
* Type of vector. Defaults to `vec0`
|
||||
*/
|
||||
vectorType?: string;
|
||||
}
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user