@@ -1272,23 +1272,28 @@ export class MetadataManager implements IMetadataService {
12721272
12731273 // Convert rows to MetadataHistoryRecord format
12741274 const includeMetadata = options ?. includeMetadata !== false ;
1275- const historyResult = records . map ( ( row : Record < string , unknown > ) => ( {
1276- id : row . id as string ,
1277- metadataId : row . metadata_id as string ,
1278- name : row . name as string ,
1279- type : row . type as string ,
1280- version : row . version as number ,
1281- operationType : row . operation_type as 'create' | 'update' | 'publish' | 'revert' | 'delete' ,
1282- metadata : includeMetadata
1283- ? ( typeof row . metadata === 'string' ? JSON . parse ( row . metadata as string ) : row . metadata )
1284- : undefined ,
1285- checksum : row . checksum as string ,
1286- previousChecksum : row . previous_checksum as string | undefined ,
1287- changeNote : row . change_note as string | undefined ,
1288- tenantId : row . tenant_id as string | undefined ,
1289- recordedBy : row . recorded_by as string | undefined ,
1290- recordedAt : row . recorded_at as string ,
1291- } ) ) ;
1275+ const historyResult = records . map ( ( row : Record < string , unknown > ) => {
1276+ const parsedMetadata =
1277+ typeof row . metadata === 'string'
1278+ ? JSON . parse ( row . metadata as string )
1279+ : ( row . metadata as Record < string , unknown > | null | undefined ) ;
1280+
1281+ return {
1282+ id : row . id as string ,
1283+ metadataId : row . metadata_id as string ,
1284+ name : row . name as string ,
1285+ type : row . type as string ,
1286+ version : row . version as number ,
1287+ operationType : row . operation_type as 'create' | 'update' | 'publish' | 'revert' | 'delete' ,
1288+ metadata : includeMetadata ? parsedMetadata : null ,
1289+ checksum : row . checksum as string ,
1290+ previousChecksum : row . previous_checksum as string | undefined ,
1291+ changeNote : row . change_note as string | undefined ,
1292+ tenantId : row . tenant_id as string | undefined ,
1293+ recordedBy : row . recorded_by as string | undefined ,
1294+ recordedAt : row . recorded_at as string ,
1295+ } ;
1296+ } ) ;
12921297
12931298 return {
12941299 records : historyResult ,
@@ -1315,9 +1320,8 @@ export class MetadataManager implements IMetadataService {
13151320 throw new Error ( 'Rollback requires a database loader to be configured' ) ;
13161321 }
13171322
1318- // Get the target version from history
1319- const history = await this . getHistory ( type , name , { limit : 1000 , includeMetadata : true } ) ;
1320- const targetVersion = history . records . find ( r => r . version === version ) ;
1323+ // Fetch the target version snapshot directly from the history table
1324+ const targetVersion = await dbLoader . getHistoryRecord ( type , name , version ) ;
13211325
13221326 if ( ! targetVersion ) {
13231327 throw new Error ( `Version ${ version } not found in history for ${ type } /${ name } ` ) ;
@@ -1327,41 +1331,17 @@ export class MetadataManager implements IMetadataService {
13271331 throw new Error ( `Version ${ version } metadata snapshot not available` ) ;
13281332 }
13291333
1330- // Restore the metadata
1334+ // Restore the metadata using the dedicated rollback path so that a single
1335+ // 'revert' history entry is written (instead of a conflicting 'update' entry)
13311336 const restoredMetadata = targetVersion . metadata ;
1332-
1333- // Register the restored version
1334- await this . register ( type , name , restoredMetadata ) ;
1335-
1336- // Create a history record for the rollback operation
1337- const driver = ( dbLoader as any ) . driver as IDataDriver ;
1338- const tableName = ( dbLoader as any ) . tableName as string ;
1339- const tenantId = ( dbLoader as any ) . tenantId as string | undefined ;
1340-
1341- const filter : Record < string , unknown > = { type, name } ;
1342- if ( tenantId ) {
1343- filter . tenant_id = tenantId ;
1344- }
1345-
1346- const metadataRecord = await driver . findOne ( tableName , {
1347- object : tableName ,
1348- where : filter ,
1349- } ) ;
1350-
1351- if ( metadataRecord ) {
1352- const currentVersion = ( metadataRecord . version as number ) ?? 1 ;
1353- await ( dbLoader as any ) . createHistoryRecord (
1354- metadataRecord . id as string ,
1355- type ,
1356- name ,
1357- currentVersion ,
1358- restoredMetadata ,
1359- 'revert' ,
1360- metadataRecord . checksum as string | undefined ,
1361- options ?. changeNote ?? `Rolled back to version ${ version } ` ,
1362- options ?. recordedBy
1363- ) ;
1364- }
1337+ await dbLoader . registerRollback (
1338+ type ,
1339+ name ,
1340+ restoredMetadata ,
1341+ version ,
1342+ options ?. changeNote ,
1343+ options ?. recordedBy
1344+ ) ;
13651345
13661346 return restoredMetadata ;
13671347 }
@@ -1381,10 +1361,9 @@ export class MetadataManager implements IMetadataService {
13811361 throw new Error ( 'Diff requires a database loader to be configured' ) ;
13821362 }
13831363
1384- // Get both versions from history
1385- const history = await this . getHistory ( type , name , { limit : 1000 , includeMetadata : true } ) ;
1386- const v1 = history . records . find ( r => r . version === version1 ) ;
1387- const v2 = history . records . find ( r => r . version === version2 ) ;
1364+ // Fetch the two version snapshots directly from the history table
1365+ const v1 = await dbLoader . getHistoryRecord ( type , name , version1 ) ;
1366+ const v2 = await dbLoader . getHistoryRecord ( type , name , version2 ) ;
13881367
13891368 if ( ! v1 ) {
13901369 throw new Error ( `Version ${ version1 } not found in history for ${ type } /${ name } ` ) ;
0 commit comments