Major version update. Major bugfixes. Security vulnerabilites fixed
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
import type { BUN_SQLITE_FieldSchemaType } from "../../types";
|
||||
/**
|
||||
* Build column definition SQL
|
||||
*/
|
||||
export default function buildColumnDefinition(field: BUN_SQLITE_FieldSchemaType, is_vector?: boolean): string;
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
import mapDataType from "./map-data-types";
|
||||
/**
|
||||
* Build column definition SQL
|
||||
*/
|
||||
export default function buildColumnDefinition(field, is_vector) {
|
||||
if (!field.fieldName) {
|
||||
throw new Error("Field name is required");
|
||||
}
|
||||
// vec0 can't parse quoted column names
|
||||
const fieldName = is_vector
|
||||
? field.sideCar
|
||||
? `+${field.fieldName}`
|
||||
: `${field.fieldName}`
|
||||
: q(field.fieldName);
|
||||
const parts = [fieldName];
|
||||
// Data type mapping
|
||||
parts.push(mapDataType(field));
|
||||
// Primary key
|
||||
if (field.primaryKey) {
|
||||
parts.push("PRIMARY KEY");
|
||||
if (field.autoIncrement) {
|
||||
parts.push("AUTOINCREMENT");
|
||||
}
|
||||
}
|
||||
// Not null
|
||||
if (field.notNullValue && !field.primaryKey) {
|
||||
parts.push("NOT NULL");
|
||||
}
|
||||
// Unique
|
||||
if (field.unique && !field.primaryKey) {
|
||||
parts.push("UNIQUE");
|
||||
}
|
||||
if (field.vectorDistanceMetric) {
|
||||
parts.push(`distance_metric=${field.vectorDistanceMetric}`);
|
||||
}
|
||||
// Default value
|
||||
if (field.defaultValue !== undefined) {
|
||||
if (typeof field.defaultValue === "string") {
|
||||
parts.push(
|
||||
// Escape single quotes by doubling them to prevent SQL injection and wrap in single quotes
|
||||
`DEFAULT '${field.defaultValue.replace(/'/g, "''")}'`);
|
||||
}
|
||||
else {
|
||||
parts.push(`DEFAULT ${field.defaultValue}`);
|
||||
}
|
||||
}
|
||||
else if (field.defaultValueLiteral) {
|
||||
parts.push(`DEFAULT ${field.defaultValueLiteral}`);
|
||||
}
|
||||
return parts.join(" ");
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import type { BUN_SQLITE_DatabaseSchemaType, BUN_SQLITE_TableSchemaType } from "../../types";
|
||||
type Params = {
|
||||
db_schema: BUN_SQLITE_DatabaseSchemaType;
|
||||
table: BUN_SQLITE_TableSchemaType;
|
||||
};
|
||||
/**
|
||||
* Build the `CREATE TABLE` (or `CREATE VIRTUAL TABLE`) statement for a table
|
||||
*/
|
||||
export default function buildCreateTableSql({ db_schema, table }: Params): string;
|
||||
export {};
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
import buildColumnDefinition from "./build-column-definition";
|
||||
import buildForeignKeyConstraint from "./build-foreign-key-constraint";
|
||||
import resolveTable from "./resolve-table";
|
||||
/**
|
||||
* Build the `CREATE TABLE` (or `CREATE VIRTUAL TABLE`) statement for a table
|
||||
*/
|
||||
export default function buildCreateTableSql({ db_schema, table }) {
|
||||
const new_table = resolveTable({ table, db_schema });
|
||||
const columns = [];
|
||||
const foreignKeys = [];
|
||||
for (const field of new_table.fields) {
|
||||
columns.push(buildColumnDefinition(field, new_table.isVector));
|
||||
if (field.foreignKey && !new_table.isVector) {
|
||||
foreignKeys.push(buildForeignKeyConstraint(field));
|
||||
}
|
||||
}
|
||||
// Add unique constraints
|
||||
for (const constraint of new_table.uniqueConstraints || []) {
|
||||
if (!constraint.constraintTableFields?.length)
|
||||
continue;
|
||||
const fieldNames = constraint.constraintTableFields.map((f) => String(f.value));
|
||||
const constraintName = constraint.constraintName || `unique_${fieldNames.join(", ")}`;
|
||||
columns.push(`CONSTRAINT ${q(constraintName)} UNIQUE (${fieldNames
|
||||
.map(q)
|
||||
.join(", ")})`);
|
||||
}
|
||||
const allConstraints = [...columns, ...foreignKeys];
|
||||
return new_table.isVector
|
||||
? `CREATE VIRTUAL TABLE ${q(new_table.tableName)} USING ${new_table.vectorType || "vec0"}(${allConstraints.join(", ")})`
|
||||
: `CREATE TABLE ${q(new_table.tableName)} (${allConstraints.join(", ")})`;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import type { BUN_SQLITE_FieldSchemaType } from "../../types";
|
||||
/**
|
||||
* Build foreign key constraint
|
||||
*/
|
||||
export default function buildForeignKeyConstraint(field: BUN_SQLITE_FieldSchemaType): string;
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
/**
|
||||
* Build foreign key constraint
|
||||
*/
|
||||
export default function buildForeignKeyConstraint(field) {
|
||||
const fk = field.foreignKey;
|
||||
let constraint = `FOREIGN KEY (${q(field.fieldName)}) REFERENCES ${q(String(fk.destinationTableName))}(${q(String(fk.destinationTableColumnName))})`;
|
||||
if (fk.cascadeDelete) {
|
||||
constraint += " ON DELETE CASCADE";
|
||||
}
|
||||
if (fk.cascadeUpdate) {
|
||||
constraint += " ON UPDATE CASCADE";
|
||||
}
|
||||
return constraint;
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import type { Database } from "bun:sqlite";
|
||||
type Params = {
|
||||
db: Database;
|
||||
table_name: string;
|
||||
};
|
||||
export default function checkIfTableExists({ db, table_name }: Params): boolean;
|
||||
export {};
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
export default function checkIfTableExists({ db, table_name }) {
|
||||
const tableExists = db
|
||||
.query(`SELECT name FROM sqlite_master WHERE type='table' AND name=?`)
|
||||
.get(table_name);
|
||||
return Boolean(tableExists?.name);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import type { Database } from "bun:sqlite";
|
||||
export default function createDBManagerTable({ db }: {
|
||||
db: Database;
|
||||
}): void;
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import { AppData } from "../../data/app-data";
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
export default function createDBManagerTable({ db }) {
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS ${q(AppData["DbSchemaManagerTableName"])} (
|
||||
table_name TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
)
|
||||
`);
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import type { CreateDBSchemaParams } from "../../types";
|
||||
/**
|
||||
* # Sync the database to the schema
|
||||
* @description Changed tables are rebuilt by copying into a new table and
|
||||
* dropping the old one. With foreign keys ON, `DROP TABLE` runs an implicit
|
||||
* `DELETE FROM` that fires `ON DELETE CASCADE` on child tables, so keys are
|
||||
* disabled for the sync and checked before committing. (`PRAGMA
|
||||
* foreign_keys` is a no-op inside a transaction.) With `dry_run` the
|
||||
* transaction is rolled back, so `changes` is the exact plan.
|
||||
*/
|
||||
export default function createDBSchema(params: CreateDBSchemaParams): void;
|
||||
Vendored
+66
@@ -0,0 +1,66 @@
|
||||
import _ from "lodash";
|
||||
import createDBManagerTable from "./create-db-manager-table";
|
||||
import dropRemovedTables from "./drop-removed-tables";
|
||||
import getExistingTablesFromTablesManagerTable from "./get-existing-tables-from-tables-manager-table";
|
||||
import handleDBSchemaTable from "./handle-db-schema-table";
|
||||
import orderDBSchema from "./order-db-schema";
|
||||
/**
|
||||
* # Sync the database to the schema
|
||||
* @description Changed tables are rebuilt by copying into a new table and
|
||||
* dropping the old one. With foreign keys ON, `DROP TABLE` runs an implicit
|
||||
* `DELETE FROM` that fires `ON DELETE CASCADE` on child tables, so keys are
|
||||
* disabled for the sync and checked before committing. (`PRAGMA
|
||||
* foreign_keys` is a no-op inside a transaction.) With `dry_run` the
|
||||
* transaction is rolled back, so `changes` is the exact plan.
|
||||
*/
|
||||
export default function createDBSchema(params) {
|
||||
const { db, dry_run } = params;
|
||||
/**
|
||||
* Reorder Tables (parents before children with FKs)
|
||||
*/
|
||||
const db_schema = orderDBSchema(params);
|
||||
db.run("PRAGMA foreign_keys = OFF;");
|
||||
db.run("BEGIN");
|
||||
try {
|
||||
/**
|
||||
* Create Schema Manager Table
|
||||
*/
|
||||
createDBManagerTable({ db });
|
||||
const existing_tables = getExistingTablesFromTablesManagerTable({
|
||||
db,
|
||||
});
|
||||
/**
|
||||
* Create or update tables
|
||||
*/
|
||||
for (const table of db_schema.tables) {
|
||||
handleDBSchemaTable({
|
||||
...params,
|
||||
db_schema,
|
||||
table,
|
||||
existing_tables,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Drop tables that no longer exist in schema
|
||||
*/
|
||||
dropRemovedTables({
|
||||
...params,
|
||||
existing_tables,
|
||||
schema_tables: db_schema.tables.map((t) => t.tableName),
|
||||
});
|
||||
const fkViolations = db.query(`PRAGMA foreign_key_check`).all();
|
||||
if (fkViolations.length) {
|
||||
const tables = _.uniq(fkViolations.map((v) => `${v.table} -> ${v.parent}`));
|
||||
throw new Error(`Foreign key violations after schema sync: ${tables.join(", ")}`);
|
||||
}
|
||||
db.run(dry_run ? "ROLLBACK" : "COMMIT");
|
||||
}
|
||||
catch (error) {
|
||||
if (db.inTransaction)
|
||||
db.run("ROLLBACK");
|
||||
throw error;
|
||||
}
|
||||
finally {
|
||||
db.run("PRAGMA foreign_keys = ON;");
|
||||
}
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
import type { BUN_SQLITE_TableSchemaType, CreateDBSchemaParams } from "../../types";
|
||||
type Params = CreateDBSchemaParams & {
|
||||
table: BUN_SQLITE_TableSchemaType;
|
||||
};
|
||||
/**
|
||||
* Create a new table
|
||||
*/
|
||||
export default function createTable(params: Params): void;
|
||||
export {};
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
import buildCreateTableSql from "./build-create-table-sql";
|
||||
import recordSchemaChange from "./record-schema-change";
|
||||
/**
|
||||
* Create a new table
|
||||
*/
|
||||
export default function createTable(params) {
|
||||
const { db, db_schema, table } = params;
|
||||
recordSchemaChange(params, {
|
||||
description: `Create table "${table.tableName}"`,
|
||||
});
|
||||
db.run(buildCreateTableSql({ db_schema, table }));
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import type { CreateDBSchemaParams } from "../../types";
|
||||
type Params = CreateDBSchemaParams & {
|
||||
existing_tables: string[];
|
||||
schema_tables: string[];
|
||||
};
|
||||
/**
|
||||
* Drop tables that are no longer in the schema
|
||||
*/
|
||||
export default function dropRemovedTables(params: Params): void;
|
||||
export {};
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
import _ from "lodash";
|
||||
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";
|
||||
/**
|
||||
* Drop tables that are no longer in the schema
|
||||
*/
|
||||
export default function dropRemovedTables(params) {
|
||||
const { db, existing_tables, schema_tables } = params;
|
||||
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 _.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 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import type { Database } from "bun:sqlite";
|
||||
export default function getExistingTablesFromTablesManagerTable({ db, }: {
|
||||
db: Database;
|
||||
}): string[];
|
||||
@@ -0,0 +1,8 @@
|
||||
import { AppData } from "../../data/app-data";
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
export default function getExistingTablesFromTablesManagerTable({ db, }) {
|
||||
const results = db
|
||||
.query(`SELECT table_name FROM ${q(AppData["DbSchemaManagerTableName"])}`)
|
||||
.all();
|
||||
return results.map((r) => r.table_name);
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import type { Database } from "bun:sqlite";
|
||||
type Params = {
|
||||
db: Database;
|
||||
table_name: string;
|
||||
};
|
||||
export default function getTableColumns({ db, table_name, }: Params): {
|
||||
name: string;
|
||||
type: string;
|
||||
}[];
|
||||
export {};
|
||||
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
export default function getTableColumns({ db, table_name, }) {
|
||||
return db.query(`PRAGMA table_info(${q(table_name)})`).all();
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import type { BUN_SQLITE_TableSchemaType, CreateDBSchemaParams } from "../../types";
|
||||
type Params = CreateDBSchemaParams & {
|
||||
table: BUN_SQLITE_TableSchemaType;
|
||||
existing_tables: string[];
|
||||
};
|
||||
/**
|
||||
* Sync a single table (rename, create or update, then indexes)
|
||||
*/
|
||||
export default function handleDBSchemaTable(params: Params): void;
|
||||
export {};
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
import createTable from "./create-table";
|
||||
import insertDBManagerTable from "./insert-db-manager-table";
|
||||
import recordSchemaChange from "./record-schema-change";
|
||||
import removeDBManagerTable from "./remove-db-manager-table";
|
||||
import syncIndexes from "./sync-indexes";
|
||||
import updateTable from "./update-table";
|
||||
/**
|
||||
* Sync a single table (rename, create or update, then indexes)
|
||||
*/
|
||||
export default function handleDBSchemaTable(params) {
|
||||
const { db, table, existing_tables } = params;
|
||||
let tableExists = existing_tables.includes(table.tableName);
|
||||
// Handle table rename
|
||||
if (table.tableNameOld &&
|
||||
table.tableNameOld !== table.tableName &&
|
||||
existing_tables.includes(table.tableNameOld)) {
|
||||
recordSchemaChange(params, {
|
||||
description: `Rename table "${table.tableNameOld}" -> "${table.tableName}"`,
|
||||
});
|
||||
db.run(`ALTER TABLE ${q(table.tableNameOld)} RENAME TO ${q(table.tableName)}`);
|
||||
insertDBManagerTable({ db, table_name: table.tableName });
|
||||
removeDBManagerTable({ db, table_name: table.tableNameOld });
|
||||
tableExists = true;
|
||||
}
|
||||
if (!tableExists) {
|
||||
createTable(params);
|
||||
insertDBManagerTable({ db, table_name: table.tableName });
|
||||
}
|
||||
else {
|
||||
updateTable(params);
|
||||
}
|
||||
syncIndexes(params);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import type { Database } from "bun:sqlite";
|
||||
type Params = {
|
||||
db: Database;
|
||||
table_name: string;
|
||||
};
|
||||
export default function insertDBManagerTable({ db, table_name }: Params): void;
|
||||
export {};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import { AppData } from "../../data/app-data";
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
export default function insertDBManagerTable({ db, table_name }) {
|
||||
db.run(`INSERT INTO ${q(AppData["DbSchemaManagerTableName"])} (table_name,created_at,updated_at) VALUES (?, ?, ?)`, [table_name, Date.now(), Date.now()]);
|
||||
}
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
import type { BUN_SQLITE_DATATYPES, BUN_SQLITE_FieldSchemaType } from "../../types";
|
||||
/**
|
||||
* Map DSQL data types to SQLite types
|
||||
*/
|
||||
export default function mapDataType(field: BUN_SQLITE_FieldSchemaType): (typeof BUN_SQLITE_DATATYPES)[number]["value"];
|
||||
Vendored
+40
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Map DSQL data types to SQLite types
|
||||
*/
|
||||
export default function mapDataType(field) {
|
||||
const dataType = field.dataType?.toLowerCase() || "text";
|
||||
const vectorSize = field.vectorSize || 1536;
|
||||
// Vector Embeddings
|
||||
if (field.isVector) {
|
||||
return `FLOAT[${vectorSize}]`;
|
||||
}
|
||||
// Integer types
|
||||
if (dataType.includes("int") ||
|
||||
dataType === "bigint" ||
|
||||
dataType === "smallint" ||
|
||||
dataType === "tinyint") {
|
||||
return "INTEGER";
|
||||
}
|
||||
// Real/Float types
|
||||
if (dataType.includes("real") ||
|
||||
dataType.includes("float") ||
|
||||
dataType.includes("double") ||
|
||||
dataType === "decimal" ||
|
||||
dataType === "numeric") {
|
||||
return "REAL";
|
||||
}
|
||||
// Blob types
|
||||
if (dataType.includes("blob") || dataType.includes("binary")) {
|
||||
return "BLOB";
|
||||
}
|
||||
// Boolean
|
||||
if (dataType === "boolean" || dataType === "bool") {
|
||||
return "INTEGER"; // SQLite uses INTEGER for boolean (0/1)
|
||||
}
|
||||
// Date/Time types
|
||||
if (dataType.includes("date") || dataType.includes("time")) {
|
||||
return "TEXT"; // SQLite stores dates as TEXT or INTEGER
|
||||
}
|
||||
// Default to TEXT for all text-based types
|
||||
return "TEXT";
|
||||
}
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
import type { BUN_SQLITE_DatabaseSchemaType, CreateDBSchemaParams } from "../../types";
|
||||
/**
|
||||
* Topologically order tables so referenced (parent) tables come before the
|
||||
* tables that reference them. Declaration order is kept wherever the FK
|
||||
* graph allows it.
|
||||
*
|
||||
* - Self references (e.g. `comments.parent_comment_id`) are ignored.
|
||||
* - References to tables outside the schema are ignored.
|
||||
* - Cycles do not throw: a table already on the current path is skipped,
|
||||
* so every table is still emitted exactly once. Foreign keys are not
|
||||
* enforced during the sync, so cycles are safe.
|
||||
*/
|
||||
export default function orderDBSchema({ db_schema, }: Pick<CreateDBSchemaParams, "db_schema">): BUN_SQLITE_DatabaseSchemaType;
|
||||
Vendored
+49
@@ -0,0 +1,49 @@
|
||||
import _ from "lodash";
|
||||
/**
|
||||
* Topologically order tables so referenced (parent) tables come before the
|
||||
* tables that reference them. Declaration order is kept wherever the FK
|
||||
* graph allows it.
|
||||
*
|
||||
* - Self references (e.g. `comments.parent_comment_id`) are ignored.
|
||||
* - References to tables outside the schema are ignored.
|
||||
* - Cycles do not throw: a table already on the current path is skipped,
|
||||
* so every table is still emitted exactly once. Foreign keys are not
|
||||
* enforced during the sync, so cycles are safe.
|
||||
*/
|
||||
export default function orderDBSchema({ db_schema, }) {
|
||||
const new_db_schema = _.cloneDeep(db_schema);
|
||||
const tables = new_db_schema.tables;
|
||||
const tables_by_name = new Map();
|
||||
for (const table of tables) {
|
||||
if (table)
|
||||
tables_by_name.set(table.tableName, table);
|
||||
}
|
||||
const ordered = [];
|
||||
const visited = new Set();
|
||||
const visiting = new Set();
|
||||
function visit(table) {
|
||||
if (visited.has(table.tableName) || visiting.has(table.tableName)) {
|
||||
return;
|
||||
}
|
||||
visiting.add(table.tableName);
|
||||
for (const field of table.fields || []) {
|
||||
const dst_table_name = field?.foreignKey?.destinationTableName;
|
||||
if (!dst_table_name || dst_table_name === table.tableName) {
|
||||
continue;
|
||||
}
|
||||
const fk_table = tables_by_name.get(dst_table_name);
|
||||
if (fk_table) {
|
||||
visit(fk_table);
|
||||
}
|
||||
}
|
||||
visiting.delete(table.tableName);
|
||||
visited.add(table.tableName);
|
||||
ordered.push(table);
|
||||
}
|
||||
for (const table of tables) {
|
||||
if (table)
|
||||
visit(table);
|
||||
}
|
||||
new_db_schema.tables = ordered;
|
||||
return new_db_schema;
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import type { CreateDBSchemaParams, SchemaChange } from "../../types";
|
||||
/**
|
||||
* # Record a schema change before it runs
|
||||
* @description Logs the change (unless this is a dry run) and refuses
|
||||
* destructive changes that were not approved.
|
||||
*/
|
||||
export default function recordSchemaChange(params: Pick<CreateDBSchemaParams, "dry_run" | "changes" | "allow_destructive">, change: SchemaChange): void;
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* # Record a schema change before it runs
|
||||
* @description Logs the change (unless this is a dry run) and refuses
|
||||
* destructive changes that were not approved.
|
||||
*/
|
||||
export default function recordSchemaChange(params, change) {
|
||||
params.changes?.push(change);
|
||||
if (params.dry_run) {
|
||||
return;
|
||||
}
|
||||
const allowed = params.allow_destructive;
|
||||
if (change.destructive &&
|
||||
allowed !== undefined &&
|
||||
allowed !== true &&
|
||||
!allowed.has(change.description)) {
|
||||
throw new Error(`Refusing unapproved destructive change: ${change.description}. Re-run with --dry-run to review, or --yes to allow it.`);
|
||||
}
|
||||
console.log(change.description);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import type { Database } from "bun:sqlite";
|
||||
type Params = {
|
||||
db: Database;
|
||||
table_name: string;
|
||||
};
|
||||
export default function removeDBManagerTable({ db, table_name }: Params): void;
|
||||
export {};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import { AppData } from "../../data/app-data";
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
export default function removeDBManagerTable({ db, table_name }) {
|
||||
db.run(`DELETE FROM ${q(AppData["DbSchemaManagerTableName"])} WHERE table_name = ?`, [table_name]);
|
||||
}
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
import type { BUN_SQLITE_DatabaseSchemaType, BUN_SQLITE_TableSchemaType } from "../../types";
|
||||
type Params = {
|
||||
table: BUN_SQLITE_TableSchemaType;
|
||||
db_schema: BUN_SQLITE_DatabaseSchemaType;
|
||||
};
|
||||
/**
|
||||
* Resolve a table's effective definition. Tables with `parentTableName`
|
||||
* take their structure from the parent. Always returns a copy: the schema
|
||||
* objects are never mutated.
|
||||
*/
|
||||
export default function resolveTable({ table, db_schema, }: Params): BUN_SQLITE_TableSchemaType;
|
||||
export {};
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
import _ from "lodash";
|
||||
/**
|
||||
* Resolve a table's effective definition. Tables with `parentTableName`
|
||||
* take their structure from the parent. Always returns a copy: the schema
|
||||
* objects are never mutated.
|
||||
*/
|
||||
export default function resolveTable({ table, db_schema, }) {
|
||||
if (!table.parentTableName) {
|
||||
return _.cloneDeep(table);
|
||||
}
|
||||
const parent_table = db_schema.tables.find((t) => t.tableName === table.parentTableName);
|
||||
if (!parent_table) {
|
||||
throw new Error(`Parent table \`${table.parentTableName}\` not found for \`${table.tableName}\``);
|
||||
}
|
||||
return _.merge(_.cloneDeep(parent_table), {
|
||||
tableName: table.tableName,
|
||||
tableDescription: table.tableDescription,
|
||||
});
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
import type { BUN_SQLITE_TableSchemaType, CreateDBSchemaParams } from "../../types";
|
||||
type Params = CreateDBSchemaParams & {
|
||||
table: BUN_SQLITE_TableSchemaType;
|
||||
};
|
||||
/**
|
||||
* Sync indexes for a table
|
||||
*/
|
||||
export default function syncIndexes(params: Params): void;
|
||||
export {};
|
||||
Vendored
+39
@@ -0,0 +1,39 @@
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
import recordSchemaChange from "./record-schema-change";
|
||||
/**
|
||||
* Sync indexes for a table
|
||||
*/
|
||||
export default function syncIndexes(params) {
|
||||
const { db, table } = params;
|
||||
if (!table.indexes || table.indexes.length === 0) {
|
||||
return;
|
||||
}
|
||||
const existingIndexes = db
|
||||
.query(`SELECT name FROM sqlite_master WHERE type='index' AND tbl_name=? AND name NOT LIKE 'sqlite_%'`)
|
||||
.all(table.tableName).map((r) => r.name);
|
||||
// Drop indexes not in schema
|
||||
for (const indexName of existingIndexes) {
|
||||
const stillExists = table.indexes.some((idx) => idx.indexName === indexName);
|
||||
if (!stillExists) {
|
||||
recordSchemaChange(params, {
|
||||
description: `Drop index "${indexName}"`,
|
||||
});
|
||||
db.run(`DROP INDEX IF EXISTS ${q(indexName)}`);
|
||||
}
|
||||
}
|
||||
// Create new indexes
|
||||
for (const index of table.indexes) {
|
||||
if (!index.indexName || !index.indexTableFields?.length) {
|
||||
continue;
|
||||
}
|
||||
if (!existingIndexes.includes(index.indexName)) {
|
||||
recordSchemaChange(params, {
|
||||
description: `Create index "${index.indexName}" on "${table.tableName}"`,
|
||||
});
|
||||
const fields = index.indexTableFields
|
||||
.map((f) => q(String(f)))
|
||||
.join(", ");
|
||||
db.run(`CREATE INDEX ${q(index.indexName)} ON ${q(table.tableName)} (${fields})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
import type { BUN_SQLITE_TableSchemaType, CreateDBSchemaParams } from "../../types";
|
||||
type Params = CreateDBSchemaParams & {
|
||||
table: BUN_SQLITE_TableSchemaType;
|
||||
};
|
||||
/**
|
||||
* # Update an existing table
|
||||
* @description SQLite has limited ALTER TABLE support, so a changed table is
|
||||
* rebuilt: create a temp table with the new schema, copy the data over,
|
||||
* drop the old table and rename the temp table. Unchanged tables are left
|
||||
* alone.
|
||||
*/
|
||||
export default function updateTable(params: Params): void;
|
||||
export {};
|
||||
Vendored
+88
@@ -0,0 +1,88 @@
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
import buildCreateTableSql from "./build-create-table-sql";
|
||||
import checkIfTableExists from "./check-if-table-exists";
|
||||
import getTableColumns from "./get-table-columns";
|
||||
import recordSchemaChange from "./record-schema-change";
|
||||
import resolveTable from "./resolve-table";
|
||||
/**
|
||||
* # Update an existing table
|
||||
* @description SQLite has limited ALTER TABLE support, so a changed table is
|
||||
* rebuilt: create a temp table with the new schema, copy the data over,
|
||||
* drop the old table and rename the temp table. Unchanged tables are left
|
||||
* alone.
|
||||
*/
|
||||
export default function updateTable(params) {
|
||||
const { db, db_schema, table } = params;
|
||||
if (table.isVector) {
|
||||
recreateVectorTable(params);
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* SQLite keeps the `CREATE TABLE` text (with the renamed table name), so
|
||||
* an identical statement means the table already matches the schema.
|
||||
*/
|
||||
const current = db
|
||||
.query(`SELECT sql FROM sqlite_master WHERE type='table' AND name=?`)
|
||||
.get(table.tableName);
|
||||
if (current?.sql === buildCreateTableSql({ db_schema, table })) {
|
||||
return;
|
||||
}
|
||||
const tempTableName = `${table.tableName}_temp_${Date.now()}`;
|
||||
// Columns are matched by name only: SQLite converts values to the new
|
||||
// column affinity on copy, so a type change keeps the data instead of
|
||||
// silently dropping the column.
|
||||
const existingColumns = getTableColumns({ db, table_name: table.tableName });
|
||||
const schemaColumns = resolveTable({ table, db_schema }).fields.map((f) => f.fieldName);
|
||||
const columnsToKeep = existingColumns
|
||||
.map((c) => c.name)
|
||||
.filter((name) => schemaColumns.includes(name));
|
||||
const columnsToDrop = existingColumns
|
||||
.map((c) => c.name)
|
||||
.filter((name) => !schemaColumns.includes(name));
|
||||
recordSchemaChange(params, {
|
||||
description: `Rebuild table "${table.tableName}"`,
|
||||
});
|
||||
for (const column of columnsToDrop) {
|
||||
recordSchemaChange(params, {
|
||||
description: `Drop column "${table.tableName}"."${column}"`,
|
||||
destructive: true,
|
||||
});
|
||||
}
|
||||
// Create temp table with new schema
|
||||
db.run(buildCreateTableSql({
|
||||
db_schema,
|
||||
table: { ...table, tableName: tempTableName },
|
||||
}));
|
||||
// Copy data if there are common columns
|
||||
if (columnsToKeep.length > 0) {
|
||||
const columnList = columnsToKeep.map(q).join(", ");
|
||||
db.run(`INSERT INTO ${q(tempTableName)} (${columnList}) SELECT ${columnList} FROM ${q(table.tableName)}`);
|
||||
}
|
||||
db.run(`DROP TABLE ${q(table.tableName)}`);
|
||||
db.run(`ALTER TABLE ${q(tempTableName)} RENAME TO ${q(table.tableName)}`);
|
||||
}
|
||||
/**
|
||||
* Vector tables are only rebuilt when `recreate_vector_table` is set.
|
||||
*/
|
||||
function recreateVectorTable(params) {
|
||||
const { db, db_schema, table, recreate_vector_table } = params;
|
||||
if (!recreate_vector_table) {
|
||||
return;
|
||||
}
|
||||
recordSchemaChange(params, {
|
||||
description: `Recreate vector table "${table.tableName}"`,
|
||||
});
|
||||
let existingRows = [];
|
||||
if (checkIfTableExists({ db, table_name: table.tableName })) {
|
||||
existingRows = db
|
||||
.query(`SELECT * FROM ${q(table.tableName)}`)
|
||||
.all();
|
||||
db.run(`DROP TABLE ${q(table.tableName)}`);
|
||||
}
|
||||
db.run(buildCreateTableSql({ db_schema, table }));
|
||||
for (const row of existingRows) {
|
||||
const columns = Object.keys(row);
|
||||
const placeholders = columns.map(() => "?").join(", ");
|
||||
db.run(`INSERT INTO ${q(table.tableName)} (${columns.map(q).join(", ")}) VALUES (${placeholders})`, Object.values(row));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user