Remove table.isVector feature altogether. All vector features are handled per column
This commit is contained in:
+3
-1
@@ -33,4 +33,6 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
.DS_Store
|
||||
/test
|
||||
.vscode
|
||||
.dump
|
||||
.dump
|
||||
|
||||
/.bun-mariadb
|
||||
@@ -241,7 +241,6 @@ interface BUN_MARIADB_TableSchemaType {
|
||||
parentTableName?: string; // inherit / merge fields from another table
|
||||
tableNameOld?: string; // rename: old name triggers ALTER TABLE RENAME
|
||||
collation?: "utf8mb4_bin" | "utf8mb4_unicode_520_ci";
|
||||
isVector?: boolean; // mark as vector-oriented table
|
||||
}
|
||||
```
|
||||
|
||||
@@ -743,7 +742,6 @@ MariaDB native `VECTOR(n)` columns and `VECTOR INDEX` are supported (MariaDB 11.
|
||||
```ts
|
||||
{
|
||||
tableName: "documents",
|
||||
isVector: true,
|
||||
fields: [
|
||||
{
|
||||
fieldName: "embedding",
|
||||
|
||||
Vendored
+3
-1
@@ -26,7 +26,9 @@ export default async function DbSQL({ sql, values }) {
|
||||
const singleRaw = res.single_res;
|
||||
return {
|
||||
...res,
|
||||
success: true,
|
||||
success: isSelect
|
||||
? Boolean(single_res) || Boolean(payload?.[0])
|
||||
: true,
|
||||
payload,
|
||||
single_res,
|
||||
debug: {
|
||||
|
||||
Vendored
+1
-1
@@ -15,7 +15,7 @@ export default async function createTable({ table, config, }) {
|
||||
if (field.primaryKey && field.fieldName) {
|
||||
primaryKeys.push(field.fieldName);
|
||||
}
|
||||
if (field.foreignKey && !table.isVector) {
|
||||
if (field.foreignKey) {
|
||||
foreignKeys.push(buildForeignKeyConstraint(field, table.tableName));
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+1
-2
@@ -1,7 +1,6 @@
|
||||
import type { BUN_MARIADB_TableSchemaType, BunMariaDBConfig } from "../../types";
|
||||
/**
|
||||
* Full table rebuild. For `isVector` tables this drops and recreates in place
|
||||
* (preserving rows when possible). For regular tables it uses a temp-table swap.
|
||||
* Full table rebuild using a temp-table swap (preserving rows when possible).
|
||||
*/
|
||||
export default function recreateTable({ table, config, }: {
|
||||
table: BUN_MARIADB_TableSchemaType;
|
||||
|
||||
Vendored
+1
-44
@@ -13,8 +13,7 @@ async function checkIfTableExists({ tableName, config, }) {
|
||||
return Boolean(rows[0]?.table_exists);
|
||||
}
|
||||
/**
|
||||
* Full table rebuild. For `isVector` tables this drops and recreates in place
|
||||
* (preserving rows when possible). For regular tables it uses a temp-table swap.
|
||||
* Full table rebuild using a temp-table swap (preserving rows when possible).
|
||||
*/
|
||||
export default async function recreateTable({ table, config, }) {
|
||||
const doesTableExist = await checkIfTableExists({
|
||||
@@ -25,48 +24,6 @@ export default async function recreateTable({ table, config, }) {
|
||||
await createTable({ table, config });
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Vector tables: drop + recreate + reinsert (MariaDB VECTOR INDEX / dim
|
||||
* changes are not reliably alterable in place).
|
||||
*/
|
||||
if (table.isVector) {
|
||||
console.log(`Recreating vector table: ${table.tableName}`);
|
||||
const existingRows = await querySchemaRows({
|
||||
query: `SELECT * FROM ${MariaDBQuoteGen(table.tableName)}`,
|
||||
config,
|
||||
});
|
||||
await runSchemaQuery({ query: `SET FOREIGN_KEY_CHECKS = 0`, config });
|
||||
try {
|
||||
await runSchemaQuery({
|
||||
query: `DROP TABLE IF EXISTS ${MariaDBQuoteGen(table.tableName)}`,
|
||||
config,
|
||||
});
|
||||
await createTable({ table, config });
|
||||
}
|
||||
finally {
|
||||
await runSchemaQuery({ query: `SET FOREIGN_KEY_CHECKS = 1`, config });
|
||||
}
|
||||
if (existingRows.length > 0) {
|
||||
const schemaFieldNames = new Set((table.fields || [])
|
||||
.map((f) => f.fieldName)
|
||||
.filter((n) => Boolean(n)));
|
||||
for (const row of existingRows) {
|
||||
const columns = Object.keys(row).filter((c) => schemaFieldNames.has(c));
|
||||
if (columns.length === 0)
|
||||
continue;
|
||||
const placeholders = columns.map(() => "?").join(", ");
|
||||
const columnList = columns
|
||||
.map((c) => MariaDBQuoteGen(c))
|
||||
.join(", ");
|
||||
await runSchemaQuery({
|
||||
query: `INSERT INTO ${MariaDBQuoteGen(table.tableName)} (${columnList}) VALUES (${placeholders})`,
|
||||
values: columns.map((c) => row[c] ?? null),
|
||||
config,
|
||||
});
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
const tempTableName = `${table.tableName}_temp_${Date.now()}`;
|
||||
const backupOldTableName = `${table.tableName}_old_${Date.now()}`;
|
||||
const existingColumns = await getTableColumns({
|
||||
|
||||
Vendored
-1
@@ -23,7 +23,6 @@ export default function resolveTable(table, db_schema) {
|
||||
tableName: table.tableName,
|
||||
tableDescription: table.tableDescription || parentTable.tableDescription,
|
||||
collation: table.collation || parentTable.collation,
|
||||
isVector: table.isVector !== undefined ? table.isVector : parentTable.isVector,
|
||||
fields: Array.from(mergedFieldsMap.values()),
|
||||
indexes: _.uniqBy([...(parentTable.indexes || []), ...(table.indexes || [])], "indexName"),
|
||||
uniqueConstraints: [
|
||||
|
||||
Vendored
-3
@@ -15,9 +15,6 @@ function rulesMatch(live, desired) {
|
||||
ruleIsCascade(live.updateRule) === desired.cascadeUpdate);
|
||||
}
|
||||
export function grabDesiredForeignKeys(table) {
|
||||
if (table.isVector) {
|
||||
return [];
|
||||
}
|
||||
const desired = [];
|
||||
for (const field of table.fields || []) {
|
||||
const fk = field.foreignKey;
|
||||
|
||||
Vendored
-2
@@ -6,8 +6,6 @@ import { grabDesiredUniqueConstraints } from "./sync-unique-constraints";
|
||||
function isVectorIndexDef(index, table) {
|
||||
if (index.indexType === "VECTOR")
|
||||
return true;
|
||||
if (table.isVector)
|
||||
return true;
|
||||
const firstFieldName = index.indexTableFields?.[0];
|
||||
if (!firstFieldName)
|
||||
return false;
|
||||
|
||||
Vendored
-4
@@ -74,10 +74,6 @@ export interface BUN_MARIADB_TableSchemaType {
|
||||
*/
|
||||
childTableDbId?: string | number;
|
||||
collation?: (typeof MariaDBCollations)[number];
|
||||
/**
|
||||
* If this is a vector-oriented table (native MariaDB VECTOR columns/indexes)
|
||||
*/
|
||||
isVector?: boolean;
|
||||
}
|
||||
/**
|
||||
* Reference object used to link a table to one of its child tables.
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@moduletrace/bun-mariadb",
|
||||
"version": "1.0.10",
|
||||
"version": "1.0.11",
|
||||
"description": "Schema-driven MariaDB manager for Bun",
|
||||
"author": "Benjamin Toby",
|
||||
"license": "MIT",
|
||||
|
||||
@@ -39,7 +39,9 @@ export default async function DbSQL<
|
||||
|
||||
return {
|
||||
...res,
|
||||
success: true,
|
||||
success: isSelect
|
||||
? Boolean(single_res) || Boolean(payload?.[0])
|
||||
: true,
|
||||
payload,
|
||||
single_res,
|
||||
debug: {
|
||||
|
||||
@@ -30,7 +30,7 @@ export default async function createTable({
|
||||
primaryKeys.push(field.fieldName);
|
||||
}
|
||||
|
||||
if (field.foreignKey && !table.isVector) {
|
||||
if (field.foreignKey) {
|
||||
foreignKeys.push(
|
||||
buildForeignKeyConstraint(field, table.tableName),
|
||||
);
|
||||
|
||||
@@ -26,8 +26,7 @@ async function checkIfTableExists({
|
||||
}
|
||||
|
||||
/**
|
||||
* Full table rebuild. For `isVector` tables this drops and recreates in place
|
||||
* (preserving rows when possible). For regular tables it uses a temp-table swap.
|
||||
* Full table rebuild using a temp-table swap (preserving rows when possible).
|
||||
*/
|
||||
export default async function recreateTable({
|
||||
table,
|
||||
@@ -46,58 +45,6 @@ export default async function recreateTable({
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Vector tables: drop + recreate + reinsert (MariaDB VECTOR INDEX / dim
|
||||
* changes are not reliably alterable in place).
|
||||
*/
|
||||
if (table.isVector) {
|
||||
console.log(`Recreating vector table: ${table.tableName}`);
|
||||
|
||||
const existingRows = await querySchemaRows<Record<string, any>>({
|
||||
query: `SELECT * FROM ${MariaDBQuoteGen(table.tableName)}`,
|
||||
config,
|
||||
});
|
||||
|
||||
await runSchemaQuery({ query: `SET FOREIGN_KEY_CHECKS = 0`, config });
|
||||
try {
|
||||
await runSchemaQuery({
|
||||
query: `DROP TABLE IF EXISTS ${MariaDBQuoteGen(table.tableName)}`,
|
||||
config,
|
||||
});
|
||||
await createTable({ table, config });
|
||||
} finally {
|
||||
await runSchemaQuery({ query: `SET FOREIGN_KEY_CHECKS = 1`, config });
|
||||
}
|
||||
|
||||
if (existingRows.length > 0) {
|
||||
const schemaFieldNames = new Set(
|
||||
(table.fields || [])
|
||||
.map((f) => f.fieldName)
|
||||
.filter((n): n is string => Boolean(n)),
|
||||
);
|
||||
|
||||
for (const row of existingRows) {
|
||||
const columns = Object.keys(row).filter((c) =>
|
||||
schemaFieldNames.has(c),
|
||||
);
|
||||
if (columns.length === 0) continue;
|
||||
|
||||
const placeholders = columns.map(() => "?").join(", ");
|
||||
const columnList = columns
|
||||
.map((c) => MariaDBQuoteGen(c))
|
||||
.join(", ");
|
||||
|
||||
await runSchemaQuery({
|
||||
query: `INSERT INTO ${MariaDBQuoteGen(table.tableName)} (${columnList}) VALUES (${placeholders})`,
|
||||
values: columns.map((c) => row[c] ?? null),
|
||||
config,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const tempTableName = `${table.tableName}_temp_${Date.now()}`;
|
||||
const backupOldTableName = `${table.tableName}_old_${Date.now()}`;
|
||||
const existingColumns = await getTableColumns({
|
||||
|
||||
@@ -41,8 +41,6 @@ export default function resolveTable(
|
||||
tableName: table.tableName,
|
||||
tableDescription: table.tableDescription || parentTable.tableDescription,
|
||||
collation: table.collation || parentTable.collation,
|
||||
isVector:
|
||||
table.isVector !== undefined ? table.isVector : parentTable.isVector,
|
||||
fields: Array.from(mergedFieldsMap.values()),
|
||||
indexes: _.uniqBy(
|
||||
[...(parentTable.indexes || []), ...(table.indexes || [])],
|
||||
|
||||
@@ -57,10 +57,6 @@ function rulesMatch(
|
||||
export function grabDesiredForeignKeys(
|
||||
table: BUN_MARIADB_TableSchemaType,
|
||||
): DesiredForeignKey[] {
|
||||
if (table.isVector) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const desired: DesiredForeignKey[] = [];
|
||||
|
||||
for (const field of table.fields || []) {
|
||||
|
||||
@@ -14,7 +14,6 @@ function isVectorIndexDef(
|
||||
table: BUN_MARIADB_TableSchemaType,
|
||||
): boolean {
|
||||
if (index.indexType === "VECTOR") return true;
|
||||
if (table.isVector) return true;
|
||||
|
||||
const firstFieldName = index.indexTableFields?.[0];
|
||||
if (!firstFieldName) return false;
|
||||
@@ -97,8 +96,7 @@ async function createIndex({
|
||||
.map((field) => MariaDBQuoteGen(field))
|
||||
.join(", ");
|
||||
const typeUpper = index.indexType?.toUpperCase();
|
||||
const isSpecialType =
|
||||
typeUpper === "FULLTEXT" || typeUpper === "SPATIAL";
|
||||
const isSpecialType = typeUpper === "FULLTEXT" || typeUpper === "SPATIAL";
|
||||
const indexPrefix = isSpecialType ? `${typeUpper} ` : "";
|
||||
const indexSuffix =
|
||||
!isSpecialType && (typeUpper === "BTREE" || typeUpper === "HASH")
|
||||
@@ -199,11 +197,7 @@ export default async function syncIndexes({
|
||||
const columnsMatch =
|
||||
details.columns.length === schemaColumns.length &&
|
||||
details.columns.every((col, idx) => col === schemaColumns[idx]);
|
||||
const typeMatch = indexTypesMatch(
|
||||
details.type,
|
||||
schemaIndex,
|
||||
table,
|
||||
);
|
||||
const typeMatch = indexTypesMatch(details.type, schemaIndex, table);
|
||||
|
||||
if (!columnsMatch || !typeMatch) {
|
||||
console.log(`Recreating changed index: ${indexName}`);
|
||||
|
||||
@@ -96,10 +96,6 @@ export interface BUN_MARIADB_TableSchemaType {
|
||||
*/
|
||||
childTableDbId?: string | number;
|
||||
collation?: (typeof MariaDBCollations)[number];
|
||||
/**
|
||||
* If this is a vector-oriented table (native MariaDB VECTOR columns/indexes)
|
||||
*/
|
||||
isVector?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user