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

This commit is contained in:
おさむのひと 2024-11-30 16:52:08 +09:00
parent ae1d0b08eb
commit f0c4565787
2 changed files with 35 additions and 23 deletions

View File

@ -3,13 +3,12 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { FindOneOptions, InsertQueryBuilder, ObjectLiteral, Repository, SelectQueryBuilder, TypeORMError } from 'typeorm'; import { FindOneOptions, InsertQueryBuilder, ObjectLiteral, Repository, SelectQueryBuilder } from 'typeorm';
import { DriverUtils } from 'typeorm/driver/DriverUtils.js';
import { RelationCountLoader } from 'typeorm/query-builder/relation-count/RelationCountLoader.js'; import { RelationCountLoader } from 'typeorm/query-builder/relation-count/RelationCountLoader.js';
import { RelationIdLoader } from 'typeorm/query-builder/relation-id/RelationIdLoader.js'; import { RelationIdLoader } from 'typeorm/query-builder/relation-id/RelationIdLoader.js';
import { RawSqlResultsToEntityTransformer } from 'typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer.js'; import {
import { ObjectUtils } from 'typeorm/util/ObjectUtils.js'; RawSqlResultsToEntityTransformer,
import { OrmUtils } from 'typeorm/util/OrmUtils.js'; } from 'typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer.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';
@ -99,7 +98,10 @@ export const miRepository = {
mainAlias.name = 't'; mainAlias.name = 't';
const columnNames = this.createTableColumnNames(); const columnNames = this.createTableColumnNames();
queryBuilder.returning(columnNames.reduce((a, c) => `${a}, ${queryBuilder.escape(c)}`, '').slice(2)); queryBuilder.returning(columnNames.reduce((a, c) => `${a}, ${queryBuilder.escape(c)}`, '').slice(2));
const builder = this.createQueryBuilder().addCommonTableExpression(queryBuilder, 'cte', { columnNames });
const queryRunner = this.manager.connection.createQueryRunner('master');
try {
const builder = this.createQueryBuilder(undefined, queryRunner).addCommonTableExpression(queryBuilder, 'cte', { columnNames });
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
builder.expressionMap.mainAlias!.tablePath = 'cte'; builder.expressionMap.mainAlias!.tablePath = 'cte';
this.selectAliasColumnNames(queryBuilder, builder); this.selectAliasColumnNames(queryBuilder, builder);
@ -112,6 +114,9 @@ export const miRepository = {
const relationCount = await new RelationCountLoader(builder.connection, this.queryRunner, builder.expressionMap.relationCountAttributes).load(raw); const relationCount = await new RelationCountLoader(builder.connection, this.queryRunner, builder.expressionMap.relationCountAttributes).load(raw);
const result = new RawSqlResultsToEntityTransformer(builder.expressionMap, builder.connection.driver, relationId, relationCount, this.queryRunner).transform(raw, mainAlias); const result = new RawSqlResultsToEntityTransformer(builder.expressionMap, builder.connection.driver, relationId, relationCount, this.queryRunner).transform(raw, mainAlias);
return result[0]; return result[0];
} finally {
await queryRunner.release();
}
}, },
selectAliasColumnNames(queryBuilder, builder) { selectAliasColumnNames(queryBuilder, builder) {
let selectOrAddSelect = (selection: string, selectionAliasName?: string) => { let selectOrAddSelect = (selection: string, selectionAliasName?: string) => {

View File

@ -7,6 +7,7 @@
import pg from 'pg'; import pg from 'pg';
import { DataSource, Logger } from 'typeorm'; import { DataSource, Logger } from 'typeorm';
import * as highlight from 'cli-highlight'; import * as highlight from 'cli-highlight';
import { type QueryRunner } from 'typeorm';
import { entities as charts } from '@/core/chart/entities.js'; import { entities as charts } from '@/core/chart/entities.js';
import { MiAbuseUserReport } from '@/models/AbuseUserReport.js'; import { MiAbuseUserReport } from '@/models/AbuseUserReport.js';
@ -98,18 +99,24 @@ class MyCustomLogger implements Logger {
} }
@bindThis @bindThis
public logQuery(query: string, parameters?: any[]) { private replicationMode(runner?: QueryRunner) {
sqlLogger.info(this.highlight(query).substring(0, 100)); const mode = runner?.getReplicationMode();
return mode ? `[${mode}]` : '[default]';
} }
@bindThis @bindThis
public logQueryError(error: string, query: string, parameters?: any[]) { public logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner) {
sqlLogger.error(this.highlight(query)); sqlLogger.info(this.replicationMode(queryRunner) + ' ' + this.highlight(query).substring(0, 100));
} }
@bindThis @bindThis
public logQuerySlow(time: number, query: string, parameters?: any[]) { public logQueryError(error: string, query: string, parameters?: any[], queryRunner?: QueryRunner) {
sqlLogger.warn(this.highlight(query)); sqlLogger.error(this.replicationMode(queryRunner) + ' ' + this.highlight(query));
}
@bindThis
public logQuerySlow(time: number, query: string, parameters?: any[], queryRunner?: QueryRunner) {
sqlLogger.warn(this.replicationMode(queryRunner) + ' ' + this.highlight(query));
} }
@bindThis @bindThis