import { DataSource } from 'typeorm';

export const truncateAllTable = async (source: DataSource) => {
  const entities = source.entityMetadatas;
  const queryRunner = source.createQueryRunner();

  try {
    await queryRunner.startTransaction();
    await queryRunner.query('SET FOREIGN_KEY_CHECKS=0');
    for (const entity of entities) {
      await queryRunner.query(`TRUNCATE TABLE \`${entity.tableName}\``);
    }
    await queryRunner.query('SET FOREIGN_KEY_CHECKS=1');
    await queryRunner.commitTransaction();
  } catch (err) {
    await queryRunner.rollbackTransaction();
    throw err;
  } finally {
    await queryRunner.release();
  }
};
