Skip to content

Commit

Permalink
fix duplicate key
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Nov 24, 2024
1 parent d72175a commit 78914a7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
5 changes: 3 additions & 2 deletions app/core/service/PackageManagerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { BadRequestError, ForbiddenError, NotFoundError } from 'egg-errors';
import { RequireAtLeastOne } from 'type-fest';
import npa from 'npm-package-arg';
import semver from 'semver';
import pMap from 'p-map';
import {
calculateIntegrity,
detectInstallScript,
Expand All @@ -23,6 +24,7 @@ import { AbbreviatedPackageJSONType, AbbreviatedPackageManifestType, PackageJSON
import { PackageVersionBlockRepository } from '../../repository/PackageVersionBlockRepository';
import { PackageVersionDownloadRepository } from '../../repository/PackageVersionDownloadRepository';
import { DistRepository } from '../../repository/DistRepository';
import { isDuplicateKeyError } from '../../repository/util/ErrorUtil';
import { Package } from '../entity/Package';
import { PackageVersion } from '../entity/PackageVersion';
import { PackageVersionBlock } from '../entity/PackageVersionBlock';
Expand All @@ -47,7 +49,6 @@ import { BugVersion } from '../entity/BugVersion';
import { RegistryManagerService } from './RegistryManagerService';
import { Registry } from '../entity/Registry';
import { PackageVersionService } from './PackageVersionService';
import pMap from 'p-map';

export interface PublishPackageCmd {
// maintainer: Maintainer;
Expand Down Expand Up @@ -271,7 +272,7 @@ export class PackageManagerService extends AbstractService {
try {
await this.packageRepository.createPackageVersion(pkgVersion);
} catch (e) {
if (e.code === 'ER_DUP_ENTRY') {
if (isDuplicateKeyError(e)) {
throw new ForbiddenError(`Can't modify pre-existing version: ${pkg.fullname}@${cmd.version}`);
}
throw e;
Expand Down
5 changes: 4 additions & 1 deletion app/core/service/PackageVersionFileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { PackageVersionFileRepository } from '../../repository/PackageVersionFileRepository';
import { PackageVersionRepository } from '../../repository/PackageVersionRepository';
import { DistRepository } from '../../repository/DistRepository';
import { isDuplicateKeyError } from '../../repository/util/ErrorUtil';
import { PackageVersionFile } from '../entity/PackageVersionFile';
import { PackageVersion } from '../entity/PackageVersion';
import { Package } from '../entity/Package';
Expand Down Expand Up @@ -272,7 +273,9 @@ export class PackageVersionFileService extends AbstractService {
file.packageVersionFileId, dist.size, file.path);
} catch (err) {
// ignore Duplicate entry
if (err.code === 'ER_DUP_ENTRY') return file;
if (isDuplicateKeyError(err)) {
return file;
}
throw err;
}
return file;
Expand Down
7 changes: 4 additions & 3 deletions app/repository/TaskRepository.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { strict as assert } from 'node:assert';
import { uniq } from 'lodash';
import { AccessLevel, SingletonProto, Inject } from '@eggjs/tegg';
import { ModelConvertor } from './util/ModelConvertor';
import { isDuplicateKeyError } from './util/ErrorUtil';
import type { Task as TaskModel } from './model/Task';
import type { HistoryTask as HistoryTaskModel } from './model/HistoryTask';
import { Task as TaskEntity, TaskUpdateCondition } from '../core/entity/Task';
import { AbstractRepository } from './AbstractRepository';
import { TaskType, TaskState } from '../../app/common/enum/Task';
import { uniq } from 'lodash';
import { Task as TaskEntity, TaskUpdateCondition } from '../core/entity/Task';

@SingletonProto({
accessLevel: AccessLevel.PUBLIC,
Expand All @@ -28,7 +29,7 @@ export class TaskRepository extends AbstractRepository {
await ModelConvertor.convertEntityToModel(task, this.Task);
} catch (e) {
e.message = '[TaskRepository] insert Task failed: ' + e.message;
if (e.code === 'ER_DUP_ENTRY') {
if (isDuplicateKeyError(e)) {
this.logger.warn(e);
const taskModel = await this.Task.findOne({ bizId: task.bizId });
// 覆盖 bizId 相同的 id 和 taskId
Expand Down
10 changes: 10 additions & 0 deletions app/repository/util/ErrorUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function isDuplicateKeyError(err: any) {
if (err.code === 'ER_DUP_ENTRY') {
return true;
}
if (err.message.includes('duplicate key value violates unique constraint')) {
// pg: duplicate key value violates unique constraint "tasks_uk_task_id"
// code: '23505'
return true;
}

Check warning on line 9 in app/repository/util/ErrorUtil.ts

View check run for this annotation

Codecov / codecov/patch

app/repository/util/ErrorUtil.ts#L6-L9

Added lines #L6 - L9 were not covered by tests
}
2 changes: 1 addition & 1 deletion test/port/controller/TokenController/createToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ describe('test/port/controller/TokenController/createToken.test.ts', () => {
expires: 30,
});

assert.match(res.body.error, /ER_DUP_ENTRY/);
assert.match(res.body.error, /ER_DUP_ENTRY|duplicate key value violates unique constraint/);
});
});

Expand Down

0 comments on commit 78914a7

Please sign in to comment.