@@ -30,7 +30,6 @@ import { wait } from "@/utils/wait.js";
3030import type { PGlite } from "@electric-sql/pglite" ;
3131import {
3232 type TableConfig ,
33- and ,
3433 eq ,
3534 getTableColumns ,
3635 getTableName ,
@@ -1289,77 +1288,57 @@ FOR EACH ROW EXECUTE FUNCTION "${namespace}".${getTableNames(table).triggerFn};
12891288 } ) ;
12901289 } ,
12911290 async revert ( { checkpoint, tx } ) {
1292- await this . wrap ( { method : "revert" , includeTraceLogs : true } , ( ) =>
1291+ await this . record ( { method : "revert" , includeTraceLogs : true } , ( ) =>
12931292 Promise . all (
12941293 tables . map ( async ( table ) => {
12951294 const primaryKeyColumns = getPrimaryKeyColumns ( table ) ;
12961295
1297- // @ts -ignore
1298- const { rows } = await tx . execute < Schema > (
1299- sql . raw (
1300- `DELETE FROM "${ namespace } "."${ getTableName ( getReorgTable ( table ) ) } " WHERE checkpoint > '${ checkpoint } ' RETURNING *` ,
1301- ) ,
1302- ) ;
1303-
1304- const reversed = rows . sort (
1305- // @ts -ignore
1306- ( a , b ) => b . operation_id - a . operation_id ,
1296+ const result = await tx . execute (
1297+ sql . raw ( `
1298+ WITH reverted1 AS (
1299+ DELETE FROM "${ namespace } "."${ getTableName ( getReorgTable ( table ) ) } "
1300+ WHERE checkpoint > '${ checkpoint } ' RETURNING *
1301+ ), reverted2 AS (
1302+ SELECT ${ primaryKeyColumns . map ( ( { sql } ) => `"${ sql } "` ) . join ( ", " ) } , MIN(operation_id) AS operation_id FROM reverted1
1303+ GROUP BY ${ primaryKeyColumns . map ( ( { sql } ) => `"${ sql } "` ) . join ( ", " ) }
1304+ ), reverted3 AS (
1305+ SELECT ${ Object . values ( getTableColumns ( table ) )
1306+ . map ( ( column ) => `reverted1."${ getColumnCasing ( column , "snake_case" ) } "` )
1307+ . join ( ", " ) } , reverted1.operation FROM reverted2
1308+ INNER JOIN reverted1
1309+ ON ${ primaryKeyColumns . map ( ( { sql } ) => `reverted2."${ sql } " = reverted1."${ sql } "` ) . join ( "AND " ) }
1310+ AND reverted2.operation_id = reverted1.operation_id
1311+ ), inserted AS (
1312+ DELETE FROM "${ namespace } "."${ getTableName ( table ) } " as t
1313+ WHERE EXISTS (
1314+ SELECT * FROM reverted3
1315+ WHERE ${ primaryKeyColumns . map ( ( { sql } ) => `t."${ sql } " = reverted3."${ sql } "` ) . join ( "AND " ) }
1316+ AND OPERATION = 0
1317+ )
1318+ RETURNING *
1319+ ), updated_or_deleted AS (
1320+ INSERT INTO "${ namespace } "."${ getTableName ( table ) } "
1321+ SELECT ${ Object . values ( getTableColumns ( table ) )
1322+ . map ( ( column ) => `"${ getColumnCasing ( column , "snake_case" ) } "` )
1323+ . join ( ", " ) } FROM reverted3
1324+ WHERE operation = 1 OR operation = 2
1325+ ON CONFLICT (${ primaryKeyColumns . map ( ( { sql } ) => `"${ sql } "` ) . join ( ", " ) } )
1326+ DO UPDATE SET
1327+ ${ Object . values ( getTableColumns ( table ) )
1328+ . map (
1329+ ( column ) =>
1330+ `"${ getColumnCasing ( column , "snake_case" ) } " = EXCLUDED."${ getColumnCasing ( column , "snake_case" ) } "` ,
1331+ )
1332+ . join ( ", " ) }
1333+ RETURNING *
1334+ ) SELECT COUNT(*) FROM reverted1 as count;
1335+ ` ) ,
13071336 ) ;
13081337
1309- // undo operation
1310- for ( const log of reversed ) {
1311- if ( log . operation === 0 ) {
1312- // create
1313-
1314- await tx . delete ( table ) . where (
1315- and (
1316- ...primaryKeyColumns . map ( ( { js, sql } ) =>
1317- // @ts -ignore
1318- eq ( table [ js ] ! , log [ sql ] ) ,
1319- ) ,
1320- ) ,
1321- ) ;
1322- } else if ( log . operation === 1 ) {
1323- // update
1324-
1325- // @ts -ignore
1326- log . operation_id = undefined ;
1327- // @ts -ignore
1328- log . checkpoint = undefined ;
1329- // @ts -ignore
1330- log . operation = undefined ;
1331- await tx
1332- . update ( table )
1333- . set ( log )
1334- . where (
1335- and (
1336- ...primaryKeyColumns . map ( ( { js, sql } ) =>
1337- // @ts -ignore
1338- eq ( table [ js ] ! , log [ sql ] ) ,
1339- ) ,
1340- ) ,
1341- ) ;
1342- } else {
1343- // delete
1344-
1345- // @ts -ignore
1346- log . operation_id = undefined ;
1347- // @ts -ignore
1348- log . checkpoint = undefined ;
1349- // @ts -ignore
1350- log . operation = undefined ;
1351- await tx
1352- . insert ( table )
1353- . values ( log )
1354- . onConflictDoNothing ( {
1355- target : primaryKeyColumns . map ( ( { js } ) => table [ js ] ! ) ,
1356- } ) ;
1357- }
1358- }
1359-
13601338 common . logger . info ( {
13611339 service : "database" ,
1362- msg : `Reverted ${ rows . length } unfinalized operations from '${ getTableName ( table ) } '` ,
1340+ // @ts -ignore
1341+ msg : `Reverted ${ result . rows [ 0 ] ! . count } unfinalized operations from '${ getTableName ( table ) } '` ,
13631342 } ) ;
13641343 } ) ,
13651344 ) ,
0 commit comments