0

I'm trying to achieve a very simple thing:

START TRANSACTION;
DELETE FROM table WHERE id = 1;
ROLLBACK;

Running this on the postgres database works perfectly. With massive.js it doesn't:

this.db.run(
   "START TRANSACTION",
   []
); 
setTimeout(() => {
   this.db.run(
       "DELETE FROM table WHERE id = $1"
       [1]
   );
}, 2000);
setTimeout(() => {
   this.db.run(
       "ROLLBACK;"
       []
   );
}, 4000);

It doesn't rollback the changes, just deletes from the database. COMMIT doesn't work as well. What's wrong?

Is there some way to dump queries order?

Pavel_K
  • 10,748
  • 13
  • 73
  • 186
khernik
  • 2,059
  • 2
  • 26
  • 51

1 Answers1

0

Massive uses pg-promise underneath, which in turn supports transactions:

db.instance.tx(t => {
    // BEGIN has been executed
    return t.none('DELETE FROM table WHERE id = $1', [123])
        .then(() => {
            // Records have been deleted within the transaction

            throw new Error('Random error to fail the transaction');
            // = the same as returning Promise.reject(new Error('Random...'))
        });
})
    .catch(error => {
        // ROLLBACK has been executed, no records end up deleted.
        console.log(error);
    });

See also:

Is there some way to dump queries order?

Monitoring Queries shows you how to do it, or you can add event query handler within db.driverConfig object when initializing massive.

Community
  • 1
  • 1
vitaly-t
  • 24,279
  • 15
  • 116
  • 138