diff --git a/dictation_server/db/migrations/050-add-index-licenses_order.sql b/dictation_server/db/migrations/050-add-index-licenses_order.sql new file mode 100644 index 0000000..006bde4 --- /dev/null +++ b/dictation_server/db/migrations/050-add-index-licenses_order.sql @@ -0,0 +1,7 @@ +-- +migrate Up +ALTER TABLE `license_orders` ADD INDEX `idx_from_account_id_and_po_number` (from_account_id,po_number); +ALTER TABLE `license_orders` DROP INDEX `license_orders_fk_from_account_id`; + +-- +migrate Down +ALTER TABLE `license_orders` DROP INDEX `idx_from_account_id_and_po_number`; +ALTER TABLE `license_orders` ADD INDEX `license_orders_fk_from_account_id` (from_account_id); \ No newline at end of file diff --git a/dictation_server/src/constants/index.ts b/dictation_server/src/constants/index.ts index 439c7ed..4c63064 100644 --- a/dictation_server/src/constants/index.ts +++ b/dictation_server/src/constants/index.ts @@ -295,3 +295,16 @@ export const TERM_TYPE = { * @const {string} */ export const USER_AUDIO_FORMAT = 'DS2(QP)'; + +/** + * ユニットテスト実行をしている場合のNODE_ENVの値 + * @const {string[]} + */ +export const NODE_ENV_TEST = 'test'; + +/** + * SQLのlockOptionの種類 + */ +export const DB_LOCK_MODE = { + PESSIMISTIC_WRITE: 'pessimistic_write', +}; diff --git a/dictation_server/src/repositories/licenses/licenses.repository.service.ts b/dictation_server/src/repositories/licenses/licenses.repository.service.ts index 988e8c1..2004be3 100644 --- a/dictation_server/src/repositories/licenses/licenses.repository.service.ts +++ b/dictation_server/src/repositories/licenses/licenses.repository.service.ts @@ -1,5 +1,5 @@ import { Injectable, Logger } from '@nestjs/common'; -import { DataSource, EntityManager, In, Not } from 'typeorm'; +import { DataSource, In } from 'typeorm'; import { LicenseOrder, License, @@ -9,9 +9,11 @@ import { } from './entity/license.entity'; import { CARD_LICENSE_LENGTH, + DB_LOCK_MODE, LICENSE_ALLOCATED_STATUS, LICENSE_ISSUE_STATUS, LICENSE_TYPE, + NODE_ENV_TEST, SWITCH_FROM_TYPE, TIERS, } from '../../constants'; @@ -415,15 +417,23 @@ export class LicensesRepositoryService { const licenseOrderRepo = entityManager.getRepository(LicenseOrder); const licenseRepo = entityManager.getRepository(License); + let lockOption = {}; + // テスト環境の場合は悲観的ロックを行わない + if (process.env.NODE_ENV !== NODE_ENV_TEST) { + lockOption = { mode: DB_LOCK_MODE.PESSIMISTIC_WRITE }; + } + const issuingOrder = await licenseOrderRepo.findOne({ where: { from_account_id: orderedAccountId, po_number: poNumber, }, comment: `${context.getTrackingId()}_${new Date().toUTCString()}`, + + ...lockOption, }); - // 注文が存在しない場合、エラー if (!issuingOrder) { + // 注文が存在しない場合、エラー throw new OrderNotFoundError(`No order found for PONumber:${poNumber}`); } // 既に発行済みの注文の場合、エラー