Add tests, schema sync safeguards, and query/handler improvements
- Add bun test suite: SQL generator and value unit tests, plus MariaDB integration tests gated on BUN_MARIADB_TEST_DB - schema: add --dry-run and require confirmation (or --yes) before dropping tables/columns; block unapproved destructive statements - Always AND targetId with the query, regardless of searchOperator - Initialize lazily on first call; init throws instead of exiting - Generate IN (?, ?, ...) lists for IN / NOT IN - Share value binding between insert and update (booleans, embeddings, function values, "" -> NULL for non-text columns) - Keep Buffers and convert BigInt/Date without a JSON round-trip - DbSQL: report success for empty SELECT results - Run update() in a transaction with row locking - Key the client cache on the full connection config - Consolidate identifier quoting, drop dead code, merge column lookup - Read CLI version from package.json, pin @types/bun, chmod CLI on compile - Rebuild dist Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -1,73 +0,0 @@
|
||||
import type { BunMariaDBConfig } from "../../types";
|
||||
import { querySchemaRows } from "./run-schema-query";
|
||||
import schemaCondition from "./schema-condition";
|
||||
|
||||
export type ColumnInfoRow = {
|
||||
name: string;
|
||||
type: string;
|
||||
comment?: string;
|
||||
isNullable: boolean;
|
||||
columnDefault: string | null;
|
||||
extra: string;
|
||||
};
|
||||
|
||||
export default async function getTableColumnsGemini({
|
||||
tableName,
|
||||
config,
|
||||
}: {
|
||||
tableName: string;
|
||||
config?: BunMariaDBConfig;
|
||||
}): Promise<ColumnInfoRow[]> {
|
||||
const schemaCond = schemaCondition(config);
|
||||
const rows = await querySchemaRows<{
|
||||
COLUMN_NAME: string;
|
||||
COLUMN_TYPE: string;
|
||||
COLUMN_COMMENT: string;
|
||||
IS_NULLABLE: string;
|
||||
COLUMN_DEFAULT: string | null;
|
||||
EXTRA: string;
|
||||
IS_JSON: number | boolean;
|
||||
}>({
|
||||
query: `
|
||||
SELECT
|
||||
c.COLUMN_NAME,
|
||||
c.COLUMN_TYPE,
|
||||
c.COLUMN_COMMENT,
|
||||
c.IS_NULLABLE,
|
||||
c.COLUMN_DEFAULT,
|
||||
c.EXTRA,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM information_schema.CHECK_CONSTRAINTS cc
|
||||
WHERE cc.CONSTRAINT_SCHEMA = c.TABLE_SCHEMA
|
||||
AND cc.TABLE_NAME = c.TABLE_NAME
|
||||
AND cc.CHECK_CLAUSE LIKE CONCAT('%json_valid(\`', c.COLUMN_NAME, '\`)%')
|
||||
) AS IS_JSON
|
||||
FROM information_schema.COLUMNS c
|
||||
WHERE ${schemaCond.where.replace(/\bTABLE_SCHEMA\b/g, "c.TABLE_SCHEMA")}
|
||||
AND c.TABLE_NAME = ?
|
||||
ORDER BY c.ORDINAL_POSITION
|
||||
`,
|
||||
values: [...schemaCond.values, tableName],
|
||||
config,
|
||||
});
|
||||
|
||||
return rows.map((row) => {
|
||||
const isJson = Number(row.IS_JSON) === 1;
|
||||
|
||||
// Normalize longtext with a json_valid constraint to "json"
|
||||
let resolvedType = row.COLUMN_TYPE;
|
||||
if (isJson && row.COLUMN_TYPE.toLowerCase() === "longtext") {
|
||||
resolvedType = "json";
|
||||
}
|
||||
|
||||
return {
|
||||
name: row.COLUMN_NAME,
|
||||
type: resolvedType,
|
||||
comment: row.COLUMN_COMMENT,
|
||||
isNullable: String(row.IS_NULLABLE).toUpperCase() === "YES",
|
||||
columnDefault: row.COLUMN_DEFAULT,
|
||||
extra: row.EXTRA || "",
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -11,6 +11,9 @@ export type ColumnInfoRow = {
|
||||
extra: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Live column info. LONGTEXT columns with a `json_valid` check are reported as `json`.
|
||||
*/
|
||||
export default async function getTableColumns({
|
||||
tableName,
|
||||
config,
|
||||
@@ -26,18 +29,48 @@ export default async function getTableColumns({
|
||||
IS_NULLABLE: string;
|
||||
COLUMN_DEFAULT: string | null;
|
||||
EXTRA: string;
|
||||
IS_JSON: number | boolean;
|
||||
}>({
|
||||
query: `SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_COMMENT, IS_NULLABLE, COLUMN_DEFAULT, EXTRA FROM information_schema.COLUMNS WHERE ${schemaCond.where} AND TABLE_NAME = ? ORDER BY ORDINAL_POSITION`,
|
||||
query: `
|
||||
SELECT
|
||||
c.COLUMN_NAME,
|
||||
c.COLUMN_TYPE,
|
||||
c.COLUMN_COMMENT,
|
||||
c.IS_NULLABLE,
|
||||
c.COLUMN_DEFAULT,
|
||||
c.EXTRA,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM information_schema.CHECK_CONSTRAINTS cc
|
||||
WHERE cc.CONSTRAINT_SCHEMA = c.TABLE_SCHEMA
|
||||
AND cc.TABLE_NAME = c.TABLE_NAME
|
||||
AND cc.CHECK_CLAUSE LIKE CONCAT('%json_valid(\`', c.COLUMN_NAME, '\`)%')
|
||||
) AS IS_JSON
|
||||
FROM information_schema.COLUMNS c
|
||||
WHERE ${schemaCond.where.replace(/\bTABLE_SCHEMA\b/g, "c.TABLE_SCHEMA")}
|
||||
AND c.TABLE_NAME = ?
|
||||
ORDER BY c.ORDINAL_POSITION
|
||||
`,
|
||||
values: [...schemaCond.values, tableName],
|
||||
config,
|
||||
});
|
||||
|
||||
return rows.map((row) => ({
|
||||
name: row.COLUMN_NAME,
|
||||
type: row.COLUMN_TYPE,
|
||||
comment: row.COLUMN_COMMENT,
|
||||
isNullable: String(row.IS_NULLABLE).toUpperCase() === "YES",
|
||||
columnDefault: row.COLUMN_DEFAULT,
|
||||
extra: row.EXTRA || "",
|
||||
}));
|
||||
return rows.map((row) => {
|
||||
const isJson = Number(row.IS_JSON) === 1;
|
||||
|
||||
// Normalize longtext with a json_valid constraint to "json"
|
||||
let resolvedType = row.COLUMN_TYPE;
|
||||
if (isJson && row.COLUMN_TYPE.toLowerCase() === "longtext") {
|
||||
resolvedType = "json";
|
||||
}
|
||||
|
||||
return {
|
||||
name: row.COLUMN_NAME,
|
||||
type: resolvedType,
|
||||
comment: row.COLUMN_COMMENT,
|
||||
isNullable: String(row.IS_NULLABLE).toUpperCase() === "YES",
|
||||
columnDefault: row.COLUMN_DEFAULT,
|
||||
extra: row.EXTRA || "",
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { quoteIdentifier } from "../../utils/sql-identifier";
|
||||
|
||||
export default function MariaDBQuoteGen(str: string) {
|
||||
return `\`${str.replace(/`/g, "``")}\``;
|
||||
return quoteIdentifier(str);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,62 @@
|
||||
import type { BunMariaDBConfig } from "../../types";
|
||||
import dbHandler from "../db-handler";
|
||||
|
||||
type SchemaRunState = {
|
||||
/** Record statements instead of executing them */
|
||||
dryRun: boolean;
|
||||
statements: string[];
|
||||
/**
|
||||
* Destructive statements that may run. `true` allows all of them;
|
||||
* otherwise only exact matches are allowed.
|
||||
*/
|
||||
allowDestructive: true | Set<string>;
|
||||
};
|
||||
|
||||
let schemaRunState: SchemaRunState | null = null;
|
||||
|
||||
/**
|
||||
* Statements that discard stored data (tables or column values).
|
||||
*/
|
||||
export function isDestructiveSchemaStatement(query: string): boolean {
|
||||
return /^\s*DROP\s+TABLE\b/i.test(query) || /\bDROP\s+COLUMN\b/i.test(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run `fn` with schema statements recorded but not executed.
|
||||
* Reads (information_schema) still run, so the plan is a close approximation:
|
||||
* steps that depend on earlier changes (e.g. after a table rename) may differ.
|
||||
*/
|
||||
export async function planSchemaQueries(
|
||||
fn: () => Promise<unknown>,
|
||||
): Promise<string[]> {
|
||||
const previous = schemaRunState;
|
||||
schemaRunState = { dryRun: true, statements: [], allowDestructive: true };
|
||||
|
||||
try {
|
||||
await fn();
|
||||
return schemaRunState.statements;
|
||||
} finally {
|
||||
schemaRunState = previous;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run `fn`, refusing any destructive statement that was not approved.
|
||||
*/
|
||||
export async function runSchemaQueriesGuarded(
|
||||
fn: () => Promise<unknown>,
|
||||
allowDestructive: true | Set<string>,
|
||||
): Promise<void> {
|
||||
const previous = schemaRunState;
|
||||
schemaRunState = { dryRun: false, statements: [], allowDestructive };
|
||||
|
||||
try {
|
||||
await fn();
|
||||
} finally {
|
||||
schemaRunState = previous;
|
||||
}
|
||||
}
|
||||
|
||||
export default async function runSchemaQuery({
|
||||
query,
|
||||
values,
|
||||
@@ -10,6 +66,25 @@ export default async function runSchemaQuery({
|
||||
values?: any[];
|
||||
config?: BunMariaDBConfig;
|
||||
}): Promise<void> {
|
||||
if (schemaRunState) {
|
||||
schemaRunState.statements.push(query);
|
||||
|
||||
if (schemaRunState.dryRun) {
|
||||
return;
|
||||
}
|
||||
|
||||
const allowed = schemaRunState.allowDestructive;
|
||||
if (
|
||||
allowed !== true &&
|
||||
isDestructiveSchemaStatement(query) &&
|
||||
!allowed.has(query)
|
||||
) {
|
||||
throw new Error(
|
||||
`Refusing unapproved destructive statement: ${query}. Re-run with --dry-run to review, or --yes to allow it.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const res = await dbHandler({
|
||||
query,
|
||||
values,
|
||||
|
||||
@@ -8,7 +8,6 @@ import buildColumnDefinition, {
|
||||
} from "./build-column-definition";
|
||||
import createTable from "./create-table";
|
||||
import getTableColumns, { type ColumnInfoRow } from "./get-table-columns";
|
||||
import getTableColumnsGemini from "./get-table-columns-gemnini";
|
||||
import isVectorField from "./is-vector-field";
|
||||
import mapDataType from "./map-data-types";
|
||||
import MariaDBQuoteGen from "./mariadb-quote-gen";
|
||||
@@ -278,7 +277,7 @@ export default async function updateTable({
|
||||
table: BUN_MARIADB_TableSchemaType;
|
||||
config?: BunMariaDBConfig;
|
||||
}): Promise<void> {
|
||||
const existingColumns = await getTableColumnsGemini({
|
||||
const existingColumns = await getTableColumns({
|
||||
tableName: table.tableName,
|
||||
config,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user