feat: canonical SQL DDL + schema validator + migration tool
- schema/canonical.sql: 29 tables across 3 databases, CHECK constraints, foreign keys, 13 indexes, WAL mode, schema_version tracking - tools/validate-schema.ts: applies DDL to in-memory SQLite, extracts PRAGMA table_info + sqlite_master metadata as JSON - tools/migrate-db.ts: CLI for Tauri→Electrobun data migration with atomic transaction, version fencing, INSERT OR IGNORE - docs/SWITCHING.md: migration guide with prerequisites and troubleshooting
This commit is contained in:
parent
0f75cb8e32
commit
631fc2efc8
4 changed files with 718 additions and 0 deletions
109
tools/validate-schema.ts
Normal file
109
tools/validate-schema.ts
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
#!/usr/bin/env bun
|
||||
/**
|
||||
* validate-schema.ts — Apply canonical.sql to an in-memory SQLite database
|
||||
* and extract structural metadata for CI comparison.
|
||||
*
|
||||
* Usage: bun tools/validate-schema.ts
|
||||
* Output: JSON to stdout with tables, columns, indexes, and schema version.
|
||||
*/
|
||||
|
||||
import { Database } from "bun:sqlite";
|
||||
import { readFileSync } from "fs";
|
||||
import { join } from "path";
|
||||
|
||||
// ── Load canonical DDL ────────────────────────────────────────────────────────
|
||||
|
||||
const schemaPath = join(import.meta.dir, "..", "schema", "canonical.sql");
|
||||
let ddl: string;
|
||||
try {
|
||||
ddl = readFileSync(schemaPath, "utf-8");
|
||||
} catch (err) {
|
||||
console.error(`Failed to read ${schemaPath}: ${err}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// ── Apply to in-memory DB ─────────────────────────────────────────────────────
|
||||
|
||||
const db = new Database(":memory:");
|
||||
|
||||
try {
|
||||
db.exec(ddl);
|
||||
} catch (err) {
|
||||
console.error(`Schema application failed: ${err}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// ── Extract metadata ──────────────────────────────────────────────────────────
|
||||
|
||||
interface ColumnInfo {
|
||||
cid: number;
|
||||
name: string;
|
||||
type: string;
|
||||
notnull: number;
|
||||
dflt_value: string | null;
|
||||
pk: number;
|
||||
}
|
||||
|
||||
interface TableMeta {
|
||||
name: string;
|
||||
type: string; // 'table' | 'virtual'
|
||||
columns: ColumnInfo[];
|
||||
indexes: string[];
|
||||
}
|
||||
|
||||
// Get all tables and virtual tables from sqlite_master
|
||||
const masterRows = db
|
||||
.prepare(
|
||||
`SELECT name, type FROM sqlite_master
|
||||
WHERE type IN ('table', 'virtual table')
|
||||
AND name NOT LIKE 'sqlite_%'
|
||||
AND name NOT LIKE '%_content'
|
||||
AND name NOT LIKE '%_data'
|
||||
AND name NOT LIKE '%_idx'
|
||||
AND name NOT LIKE '%_config'
|
||||
AND name NOT LIKE '%_docsize'
|
||||
ORDER BY name`,
|
||||
)
|
||||
.all() as Array<{ name: string; type: string }>;
|
||||
|
||||
const tables: TableMeta[] = [];
|
||||
|
||||
for (const { name, type } of masterRows) {
|
||||
// Get column info (not available for FTS5 virtual tables)
|
||||
let columns: ColumnInfo[] = [];
|
||||
try {
|
||||
columns = db
|
||||
.prepare(`PRAGMA table_info('${name}')`)
|
||||
.all() as ColumnInfo[];
|
||||
} catch {
|
||||
// FTS5 tables don't support table_info
|
||||
}
|
||||
|
||||
// Get indexes for this table
|
||||
const indexRows = db
|
||||
.prepare(
|
||||
`SELECT name FROM sqlite_master
|
||||
WHERE type = 'index' AND tbl_name = ?
|
||||
ORDER BY name`,
|
||||
)
|
||||
.all(name) as Array<{ name: string }>;
|
||||
|
||||
tables.push({
|
||||
name,
|
||||
type: type === "table" ? "table" : "virtual",
|
||||
columns,
|
||||
indexes: indexRows.map((r) => r.name),
|
||||
});
|
||||
}
|
||||
|
||||
// ── Output ────────────────────────────────────────────────────────────────────
|
||||
|
||||
const output = {
|
||||
schemaFile: "schema/canonical.sql",
|
||||
version: 1,
|
||||
tableCount: tables.length,
|
||||
tables,
|
||||
};
|
||||
|
||||
console.log(JSON.stringify(output, null, 2));
|
||||
db.close();
|
||||
Loading…
Add table
Add a link
Reference in a new issue