Port bun-mariadb schema guard, targetId fix, lazy init and tests
- schema: add --dry-run (sync in a rolled-back transaction) and require confirmation (or --yes) before dropping tables or columns; unapproved destructive changes are refused - only rebuild tables whose CREATE TABLE statement changed - always AND targetId with the query, regardless of searchOperator - empty IN / NOT IN lists become 1 = 0 / 1 = 1 - initialize lazily on first call so importing never throws - read CLI version from package.json, chmod CLI on compile, pin @types/bun - add SQL generator, value and schema guard tests Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import type { Database } from "bun:sqlite";
|
||||
import _ from "lodash";
|
||||
import type { CreateDBSchemaParams } from "../../types";
|
||||
import { readLiveSchema } from "../../functions/live-schema";
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
import removeDBManagerTable from "./remove-db-manager-table";
|
||||
import recordSchemaChange from "./record-schema-change";
|
||||
import checkIfTableExists from "./check-if-table-exists";
|
||||
|
||||
type Params = {
|
||||
db: Database;
|
||||
type Params = CreateDBSchemaParams & {
|
||||
existing_tables: string[];
|
||||
schema_tables: string[];
|
||||
};
|
||||
@@ -12,12 +14,8 @@ type Params = {
|
||||
/**
|
||||
* Drop tables that are no longer in the schema
|
||||
*/
|
||||
export default function dropRemovedTables({
|
||||
db,
|
||||
existing_tables,
|
||||
schema_tables,
|
||||
}: Params) {
|
||||
console.log(`Cleaning up tables ...`);
|
||||
export default function dropRemovedTables(params: Params) {
|
||||
const { db, existing_tables, schema_tables } = params;
|
||||
|
||||
const tablesToDrop = existing_tables.filter(
|
||||
(t) =>
|
||||
@@ -35,8 +33,16 @@ export default function dropRemovedTables({
|
||||
}
|
||||
}
|
||||
|
||||
for (const tableName of tablesToDrop) {
|
||||
console.log(`Dropping table: ${tableName}`);
|
||||
for (const tableName of _.uniq(tablesToDrop)) {
|
||||
if (!checkIfTableExists({ db, table_name: tableName })) {
|
||||
removeDBManagerTable({ db, table_name: tableName });
|
||||
continue;
|
||||
}
|
||||
|
||||
recordSchemaChange(params, {
|
||||
description: `Drop table "${tableName}"`,
|
||||
destructive: true,
|
||||
});
|
||||
db.run(`DROP TABLE IF EXISTS ${q(tableName)}`);
|
||||
removeDBManagerTable({ db, table_name: tableName });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user