modified: backend/src/controllers/databaseManagementController.ts

modified:   backend/src/controllers/schemaController.ts
	modified:   frontend/src/pages/DatabaseSchema.tsx
This commit is contained in:
2026-01-28 00:00:15 +03:00
parent c780979b57
commit 4fb92470ce
3 changed files with 39 additions and 11 deletions

View File

@@ -33,11 +33,17 @@ interface SchemaData {
// Parse PostgreSQL schema
async function parsePostgresSchema(databaseId: string): Promise<SchemaData> {
const startTime = Date.now();
console.log(`[Schema] Starting schema parse for database ${databaseId}`);
const pool = databasePoolManager.getPool(databaseId);
if (!pool) {
throw new Error('Database not found or not active');
}
console.log(`[Schema] Fetching tables list...`);
const tablesStartTime = Date.now();
// Get all tables with comments via pg_catalog
const tablesResult = await pool.query(`
SELECT
@@ -52,9 +58,17 @@ async function parsePostgresSchema(databaseId: string): Promise<SchemaData> {
ORDER BY t.table_schema, t.table_name
`);
console.log(`[Schema] Found ${tablesResult.rows.length} tables in ${Date.now() - tablesStartTime}ms`);
const tables: TableInfo[] = [];
let processedCount = 0;
for (const table of tablesResult.rows) {
processedCount++;
if (processedCount % 50 === 0 || processedCount === tablesResult.rows.length) {
console.log(`[Schema] Processing table ${processedCount}/${tablesResult.rows.length}: ${table.table_schema}.${table.table_name}`);
}
// Get columns for each table
const columnsResult = await pool.query(`
SELECT
@@ -121,6 +135,9 @@ async function parsePostgresSchema(databaseId: string): Promise<SchemaData> {
});
}
const totalTime = Date.now() - startTime;
console.log(`[Schema] Completed! ${tables.length} tables, ${tables.reduce((acc, t) => acc + t.columns.length, 0)} columns, ${tables.reduce((acc, t) => acc + t.foreign_keys.length, 0)} FKs in ${totalTime}ms`);
return {
tables,
updated_at: new Date().toISOString(),
@@ -188,3 +205,15 @@ export const refreshSchema = async (req: Request, res: Response) => {
res.status(500).json({ success: false, error: error.message });
}
};
// Generate schema for a database (called from other controllers)
export const generateSchemaForDatabase = async (databaseId: string): Promise<void> => {
try {
const schema = await parsePostgresSchema(databaseId);
await saveSchemaToCache(databaseId, schema);
console.log(`Schema generated for database ${databaseId}`);
} catch (error: any) {
console.error(`Error generating schema for database ${databaseId}:`, error.message);
// Don't throw - schema generation is not critical
}
};