@@ -7369,6 +7369,16 @@ class DatabaseService {
73697369 }
73707370
73717371 cleanupOldRouteSegments ( days : number = 30 ) : number {
7372+ // For PostgreSQL/MySQL, fire-and-forget async cleanup
7373+ if ( this . drizzleDbType === 'postgres' || this . drizzleDbType === 'mysql' ) {
7374+ if ( this . traceroutesRepo ) {
7375+ this . traceroutesRepo . cleanupOldRouteSegments ( days ) . catch ( err => {
7376+ logger . debug ( 'Failed to cleanup old route segments:' , err ) ;
7377+ } ) ;
7378+ }
7379+ return 0 ;
7380+ }
7381+
73727382 const cutoff = Date . now ( ) - ( days * 24 * 60 * 60 * 1000 ) ;
73737383 const stmt = this . db . prepare ( `
73747384 DELETE FROM route_segments
@@ -7382,6 +7392,16 @@ class DatabaseService {
73827392 * Delete traceroutes older than the specified number of days
73837393 */
73847394 cleanupOldTraceroutes ( days : number = 30 ) : number {
7395+ // For PostgreSQL/MySQL, fire-and-forget async cleanup
7396+ if ( this . drizzleDbType === 'postgres' || this . drizzleDbType === 'mysql' ) {
7397+ if ( this . traceroutesRepo ) {
7398+ this . traceroutesRepo . cleanupOldTraceroutes ( days * 24 ) . catch ( err => {
7399+ logger . debug ( 'Failed to cleanup old traceroutes:' , err ) ;
7400+ } ) ;
7401+ }
7402+ return 0 ;
7403+ }
7404+
73857405 const cutoff = Date . now ( ) - ( days * 24 * 60 * 60 * 1000 ) ;
73867406 const stmt = this . db . prepare ( 'DELETE FROM traceroutes WHERE timestamp < ?' ) ;
73877407 const result = stmt . run ( cutoff ) ;
@@ -7392,6 +7412,16 @@ class DatabaseService {
73927412 * Delete neighbor info records older than the specified number of days
73937413 */
73947414 cleanupOldNeighborInfo ( days : number = 30 ) : number {
7415+ // For PostgreSQL/MySQL, fire-and-forget async cleanup
7416+ if ( this . drizzleDbType === 'postgres' || this . drizzleDbType === 'mysql' ) {
7417+ if ( this . neighborsRepo ) {
7418+ this . neighborsRepo . cleanupOldNeighborInfo ( days ) . catch ( err => {
7419+ logger . debug ( 'Failed to cleanup old neighbor info:' , err ) ;
7420+ } ) ;
7421+ }
7422+ return 0 ;
7423+ }
7424+
73957425 const cutoff = Date . now ( ) - ( days * 24 * 60 * 60 * 1000 ) ;
73967426 const stmt = this . db . prepare ( 'DELETE FROM neighbor_info WHERE timestamp < ?' ) ;
73977427 const result = stmt . run ( cutoff ) ;
@@ -7403,6 +7433,23 @@ class DatabaseService {
74037433 * This can take a while on large databases and temporarily doubles disk usage
74047434 */
74057435 vacuum ( ) : void {
7436+ // For PostgreSQL/MySQL, use native vacuum/optimize
7437+ if ( this . drizzleDbType === 'postgres' ) {
7438+ logger . info ( '🧹 Running VACUUM on PostgreSQL database...' ) ;
7439+ this . postgresPool ! . query ( 'VACUUM' ) . then ( ( ) => {
7440+ logger . info ( '✅ PostgreSQL VACUUM complete' ) ;
7441+ } ) . catch ( err => {
7442+ logger . error ( 'Failed to VACUUM PostgreSQL:' , err ) ;
7443+ } ) ;
7444+ return ;
7445+ }
7446+ if ( this . drizzleDbType === 'mysql' ) {
7447+ logger . info ( '🧹 Running OPTIMIZE TABLE on MySQL database...' ) ;
7448+ // MySQL OPTIMIZE TABLE requires table names; skip for now as it's not critical
7449+ logger . info ( '✅ MySQL OPTIMIZE TABLE skipped (not critical)' ) ;
7450+ return ;
7451+ }
7452+
74067453 logger . info ( '🧹 Running VACUUM on database...' ) ;
74077454 this . db . exec ( 'VACUUM' ) ;
74087455 logger . info ( '✅ VACUUM complete' ) ;
@@ -7412,6 +7459,17 @@ class DatabaseService {
74127459 * Get the current database file size in bytes
74137460 */
74147461 getDatabaseSize ( ) : number {
7462+ // For PostgreSQL, use pg_database_size()
7463+ if ( this . drizzleDbType === 'postgres' ) {
7464+ // Return 0 from sync context; use getDatabaseSizeAsync for accurate results
7465+ return 0 ;
7466+ }
7467+ // For MySQL, use information_schema
7468+ if ( this . drizzleDbType === 'mysql' ) {
7469+ // Return 0 from sync context; use getDatabaseSizeAsync for accurate results
7470+ return 0 ;
7471+ }
7472+
74157473 const stmt = this . db . prepare ( 'SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size()' ) ;
74167474 const result = stmt . get ( ) as { size : number } | undefined ;
74177475 return result ?. size ?? 0 ;
@@ -8469,8 +8527,8 @@ class DatabaseService {
84698527 [ cutoff ]
84708528 ) ;
84718529 const userStats = await client . query (
8472- `SELECT u.username, COUNT(*) as count FROM audit_log al LEFT JOIN users u ON al.user_id = u.id
8473- WHERE al.timestamp >= $1 GROUP BY al.user_id , u.username ORDER BY count DESC LIMIT 10` ,
8530+ `SELECT u.username, COUNT(*) as count FROM audit_log al LEFT JOIN users u ON al."userId" = u.id
8531+ WHERE al.timestamp >= $1 GROUP BY al."userId" , u.username ORDER BY count DESC LIMIT 10` ,
84748532 [ cutoff ]
84758533 ) ;
84768534 const dailyStats = await client . query (
@@ -8497,8 +8555,8 @@ class DatabaseService {
84978555 [ cutoff ]
84988556 ) ;
84998557 const [ userRows ] = await pool . query (
8500- `SELECT u.username, COUNT(*) as count FROM audit_log al LEFT JOIN users u ON al.user_id = u.id
8501- WHERE al.timestamp >= ? GROUP BY al.user_id ORDER BY count DESC LIMIT 10` ,
8558+ `SELECT u.username, COUNT(*) as count FROM audit_log al LEFT JOIN users u ON al.userId = u.id
8559+ WHERE al.timestamp >= ? GROUP BY al.userId ORDER BY count DESC LIMIT 10` ,
85028560 [ cutoff ]
85038561 ) ;
85048562 const [ dailyRows ] = await pool . query (
@@ -8521,6 +8579,16 @@ class DatabaseService {
85218579
85228580 // Cleanup old audit logs
85238581 cleanupAuditLogs ( days : number ) : number {
8582+ // For PostgreSQL/MySQL, fire-and-forget async cleanup
8583+ if ( this . drizzleDbType === 'postgres' || this . drizzleDbType === 'mysql' ) {
8584+ if ( this . authRepo ) {
8585+ this . authRepo . cleanupOldAuditLogs ( days ) . catch ( err => {
8586+ logger . debug ( 'Failed to cleanup old audit logs:' , err ) ;
8587+ } ) ;
8588+ }
8589+ return 0 ;
8590+ }
8591+
85248592 const cutoff = Date . now ( ) - ( days * 24 * 60 * 60 * 1000 ) ;
85258593 const stmt = this . db . prepare ( 'DELETE FROM audit_log WHERE timestamp < ?' ) ;
85268594 const result = stmt . run ( cutoff ) ;
@@ -10132,14 +10200,23 @@ class DatabaseService {
1013210200 }
1013310201
1013410202 async cleanupOldTraceroutesAsync ( days : number = 30 ) : Promise < number > {
10203+ if ( ( this . drizzleDbType === 'postgres' || this . drizzleDbType === 'mysql' ) && this . traceroutesRepo ) {
10204+ return this . traceroutesRepo . cleanupOldTraceroutes ( days * 24 ) ;
10205+ }
1013510206 return this . cleanupOldTraceroutes ( days ) ;
1013610207 }
1013710208
1013810209 async cleanupOldRouteSegmentsAsync ( days : number = 30 ) : Promise < number > {
10210+ if ( ( this . drizzleDbType === 'postgres' || this . drizzleDbType === 'mysql' ) && this . traceroutesRepo ) {
10211+ return this . traceroutesRepo . cleanupOldRouteSegments ( days ) ;
10212+ }
1013910213 return this . cleanupOldRouteSegments ( days ) ;
1014010214 }
1014110215
1014210216 async cleanupOldNeighborInfoAsync ( days : number = 30 ) : Promise < number > {
10217+ if ( ( this . drizzleDbType === 'postgres' || this . drizzleDbType === 'mysql' ) && this . neighborsRepo ) {
10218+ return this . neighborsRepo . cleanupOldNeighborInfo ( days ) ;
10219+ }
1014310220 return this . cleanupOldNeighborInfo ( days ) ;
1014410221 }
1014510222
@@ -10152,14 +10229,35 @@ class DatabaseService {
1015210229 }
1015310230
1015410231 async cleanupAuditLogsAsync ( days : number ) : Promise < number > {
10232+ if ( ( this . drizzleDbType === 'postgres' || this . drizzleDbType === 'mysql' ) && this . authRepo ) {
10233+ return this . authRepo . cleanupOldAuditLogs ( days ) ;
10234+ }
1015510235 return this . cleanupAuditLogs ( days ) ;
1015610236 }
1015710237
1015810238 async vacuumAsync ( ) : Promise < void > {
10239+ if ( this . drizzleDbType === 'postgres' ) {
10240+ await this . postgresPool ! . query ( 'VACUUM' ) ;
10241+ return ;
10242+ }
10243+ if ( this . drizzleDbType === 'mysql' ) {
10244+ // MySQL auto-manages space; OPTIMIZE TABLE requires table names
10245+ return ;
10246+ }
1015910247 return this . vacuum ( ) ;
1016010248 }
1016110249
1016210250 async getDatabaseSizeAsync ( ) : Promise < number > {
10251+ if ( this . drizzleDbType === 'postgres' ) {
10252+ const result = await this . postgresPool ! . query ( 'SELECT pg_database_size(current_database()) as size' ) ;
10253+ return Number ( result . rows [ 0 ] ?. size ?? 0 ) ;
10254+ }
10255+ if ( this . drizzleDbType === 'mysql' ) {
10256+ const [ rows ] = await this . mysqlPool ! . query (
10257+ `SELECT SUM(data_length + index_length) as size FROM information_schema.tables WHERE table_schema = DATABASE()`
10258+ ) ;
10259+ return Number ( ( rows as any [ ] ) [ 0 ] ?. size ?? 0 ) ;
10260+ }
1016310261 return this . getDatabaseSize ( ) ;
1016410262 }
1016510263
0 commit comments