From 15a39fbd65428a3fb7a604b17af533d85b1b240d Mon Sep 17 00:00:00 2001 From: Raphael Rivas Date: Fri, 26 Jul 2024 14:05:51 -0300 Subject: [PATCH] fix: preventive hotfix for unaccent --- src/app.module.ts | 3 ++- src/app.service.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/app.service.ts diff --git a/src/app.module.ts b/src/app.module.ts index 48755f50..52559959 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -46,6 +46,7 @@ import { UsersModule } from './users/users.module'; import { TransacaoViewService } from './transacao-bq/transacao-view.service'; import { TransacaoViewModule } from './transacao-bq/transacao-view.module'; import { AppLoggerMiddleware } from './utils/logger-middleware'; +import { AppService } from './app.service'; @Module({ imports: [ @@ -122,7 +123,7 @@ import { AppLoggerMiddleware } from './utils/logger-middleware'; SftpModule, TransacaoViewModule, ], - providers: [TransacaoViewService], + providers: [TransacaoViewService, AppService], }) export class AppModule implements NestModule { configure(consumer: MiddlewareConsumer) { diff --git a/src/app.service.ts b/src/app.service.ts new file mode 100644 index 00000000..945a3b17 --- /dev/null +++ b/src/app.service.ts @@ -0,0 +1,41 @@ +import { Injectable, OnModuleInit } from '@nestjs/common'; +import { InjectDataSource, InjectRepository } from '@nestjs/typeorm'; +import { DataSource, Repository } from 'typeorm'; +import { CustomLogger } from './utils/custom-logger'; + +@Injectable() +export class AppService implements OnModuleInit { + private logger: CustomLogger = new CustomLogger(AppService.name, { + timestamp: true, + }); + + constructor(@InjectDataSource() private dataSource: DataSource) {} + + async onModuleInit() { + await this.applyHotfix(); + } + + async applyHotfix() { + const queryRunner = this.dataSource.createQueryRunner(); + try { + this.logger.log('Applying preventive hotfix...'); + await queryRunner.connect(); + /** + * Unaccent + * + * Sometimes the unaccent extension is dropped with no reason so far. + * To prevent it, everytime Nest runs, it will re-install this extension. + */ + await queryRunner.query('DROP EXTENSION IF EXISTS unaccent'); + await queryRunner.query('CREATE EXTENSION IF NOT EXISTS unaccent'); + } catch (error) { + this.logger.error( + `Falha ao rodar o hotfix - ${error?.message}`, + error?.stack, + ); + } finally { + await queryRunner.release(); + this.logger.log('Preventive hotfix applied!'); + } + } +}