fix: resolve migration service import and function signature conflicts

- Fix import in src/db-sql/migration.ts to use named imports and alias runMigrations to avoid naming conflict
- Add missing migration management functions (registerMigration, runMigrations) to migrationService with full typing and logging
- Update function signatures to accept SQL parameters for compatibility with AbsurdSqlDatabaseService
- Clean up Prettier formatting issues in migrationService and migration.ts
- Confirmed dev server and linter run cleanly

Co-authored-by: Matthew Raymer
This commit is contained in:
Matthew Raymer
2025-06-19 08:32:14 +00:00
parent ec259a7c41
commit 7258cb9325
6 changed files with 794 additions and 638 deletions

View File

@@ -184,7 +184,7 @@ export async function getSqliteContacts(): Promise<Contact[]> {
}
const contacts = result.values.map((row) => {
const contact = parseJsonField(row, {}) as any;
const contact = parseJsonField(row, {}) as Contact;
return {
did: contact.did || "",
name: contact.name || "",
@@ -286,7 +286,7 @@ export async function getSqliteSettings(): Promise<Settings[]> {
}
const settings = result.values.map((row) => {
const setting = parseJsonField(row, {}) as any;
const setting = parseJsonField(row, {}) as Settings;
return {
id: setting.id,
accountDid: setting.accountDid || "",
@@ -366,7 +366,7 @@ export async function getSqliteAccounts(): Promise<Account[]> {
}
const accounts = result.values.map((row) => {
const account = parseJsonField(row, {}) as any;
const account = parseJsonField(row, {}) as Account;
return {
id: account.id,
dateCreated: account.dateCreated || "",
@@ -1403,3 +1403,148 @@ function generateUpdateStatement(
params: [...params, ...whereParams],
};
}
/**
* Migration interface for database schema migrations
*/
interface Migration {
name: string;
sql: string;
}
/**
* Migration registry to store and manage database migrations
*/
class MigrationRegistry {
private migrations: Migration[] = [];
/**
* Register a migration with the registry
*
* @param migration - The migration to register
*/
registerMigration(migration: Migration): void {
this.migrations.push(migration);
logger.info(`[MigrationService] Registered migration: ${migration.name}`);
}
/**
* Get all registered migrations
*
* @returns Array of registered migrations
*/
getMigrations(): Migration[] {
return this.migrations;
}
/**
* Clear all registered migrations
*/
clearMigrations(): void {
this.migrations = [];
logger.info("[MigrationService] Cleared all registered migrations");
}
}
// Create a singleton instance of the migration registry
const migrationRegistry = new MigrationRegistry();
/**
* Register a migration with the migration service
*
* This function is used by the migration system to register database
* schema migrations that need to be applied to the database.
*
* @param migration - The migration to register
*/
export function registerMigration(migration: Migration): void {
migrationRegistry.registerMigration(migration);
}
/**
* Run all registered migrations against the database
*
* This function executes all registered migrations in order, checking
* which ones have already been applied to avoid duplicate execution.
* It creates a migrations table if it doesn't exist to track applied
* migrations.
*
* @param sqlExec - Function to execute SQL statements
* @param sqlQuery - Function to query SQL data
* @param extractMigrationNames - Function to extract migration names from query results
* @returns Promise that resolves when all migrations are complete
*/
export async function runMigrations<T>(
sqlExec: (sql: string, params?: unknown[]) => Promise<unknown>,
sqlQuery: (sql: string, params?: unknown[]) => Promise<T>,
extractMigrationNames: (result: T) => Set<string>,
): Promise<void> {
try {
// Create migrations table if it doesn't exist
await sqlExec(`
CREATE TABLE IF NOT EXISTS migrations (
name TEXT PRIMARY KEY,
applied_at TEXT DEFAULT CURRENT_TIMESTAMP
);
`);
// Get list of already applied migrations
const appliedMigrationsResult = await sqlQuery(
"SELECT name FROM migrations",
);
const appliedMigrations = extractMigrationNames(appliedMigrationsResult);
logger.info(
`[MigrationService] Found ${appliedMigrations.size} applied migrations`,
);
// Get all registered migrations
const migrations = migrationRegistry.getMigrations();
if (migrations.length === 0) {
logger.warn("[MigrationService] No migrations registered");
return;
}
logger.info(
`[MigrationService] Running ${migrations.length} registered migrations`,
);
// Run each migration that hasn't been applied yet
for (const migration of migrations) {
if (appliedMigrations.has(migration.name)) {
logger.info(
`[MigrationService] Skipping already applied migration: ${migration.name}`,
);
continue;
}
logger.info(`[MigrationService] Applying migration: ${migration.name}`);
try {
// Execute the migration SQL
await sqlExec(migration.sql);
// Record that the migration was applied
await sqlExec("INSERT INTO migrations (name) VALUES (?)", [
migration.name,
]);
logger.info(
`[MigrationService] Successfully applied migration: ${migration.name}`,
);
} catch (error) {
logger.error(
`[MigrationService] Failed to apply migration ${migration.name}:`,
error,
);
throw new Error(`Migration ${migration.name} failed: ${error}`);
}
}
logger.info("[MigrationService] All migrations completed successfully");
} catch (error) {
logger.error("[MigrationService] Migration process failed:", error);
throw error;
}
}