Port bun-mariadb conventions: init globals, db handler, split schema sync
- init() loads config, schema and the SQLite client once into globals - all CRUD queries go through lib/db-handler.ts (optional per-call config) - split db-schema-manager.ts into lib/schema/* with FK-aware table ordering - add bun:test tests for schema ordering, FK constraints and schema sync - config/schema files can be .ts/.js/.json/.yaml/.yml; db_schema_file_name is now optional (defaults to schema.* in db_dir) - add export/import commands (database snapshot + schema archives) - package exports map, type re-exports, hardened publish.sh - validate table name in delete Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import type { Database } from "bun:sqlite";
|
||||
import { readLiveSchema } from "../../functions/live-schema";
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
import removeDBManagerTable from "./remove-db-manager-table";
|
||||
|
||||
type Params = {
|
||||
db: Database;
|
||||
existing_tables: string[];
|
||||
schema_tables: string[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Drop tables that are no longer in the schema
|
||||
*/
|
||||
export default function dropRemovedTables({
|
||||
db,
|
||||
existing_tables,
|
||||
schema_tables,
|
||||
}: Params) {
|
||||
console.log(`Cleaning up tables ...`);
|
||||
|
||||
const tablesToDrop = existing_tables.filter(
|
||||
(t) =>
|
||||
!schema_tables.includes(t) &&
|
||||
!schema_tables.find((scT) => t.startsWith(scT + "_")),
|
||||
);
|
||||
|
||||
const currentSchema = readLiveSchema();
|
||||
|
||||
for (const table of currentSchema?.tables || []) {
|
||||
if (!table?.tableName) continue;
|
||||
|
||||
if (!schema_tables.includes(table.tableName)) {
|
||||
tablesToDrop.push(table.tableName);
|
||||
}
|
||||
}
|
||||
|
||||
for (const tableName of tablesToDrop) {
|
||||
console.log(`Dropping table: ${tableName}`);
|
||||
db.run(`DROP TABLE IF EXISTS ${q(tableName)}`);
|
||||
removeDBManagerTable({ db, table_name: tableName });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user