@@ -11,10 +11,19 @@ import * as ensDbClientMock from "./ensdb-client.mock";
1111import { EnsDbWriter } from "./ensdb-writer" ;
1212import { EnsNodeMetadataKeys } from "./ensnode-metadata" ;
1313
14+ const executeMock = vi . fn ( async ( ) => undefined ) ;
1415const onConflictDoUpdateMock = vi . fn ( async ( ) => undefined ) ;
1516const valuesMock = vi . fn ( ( ) => ( { onConflictDoUpdate : onConflictDoUpdateMock } ) ) ;
1617const insertMock = vi . fn ( ( ) => ( { values : valuesMock } ) ) ;
17- const drizzleClientMock = { insert : insertMock } as any ;
18+ const transactionMock = vi . fn ( async ( callback : ( tx : any ) => Promise < void > ) => {
19+ const tx = { execute : executeMock , insert : insertMock } ;
20+ return callback ( tx ) ;
21+ } ) ;
22+ const drizzleClientMock = {
23+ insert : insertMock ,
24+ transaction : transactionMock ,
25+ execute : executeMock ,
26+ } as any ;
1827
1928vi . mock ( "drizzle-orm/node-postgres" , ( ) => ( {
2029 drizzle : vi . fn ( ( ) => drizzleClientMock ) ,
@@ -26,9 +35,11 @@ describe("EnsDbWriter", () => {
2635 new EnsDbWriter ( ensDbClientMock . ensDbUrl , ensDbClientMock . ensIndexerSchemaName ) ;
2736
2837 beforeEach ( ( ) => {
38+ executeMock . mockClear ( ) ;
2939 onConflictDoUpdateMock . mockClear ( ) ;
3040 valuesMock . mockClear ( ) ;
3141 insertMock . mockClear ( ) ;
42+ transactionMock . mockClear ( ) ;
3243 vi . mocked ( migrate ) . mockClear ( ) ;
3344 } ) ;
3445
@@ -84,18 +95,31 @@ describe("EnsDbWriter", () => {
8495 } ) ;
8596
8697 describe ( "migrateEnsNodeSchema" , ( ) => {
87- it ( "calls drizzle-orm migrateEnsNodeSchema with the correct parameters" , async ( ) => {
98+ it ( "calls drizzle-orm migrate with the correct parameters inside a transaction " , async ( ) => {
8899 const migrationsDirPath = "/path/to/migrations" ;
89100
90101 await createEnsDbWriter ( ) . migrateEnsNodeSchema ( migrationsDirPath ) ;
91102
92- expect ( vi . mocked ( migrate ) ) . toHaveBeenCalledWith ( drizzleClientMock , {
93- migrationsFolder : migrationsDirPath ,
94- migrationsSchema : "ensnode" ,
95- } ) ;
103+ expect ( transactionMock ) . toHaveBeenCalled ( ) ;
104+ expect ( executeMock ) . toHaveBeenCalledWith (
105+ expect . objectContaining ( {
106+ queryChunks : expect . arrayContaining ( [
107+ expect . objectContaining ( { value : [ "SELECT pg_advisory_xact_lock(" ] } ) ,
108+ expect . any ( BigInt ) ,
109+ expect . objectContaining ( { value : [ ")" ] } ) ,
110+ ] ) ,
111+ } ) ,
112+ ) ;
113+ expect ( vi . mocked ( migrate ) ) . toHaveBeenCalledWith (
114+ expect . objectContaining ( { execute : executeMock } ) ,
115+ {
116+ migrationsFolder : migrationsDirPath ,
117+ migrationsSchema : "ensnode" ,
118+ } ,
119+ ) ;
96120 } ) ;
97121
98- it ( "propagates errors from the migrateEnsNodeSchema function" , async ( ) => {
122+ it ( "propagates errors from the migrate function" , async ( ) => {
99123 const migrationsDirPath = "/path/to/migrations" ;
100124 vi . mocked ( migrate ) . mockRejectedValueOnce ( new Error ( "Migration failed" ) ) ;
101125
0 commit comments