Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
samunohito committed Dec 10, 2024
1 parent f0c4565 commit e4e3884
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
4 changes: 4 additions & 0 deletions packages/backend/src/models/_.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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!;
Expand Down
28 changes: 17 additions & 11 deletions packages/backend/src/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,32 +91,38 @@ export const dbLogger = new MisskeyLogger('db');
const sqlLogger = dbLogger.createSubLogger('sql', 'gray');

class MyCustomLogger implements Logger {
constructor(
private printReplicationMode?: boolean,
) {
}

@bindThis
private highlight(sql: string) {
return highlight.highlight(sql, {
private highlight(sql: string, queryRunner?: QueryRunner) {
const result = highlight.highlight(sql, {
language: 'sql', ignoreIllegals: true,
});
}

@bindThis
private replicationMode(runner?: QueryRunner) {
const mode = runner?.getReplicationMode();
return mode ? `[${mode}]` : '[default]';
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
Expand Down Expand Up @@ -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'],
Expand Down

0 comments on commit e4e3884

Please sign in to comment.