I want to have a transaction, in typescript, to refresh some data in a table. To do so the following steps need to apply:
Don't do it that way. Instead:
CREATE TABLE new LIKE real;
populate `new`
RENAME TABLE real TO old, new TO real;
DROP TABLE old;
If the CREATE
or populate
fail, then abandon the task; no harm done. (Well, need to DROP TABLE real
.)
The RENAME TABLE
is very fast, and atomic, and unlikely to fail.
This approach has the advantage that, to other queries, the real
table is available and fully populated the entire time.
With mysql all schema altering DDL queries does implicit commit and they cannot be rolled back. So you need to change your implementation to guarantee integrity of db in some other way.
https://dev.mysql.com/doc/refman/8.0/en/cannot-roll-back.html https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html