-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
レプリケーション設定時におけるinsertOne()の挙動を調整 #15109
Open
samunohito
wants to merge
6
commits into
misskey-dev:develop
Choose a base branch
from
samunohito:fix/fix-insert-execute-to-master
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+62
−16
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f0c4565
returningを含むクエリをmasterで動かす
samunohito e4e3884
wip
samunohito f2c6485
wip
samunohito 302b227
fix CHANGELOG.md
samunohito 72e6adf
Merge branch 'develop' into fix/fix-insert-execute-to-master
samunohito ca03491
調整
samunohito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,20 @@ | |
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import { FindOneOptions, InsertQueryBuilder, ObjectLiteral, Repository, SelectQueryBuilder, TypeORMError } from 'typeorm'; | ||
import { DriverUtils } from 'typeorm/driver/DriverUtils.js'; | ||
import { | ||
FindOneOptions, | ||
InsertQueryBuilder, | ||
ObjectLiteral, | ||
QueryRunner, | ||
Repository, | ||
SelectQueryBuilder, | ||
} from 'typeorm'; | ||
import { RelationCountLoader } from 'typeorm/query-builder/relation-count/RelationCountLoader.js'; | ||
import { RelationIdLoader } from 'typeorm/query-builder/relation-id/RelationIdLoader.js'; | ||
import { RawSqlResultsToEntityTransformer } from 'typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer.js'; | ||
import { ObjectUtils } from 'typeorm/util/ObjectUtils.js'; | ||
import { OrmUtils } from 'typeorm/util/OrmUtils.js'; | ||
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'; | ||
|
@@ -83,7 +90,11 @@ import type { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialE | |
|
||
export interface MiRepository<T extends ObjectLiteral> { | ||
createTableColumnNames(this: Repository<T> & MiRepository<T>): string[]; | ||
|
||
insertOne(this: Repository<T> & MiRepository<T>, entity: QueryDeepPartialEntity<T>, findOptions?: Pick<FindOneOptions<T>, 'relations'>): Promise<T>; | ||
|
||
insertOneImpl(this: Repository<T> & MiRepository<T>, entity: QueryDeepPartialEntity<T>, findOptions?: Pick<FindOneOptions<T>, 'relations'>, queryRunner?: QueryRunner): Promise<T>; | ||
|
||
selectAliasColumnNames(this: Repository<T> & MiRepository<T>, queryBuilder: InsertQueryBuilder<T>, builder: SelectQueryBuilder<T>): void; | ||
} | ||
|
||
|
@@ -92,14 +103,31 @@ 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; | ||
if (opt.replication) { | ||
const queryRunner = this.manager.connection.createQueryRunner('master'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これによりmasterノードでクエリを実行してくれるrunnerが取れます。 |
||
try { | ||
return this.insertOneImpl(entity, findOptions, queryRunner); | ||
} finally { | ||
await queryRunner.release(); | ||
} | ||
} else { | ||
return this.insertOneImpl(entity, findOptions); | ||
} | ||
}, | ||
async insertOneImpl(entity, findOptions?, queryRunner?) { | ||
// ---- insert + returningの結果を共通テーブル式(CTE)に保持するクエリを生成 ---- | ||
|
||
const queryBuilder = this.createQueryBuilder().insert().values(entity); | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const mainAlias = queryBuilder.expressionMap.mainAlias!; | ||
const name = mainAlias.name; | ||
mainAlias.name = 't'; | ||
const columnNames = this.createTableColumnNames(); | ||
queryBuilder.returning(columnNames.reduce((a, c) => `${a}, ${queryBuilder.escape(c)}`, '').slice(2)); | ||
const builder = this.createQueryBuilder().addCommonTableExpression(queryBuilder, 'cte', { columnNames }); | ||
|
||
// ---- 共通テーブル式(CTE)から結果を取得 ---- | ||
const builder = this.createQueryBuilder(undefined, queryRunner).addCommonTableExpression(queryBuilder, 'cte', { columnNames }); | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
builder.expressionMap.mainAlias!.tablePath = 'cte'; | ||
this.selectAliasColumnNames(queryBuilder, builder); | ||
|
@@ -197,7 +225,9 @@ export { | |
}; | ||
|
||
export type AbuseUserReportsRepository = Repository<MiAbuseUserReport> & MiRepository<MiAbuseUserReport>; | ||
export type AbuseReportNotificationRecipientRepository = Repository<MiAbuseReportNotificationRecipient> & MiRepository<MiAbuseReportNotificationRecipient>; | ||
export type AbuseReportNotificationRecipientRepository = | ||
Repository<MiAbuseReportNotificationRecipient> | ||
& MiRepository<MiAbuseReportNotificationRecipient>; | ||
export type AccessTokensRepository = Repository<MiAccessToken> & MiRepository<MiAccessToken>; | ||
export type AdsRepository = Repository<MiAd> & MiRepository<MiAd>; | ||
export type AnnouncementsRepository = Repository<MiAnnouncement> & MiRepository<MiAnnouncement>; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
configに設定したreplicationの値が巡り巡ってこの値に入っています。
レプリケーションが無効の時はnull(かundefinedかは忘れましたが)になっているので、else(=今までの処理)に流れます。