diff --git a/__tests__/setup.ts b/__tests__/setup.ts index 44acb6a722..c5b070ad19 100644 --- a/__tests__/setup.ts +++ b/__tests__/setup.ts @@ -61,20 +61,16 @@ const cleanDatabase = async (): Promise => { await remoteConfig.init(); const con = await createOrGetConnection(); - for (const entity of con.entityMetadatas) { - const repository = con.getRepository(entity.name); - if (repository.metadata.tableType === 'view') continue; - await repository.query(`DELETE - FROM "${entity.tableName}";`); - for (const column of entity.primaryColumns) { - if (column.generationStrategy === 'increment') { - await repository.query( - `ALTER SEQUENCE ${entity.tableName}_${column.databaseName}_seq RESTART WITH 1`, - ); - } - } - } + // Get all table names, excluding views + const tableNames = con.entityMetadatas + .filter((entity) => entity.tableType !== 'view') + .map((entity) => `"${entity.tableName}"`) + .join(', '); + + // Single TRUNCATE with CASCADE and RESTART IDENTITY + // Much faster than individual DELETE statements per table + await con.query(`TRUNCATE TABLE ${tableNames} RESTART IDENTITY CASCADE`); }; export const fileTypeFromBuffer = jest.fn();