(sequelize, Task, from, to)
| 441 | class="cm">// Lock the row with id of `from`, and then try to update the row |
| 442 | class="cm">// with id of `to` |
| 443 | const update = async (sequelize, Task, from, to) => { |
| 444 | await sequelize |
| 445 | .transaction(async transaction => { |
| 446 | try { |
| 447 | try { |
| 448 | await Task.findAll({ |
| 449 | where: { id: { [Sequelize.Op.eq]: from } }, |
| 450 | lock: transaction.LOCK.UPDATE, |
| 451 | transaction |
| 452 | }); |
| 453 | |
| 454 | await delay(10); |
| 455 | |
| 456 | await Task.update( |
| 457 | { id: to }, |
| 458 | { |
| 459 | where: { id: { [Sequelize.Op.ne]: to } }, |
| 460 | lock: transaction.LOCK.UPDATE, |
| 461 | transaction |
| 462 | } |
| 463 | ); |
| 464 | } catch (e) { |
| 465 | console.log(e.message); |
| 466 | } |
| 467 | |
| 468 | await Task.create({ id: 2 }, { transaction }); |
| 469 | } catch (e) { |
| 470 | console.log(e.message); |
| 471 | } |
| 472 | |
| 473 | throw new Error(class="st">'Rollback!'); |
| 474 | }) |
| 475 | .catch(() => {}); |
| 476 | }; |
| 477 | |
| 478 | it(class="st">'should treat deadlocked transaction as rollback', async function() { |
| 479 | const Task = await getAndInitializeTaskModel(this.sequelize); |
no test coverage detected