Init
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* Database Migrations
|
||||
*
|
||||
* Schema versioning and migration support.
|
||||
*/
|
||||
|
||||
import Database from 'better-sqlite3';
|
||||
|
||||
/**
|
||||
* Current schema version
|
||||
*/
|
||||
export const CURRENT_SCHEMA_VERSION = 1;
|
||||
|
||||
/**
|
||||
* Migration definition
|
||||
*/
|
||||
interface Migration {
|
||||
version: number;
|
||||
description: string;
|
||||
up: (db: Database.Database) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* All migrations in order
|
||||
*
|
||||
* Note: Version 1 is the initial schema, handled by schema.sql
|
||||
* Future migrations go here.
|
||||
*/
|
||||
const migrations: Migration[] = [
|
||||
// Example migration for version 2 (when needed):
|
||||
// {
|
||||
// version: 2,
|
||||
// description: 'Add support for module resolution',
|
||||
// up: (db) => {
|
||||
// db.exec(`
|
||||
// ALTER TABLE nodes ADD COLUMN module_path TEXT;
|
||||
// CREATE INDEX idx_nodes_module_path ON nodes(module_path);
|
||||
// `);
|
||||
// },
|
||||
// },
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the current schema version from the database
|
||||
*/
|
||||
export function getCurrentVersion(db: Database.Database): number {
|
||||
try {
|
||||
const row = db
|
||||
.prepare('SELECT MAX(version) as version FROM schema_versions')
|
||||
.get() as { version: number | null } | undefined;
|
||||
return row?.version ?? 0;
|
||||
} catch {
|
||||
// Table doesn't exist yet
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Record a migration as applied
|
||||
*/
|
||||
function recordMigration(db: Database.Database, version: number, description: string): void {
|
||||
db.prepare(
|
||||
'INSERT INTO schema_versions (version, applied_at, description) VALUES (?, ?, ?)'
|
||||
).run(version, Date.now(), description);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run all pending migrations
|
||||
*/
|
||||
export function runMigrations(db: Database.Database, fromVersion: number): void {
|
||||
const pending = migrations.filter((m) => m.version > fromVersion);
|
||||
|
||||
if (pending.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Sort by version
|
||||
pending.sort((a, b) => a.version - b.version);
|
||||
|
||||
// Run each migration in a transaction
|
||||
for (const migration of pending) {
|
||||
db.transaction(() => {
|
||||
migration.up(db);
|
||||
recordMigration(db, migration.version, migration.description);
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the database needs migration
|
||||
*/
|
||||
export function needsMigration(db: Database.Database): boolean {
|
||||
const current = getCurrentVersion(db);
|
||||
return current < CURRENT_SCHEMA_VERSION;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of pending migrations
|
||||
*/
|
||||
export function getPendingMigrations(db: Database.Database): Migration[] {
|
||||
const current = getCurrentVersion(db);
|
||||
return migrations
|
||||
.filter((m) => m.version > current)
|
||||
.sort((a, b) => a.version - b.version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get migration history from database
|
||||
*/
|
||||
export function getMigrationHistory(
|
||||
db: Database.Database
|
||||
): Array<{ version: number; appliedAt: number; description: string | null }> {
|
||||
const rows = db
|
||||
.prepare('SELECT version, applied_at, description FROM schema_versions ORDER BY version')
|
||||
.all() as Array<{ version: number; applied_at: number; description: string | null }>;
|
||||
|
||||
return rows.map((row) => ({
|
||||
version: row.version,
|
||||
appliedAt: row.applied_at,
|
||||
description: row.description,
|
||||
}));
|
||||
}
|
||||
Reference in New Issue
Block a user