This commit is contained in:
おさむのひと 2024-12-10 14:14:10 +09:00
parent f0c4565787
commit e4e3884758
2 changed files with 22 additions and 12 deletions

View File

@ -9,6 +9,7 @@ import { RelationIdLoader } from 'typeorm/query-builder/relation-id/RelationIdLo
import { import {
RawSqlResultsToEntityTransformer, RawSqlResultsToEntityTransformer,
} from 'typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer.js'; } from 'typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer.js';
import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions.js';
import { MiAbuseUserReport } from '@/models/AbuseUserReport.js'; import { MiAbuseUserReport } from '@/models/AbuseUserReport.js';
import { MiAbuseReportNotificationRecipient } from '@/models/AbuseReportNotificationRecipient.js'; import { MiAbuseReportNotificationRecipient } from '@/models/AbuseReportNotificationRecipient.js';
import { MiAccessToken } from '@/models/AccessToken.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); return this.metadata.columns.filter(column => column.isSelect && !column.isVirtual).map(column => column.databaseName);
}, },
async insertOne(entity, findOptions?) { async insertOne(entity, findOptions?) {
const opt = this.manager.connection.options as PostgresConnectionOptions;
console.log(opt.replication);
const queryBuilder = this.createQueryBuilder().insert().values(entity); const queryBuilder = this.createQueryBuilder().insert().values(entity);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const mainAlias = queryBuilder.expressionMap.mainAlias!; const mainAlias = queryBuilder.expressionMap.mainAlias!;

View File

@ -91,32 +91,38 @@ export const dbLogger = new MisskeyLogger('db');
const sqlLogger = dbLogger.createSubLogger('sql', 'gray'); const sqlLogger = dbLogger.createSubLogger('sql', 'gray');
class MyCustomLogger implements Logger { class MyCustomLogger implements Logger {
@bindThis constructor(
private highlight(sql: string) { private printReplicationMode?: boolean,
return highlight.highlight(sql, { ) {
language: 'sql', ignoreIllegals: true,
});
} }
@bindThis @bindThis
private replicationMode(runner?: QueryRunner) { private highlight(sql: string, queryRunner?: QueryRunner) {
const mode = runner?.getReplicationMode(); const result = highlight.highlight(sql, {
return mode ? `[${mode}]` : '[default]'; language: 'sql', ignoreIllegals: true,
});
if (this.printReplicationMode && queryRunner) {
const mode = queryRunner.getReplicationMode();
return `[${mode}] ${result}`;
} else {
return result;
}
} }
@bindThis @bindThis
public logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner) { 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 @bindThis
public logQueryError(error: string, query: string, parameters?: any[], queryRunner?: QueryRunner) { 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 @bindThis
public logQuerySlow(time: number, query: string, parameters?: any[], queryRunner?: QueryRunner) { 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 @bindThis
@ -254,7 +260,7 @@ export function createPostgresDataSource(config: Config) {
}, },
} : false, } : false,
logging: log, logging: log,
logger: log ? new MyCustomLogger() : undefined, logger: log ? new MyCustomLogger(config.dbReplications) : undefined,
maxQueryExecutionTime: 300, maxQueryExecutionTime: 300,
entities: entities, entities: entities,
migrations: ['../../migration/*.js'], migrations: ['../../migration/*.js'],