|
1 | 1 | jest.mock('../nitro') |
2 | 2 |
|
| 3 | +import Database from 'better-sqlite3' |
3 | 4 | import { HybridNitroSQLite } from '../nitro' |
4 | 5 | import { closeDatabaseQueue, openDatabaseQueue } from '../DatabaseQueue' |
5 | 6 | import { transaction } from '../operations/transaction' |
@@ -208,7 +209,8 @@ describe('transaction', () => { |
208 | 209 | }), |
209 | 210 | ).rejects.toMatchObject({ |
210 | 211 | name: 'NitroSQLiteError', |
211 | | - message: 'rollback failed', |
| 212 | + message: 'callback failed\nRollback failed: rollback failed', |
| 213 | + cause: expect.any(AggregateError), |
212 | 214 | }) |
213 | 215 | }) |
214 | 216 |
|
@@ -239,4 +241,152 @@ describe('transaction', () => { |
239 | 241 | await Promise.all([first, second]) |
240 | 242 | expect(order).toEqual(['first', 'second']) |
241 | 243 | }) |
| 244 | + it.each(['automatic', 'manual', 'caught manual'])( |
| 245 | + 'rolls back a real deferred constraint after a %s commit failure', |
| 246 | + async (mode) => { |
| 247 | + const sqlite = new Database(':memory:') |
| 248 | + sqlite.exec( |
| 249 | + 'PRAGMA foreign_keys = ON; CREATE TABLE Parent (id INTEGER PRIMARY KEY); CREATE TABLE Child (parentId INTEGER REFERENCES Parent(id) DEFERRABLE INITIALLY DEFERRED)', |
| 250 | + ) |
| 251 | + const execute = (_name: string, query: string) => { |
| 252 | + try { |
| 253 | + sqlite.exec(query) |
| 254 | + return nativeResult() |
| 255 | + } catch (error) { |
| 256 | + // The addon creates errors outside Jest's realm. The native bridge |
| 257 | + // exposes a JavaScript Error, so mirror that boundary here. |
| 258 | + if ( |
| 259 | + typeof error === 'object' && |
| 260 | + error !== null && |
| 261 | + 'message' in error && |
| 262 | + typeof error.message === 'string' |
| 263 | + ) { |
| 264 | + throw new Error(error.message, { cause: error }) |
| 265 | + } |
| 266 | + throw error |
| 267 | + } |
| 268 | + } |
| 269 | + jest.mocked(HybridNitroSQLite.execute).mockImplementation(execute) |
| 270 | + jest |
| 271 | + .mocked(HybridNitroSQLite.executeAsync) |
| 272 | + .mockImplementation(async (name, query) => execute(name, query)) |
| 273 | + try { |
| 274 | + await expect( |
| 275 | + transaction(dbName, async (tx) => { |
| 276 | + tx.execute('INSERT INTO Child VALUES (1)') |
| 277 | + if (mode === 'manual') tx.commit() |
| 278 | + if (mode === 'caught manual') { |
| 279 | + expect(() => tx.commit()).toThrow('FOREIGN KEY constraint failed') |
| 280 | + expect(() => tx.execute('SELECT 1')).toThrow( |
| 281 | + 'finalized transaction', |
| 282 | + ) |
| 283 | + expect(() => tx.executeAsync('SELECT 1')).toThrow( |
| 284 | + 'finalized transaction', |
| 285 | + ) |
| 286 | + expect(() => tx.commit()).toThrow('finalized transaction') |
| 287 | + } |
| 288 | + }), |
| 289 | + ).rejects.toThrow('FOREIGN KEY constraint failed') |
| 290 | + expect(sqlite.inTransaction).toBe(false) |
| 291 | + expect( |
| 292 | + sqlite.prepare('SELECT COUNT(*) AS count FROM Child').get(), |
| 293 | + ).toEqual({ count: 0 }) |
| 294 | + await transaction(dbName, async (tx) => { |
| 295 | + tx.execute('INSERT INTO Parent VALUES (1)') |
| 296 | + }) |
| 297 | + expect(sqlite.inTransaction).toBe(false) |
| 298 | + } finally { |
| 299 | + sqlite.close() |
| 300 | + } |
| 301 | + }, |
| 302 | + ) |
| 303 | + |
| 304 | + it('allows explicit rollback to recover a caught commit failure', async () => { |
| 305 | + jest |
| 306 | + .mocked(HybridNitroSQLite.execute) |
| 307 | + .mockImplementation((_name, query) => { |
| 308 | + if (query === 'COMMIT') throw new Error('commit failed') |
| 309 | + return nativeResult() |
| 310 | + }) |
| 311 | + await expect( |
| 312 | + transaction(dbName, async (tx) => { |
| 313 | + expect(() => tx.commit()).toThrow('commit failed') |
| 314 | + tx.rollback() |
| 315 | + return 'recovered' |
| 316 | + }), |
| 317 | + ).resolves.toBe('recovered') |
| 318 | + }) |
| 319 | + |
| 320 | + it.each([false, true])( |
| 321 | + 'rejects a failed manual rollback even when caught: %s', |
| 322 | + async (caught) => { |
| 323 | + jest.mocked(HybridNitroSQLite.execute).mockImplementation(() => { |
| 324 | + throw new Error('rollback failed') |
| 325 | + }) |
| 326 | + await expect( |
| 327 | + transaction(dbName, async (tx) => { |
| 328 | + if (caught) { |
| 329 | + expect(() => tx.rollback()).toThrow('rollback failed') |
| 330 | + return |
| 331 | + } |
| 332 | + tx.rollback() |
| 333 | + }), |
| 334 | + ).rejects.toThrow('rollback failed') |
| 335 | + expect(HybridNitroSQLite.execute).toHaveBeenCalledTimes(1) |
| 336 | + }, |
| 337 | + ) |
| 338 | + |
| 339 | + it('preserves commit and rollback errors in order', async () => { |
| 340 | + jest |
| 341 | + .mocked(HybridNitroSQLite.execute) |
| 342 | + .mockImplementation((_name, query) => { |
| 343 | + throw new Error( |
| 344 | + query === 'COMMIT' ? 'commit failed' : 'rollback failed', |
| 345 | + ) |
| 346 | + }) |
| 347 | + await expect(transaction(dbName, async () => {})).rejects.toMatchObject({ |
| 348 | + message: 'commit failed\nRollback failed: rollback failed', |
| 349 | + cause: { |
| 350 | + errors: [ |
| 351 | + expect.objectContaining({ message: 'commit failed' }), |
| 352 | + expect.objectContaining({ message: 'rollback failed' }), |
| 353 | + ], |
| 354 | + }, |
| 355 | + }) |
| 356 | + }) |
| 357 | + |
| 358 | + it('preserves both errors when manual rollback after a failed commit also fails', async () => { |
| 359 | + jest |
| 360 | + .mocked(HybridNitroSQLite.execute) |
| 361 | + .mockImplementation((_name, query) => { |
| 362 | + throw new Error( |
| 363 | + query === 'COMMIT' ? 'commit failed' : 'rollback failed', |
| 364 | + ) |
| 365 | + }) |
| 366 | + await expect( |
| 367 | + transaction(dbName, async (tx) => { |
| 368 | + expect(() => tx.commit()).toThrow('commit failed') |
| 369 | + expect(() => tx.rollback()).toThrow('rollback failed') |
| 370 | + }), |
| 371 | + ).rejects.toMatchObject({ |
| 372 | + message: 'commit failed\nRollback failed: rollback failed', |
| 373 | + cause: { |
| 374 | + errors: [ |
| 375 | + expect.objectContaining({ message: 'commit failed' }), |
| 376 | + expect.objectContaining({ message: 'rollback failed' }), |
| 377 | + ], |
| 378 | + }, |
| 379 | + }) |
| 380 | + expect(HybridNitroSQLite.execute).toHaveBeenCalledTimes(2) |
| 381 | + }) |
| 382 | + |
| 383 | + it('does not roll back when BEGIN fails', async () => { |
| 384 | + jest |
| 385 | + .mocked(HybridNitroSQLite.executeAsync) |
| 386 | + .mockRejectedValue(new Error('begin failed')) |
| 387 | + const callback = jest.fn() |
| 388 | + await expect(transaction(dbName, callback)).rejects.toThrow('begin failed') |
| 389 | + expect(callback).not.toHaveBeenCalled() |
| 390 | + expect(HybridNitroSQLite.execute).not.toHaveBeenCalled() |
| 391 | + }) |
242 | 392 | }) |
0 commit comments