From e4e388475886a6eddcdbcf1c4f49e9d4465c418d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=8A=E3=81=95=E3=82=80=E3=81=AE=E3=81=B2=E3=81=A8?= <46447427+samunohito@users.noreply.github.com> Date: Tue, 10 Dec 2024 14:14:10 +0900 Subject: [PATCH] wip --- packages/backend/src/models/_.ts | 4 ++++ packages/backend/src/postgres.ts | 30 ++++++++++++++++++------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/backend/src/models/_.ts b/packages/backend/src/models/_.ts index 458c9b9e20..04cd78daa4 100644 --- a/packages/backend/src/models/_.ts +++ b/packages/backend/src/models/_.ts @@ -9,6 +9,7 @@ import { RelationIdLoader } from 'typeorm/query-builder/relation-id/RelationIdLo import { RawSqlResultsToEntityTransformer, } from 'typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer.js'; +import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions.js'; import { MiAbuseUserReport } from '@/models/AbuseUserReport.js'; import { MiAbuseReportNotificationRecipient } from '@/models/AbuseReportNotificationRecipient.js'; import { MiAccessToken } from '@/models/AccessToken.js'; @@ -91,6 +92,9 @@ export const miRepository = { return this.metadata.columns.filter(column => column.isSelect && !column.isVirtual).map(column => column.databaseName); }, async insertOne(entity, findOptions?) { + const opt = this.manager.connection.options as PostgresConnectionOptions; + console.log(opt.replication); + const queryBuilder = this.createQueryBuilder().insert().values(entity); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const mainAlias = queryBuilder.expressionMap.mainAlias!; diff --git a/packages/backend/src/postgres.ts b/packages/backend/src/postgres.ts index 05f2340adf..a680ddd28f 100644 --- a/packages/backend/src/postgres.ts +++ b/packages/backend/src/postgres.ts @@ -91,32 +91,38 @@ export const dbLogger = new MisskeyLogger('db'); const sqlLogger = dbLogger.createSubLogger('sql', 'gray'); class MyCustomLogger implements Logger { - @bindThis - private highlight(sql: string) { - return highlight.highlight(sql, { - language: 'sql', ignoreIllegals: true, - }); + constructor( + private printReplicationMode?: boolean, + ) { } @bindThis - private replicationMode(runner?: QueryRunner) { - const mode = runner?.getReplicationMode(); - return mode ? `[${mode}]` : '[default]'; + private highlight(sql: string, queryRunner?: QueryRunner) { + const result = highlight.highlight(sql, { + language: 'sql', ignoreIllegals: true, + }); + + if (this.printReplicationMode && queryRunner) { + const mode = queryRunner.getReplicationMode(); + return `[${mode}] ${result}`; + } else { + return result; + } } @bindThis public logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner) { - sqlLogger.info(this.replicationMode(queryRunner) + ' ' + this.highlight(query).substring(0, 100)); + sqlLogger.info(this.highlight(query, queryRunner).substring(0, 100)); } @bindThis public logQueryError(error: string, query: string, parameters?: any[], queryRunner?: QueryRunner) { - sqlLogger.error(this.replicationMode(queryRunner) + ' ' + this.highlight(query)); + sqlLogger.error(this.highlight(query, queryRunner)); } @bindThis public logQuerySlow(time: number, query: string, parameters?: any[], queryRunner?: QueryRunner) { - sqlLogger.warn(this.replicationMode(queryRunner) + ' ' + this.highlight(query)); + sqlLogger.warn(this.highlight(query, queryRunner)); } @bindThis @@ -254,7 +260,7 @@ export function createPostgresDataSource(config: Config) { }, } : false, logging: log, - logger: log ? new MyCustomLogger() : undefined, + logger: log ? new MyCustomLogger(config.dbReplications) : undefined, maxQueryExecutionTime: 300, entities: entities, migrations: ['../../migration/*.js'],