Transaction issue with knexjs, typescript and mariadb

前端 未结 2 1313
野趣味
野趣味 2021-01-15 12:32

I want to have a transaction, in typescript, to refresh some data in a table. To do so the following steps need to apply:

  1. Truncate all the records from a table
2条回答
  •  一整个雨季
    2021-01-15 13:07

    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.

提交回复
热议问题