レプリケーション設定時におけるinsertOne()の挙動を調整 (#15109)

* returningを含むクエリをmasterで動かす

* wip

* wip

* fix CHANGELOG.md

* 調整

* fix

* fix import
This commit is contained in:
おさむのひと 2025-04-13 20:44:44 +09:00 committed by GitHub
parent 4c473eb76d
commit 1f0621b085
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 81 additions and 34 deletions

View file

@ -5,7 +5,7 @@
// https://github.com/typeorm/typeorm/issues/2400
import pg from 'pg';
import { DataSource, Logger } from 'typeorm';
import { DataSource, Logger, type QueryRunner } from 'typeorm';
import * as highlight from 'cli-highlight';
import { entities as charts } from '@/core/chart/entities.js';
import { Config } from '@/config.js';
@ -96,6 +96,7 @@ const sqlLogger = dbLogger.createSubLogger('sql', 'gray');
export type LoggerProps = {
disableQueryTruncation?: boolean;
enableQueryParamLogging?: boolean;
printReplicationMode?: boolean,
};
function highlightSql(sql: string) {
@ -121,8 +122,10 @@ class MyCustomLogger implements Logger {
}
@bindThis
private transformQueryLog(sql: string) {
let modded = sql;
private transformQueryLog(sql: string, opts?: {
prefix?: string;
}) {
let modded = opts?.prefix ? opts.prefix + sql : sql;
if (!this.props.disableQueryTruncation) {
modded = truncateSql(modded);
}
@ -140,18 +143,27 @@ class MyCustomLogger implements Logger {
}
@bindThis
public logQuery(query: string, parameters?: any[]) {
sqlLogger.info(this.transformQueryLog(query), this.transformParameters(parameters));
public logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner) {
const prefix = (this.props.printReplicationMode && queryRunner)
? `[${queryRunner.getReplicationMode()}] `
: undefined;
sqlLogger.info(this.transformQueryLog(query, { prefix }), this.transformParameters(parameters));
}
@bindThis
public logQueryError(error: string, query: string, parameters?: any[]) {
sqlLogger.error(this.transformQueryLog(query), this.transformParameters(parameters));
public logQueryError(error: string, query: string, parameters?: any[], queryRunner?: QueryRunner) {
const prefix = (this.props.printReplicationMode && queryRunner)
? `[${queryRunner.getReplicationMode()}] `
: undefined;
sqlLogger.error(this.transformQueryLog(query, { prefix }), this.transformParameters(parameters));
}
@bindThis
public logQuerySlow(time: number, query: string, parameters?: any[]) {
sqlLogger.warn(this.transformQueryLog(query), this.transformParameters(parameters));
public logQuerySlow(time: number, query: string, parameters?: any[], queryRunner?: QueryRunner) {
const prefix = (this.props.printReplicationMode && queryRunner)
? `[${queryRunner.getReplicationMode()}] `
: undefined;
sqlLogger.warn(this.transformQueryLog(query, { prefix }), this.transformParameters(parameters));
}
@bindThis
@ -298,6 +310,7 @@ export function createPostgresDataSource(config: Config) {
? new MyCustomLogger({
disableQueryTruncation: config.logging?.sql?.disableQueryTruncation,
enableQueryParamLogging: config.logging?.sql?.enableQueryParamLogging,
printReplicationMode: !!config.dbReplications,
})
: undefined,
maxQueryExecutionTime: 300,