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 {
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!;

View File

@ -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'],