feat(v2.2): 完成高频读取热路径
This commit is contained in:
@@ -55,6 +55,31 @@ function buildAppData(): Record<string, any> {
|
||||
},
|
||||
],
|
||||
},
|
||||
members: {
|
||||
members: [
|
||||
{
|
||||
id: 'member-pm',
|
||||
name: 'Product manager',
|
||||
username: 'pm',
|
||||
email: '',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
{
|
||||
id: 'member-dev',
|
||||
name: 'Developer',
|
||||
username: 'dev',
|
||||
email: 'dev@example.com',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
{
|
||||
id: 'member-test',
|
||||
name: 'Tester',
|
||||
username: 'test',
|
||||
email: '',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
},
|
||||
'task-categories': [
|
||||
{
|
||||
id: 'cat-fe',
|
||||
@@ -261,6 +286,11 @@ describe('mapAppDataToV22Rows', () => {
|
||||
releaseDate: '2026-02-01',
|
||||
}),
|
||||
]);
|
||||
expect(result.users).toEqual([
|
||||
expect.objectContaining({ id: 'member-pm', email: 'pm@local.ftb', name: 'Product manager' }),
|
||||
expect.objectContaining({ id: 'member-dev', email: 'dev@example.com', name: 'Developer' }),
|
||||
expect.objectContaining({ id: 'member-test', email: 'test@local.ftb', name: 'Tester' }),
|
||||
]);
|
||||
|
||||
expect(result.requirements).toEqual([
|
||||
expect.objectContaining({
|
||||
|
||||
@@ -34,6 +34,15 @@ interface VersionRow {
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
interface UserRow {
|
||||
id: string;
|
||||
email: string;
|
||||
name: string;
|
||||
avatar?: string;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
interface RequirementRow {
|
||||
id: string;
|
||||
productId: string;
|
||||
@@ -256,6 +265,7 @@ export interface V22MappedRows {
|
||||
products: ProductRow[];
|
||||
projects: ProjectRow[];
|
||||
versions: VersionRow[];
|
||||
users: UserRow[];
|
||||
requirements: RequirementRow[];
|
||||
taskCategories: TaskCategoryRow[];
|
||||
versionPlans: VersionPlanRow[];
|
||||
@@ -277,6 +287,7 @@ export function mapAppDataToV22Rows(appData: Record<string, unknown>): V22Mapped
|
||||
const products: ProductRow[] = [];
|
||||
const projects: ProjectRow[] = [];
|
||||
const versions: VersionRow[] = [];
|
||||
const users: UserRow[] = [];
|
||||
const requirements: RequirementRow[] = [];
|
||||
const taskCategories: TaskCategoryRow[] = [];
|
||||
const versionPlans: VersionPlanRow[] = [];
|
||||
@@ -346,6 +357,21 @@ export function mapAppDataToV22Rows(appData: Record<string, unknown>): V22Mapped
|
||||
}
|
||||
}
|
||||
|
||||
const nextEmail = uniqueEmailFactory();
|
||||
for (const [index, member] of readNestedArray(appData.members, 'members').entries()) {
|
||||
const id = stringField(member, 'id') ?? `member-${index + 1}`;
|
||||
const username = stringField(member, 'username') ?? id;
|
||||
const email = normalizeMemberEmail(stringField(member, 'email'), username, id);
|
||||
users.push({
|
||||
id,
|
||||
email: nextEmail(email),
|
||||
name: stringField(member, 'name') ?? username,
|
||||
avatar: stringField(member, 'avatar'),
|
||||
createdAt: stringField(member, 'createdAt'),
|
||||
updatedAt: stringField(member, 'updatedAt') ?? stringField(member, 'createdAt'),
|
||||
});
|
||||
}
|
||||
|
||||
const defaultProductId = products[0]?.id;
|
||||
const requirementCode = scopedCodeFactory('REQ');
|
||||
for (const [index, requirement] of readNestedArray(appData.requirements, 'requirements').entries()) {
|
||||
@@ -705,6 +731,7 @@ export function mapAppDataToV22Rows(appData: Record<string, unknown>): V22Mapped
|
||||
products,
|
||||
projects,
|
||||
versions,
|
||||
users,
|
||||
requirements,
|
||||
taskCategories,
|
||||
versionPlans,
|
||||
@@ -809,6 +836,34 @@ function scopedCodeFactory(prefix: string) {
|
||||
};
|
||||
}
|
||||
|
||||
function uniqueEmailFactory() {
|
||||
const used = new Set<string>();
|
||||
return (email: string): string => {
|
||||
const normalized = email.trim().toLowerCase();
|
||||
if (!used.has(normalized)) {
|
||||
used.add(normalized);
|
||||
return normalized;
|
||||
}
|
||||
const atIndex = normalized.indexOf('@');
|
||||
const name = atIndex >= 0 ? normalized.slice(0, atIndex) : normalized;
|
||||
const domain = atIndex >= 0 ? normalized.slice(atIndex + 1) : 'local.ftb';
|
||||
let suffix = 2;
|
||||
let next = `${name}+${suffix}@${domain}`;
|
||||
while (used.has(next)) {
|
||||
suffix++;
|
||||
next = `${name}+${suffix}@${domain}`;
|
||||
}
|
||||
used.add(next);
|
||||
return next;
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeMemberEmail(email: string | undefined, username: string, id: string): string {
|
||||
if (email?.includes('@')) return email;
|
||||
const base = email || username || id;
|
||||
return `${base.trim().toLowerCase()}@local.ftb`;
|
||||
}
|
||||
|
||||
function inferProjectIdForVersion(version: AppDataRecord, projects: AppDataRecord[]): string | undefined {
|
||||
const explicitProjectId = stringField(version, 'projectId');
|
||||
if (explicitProjectId) return explicitProjectId;
|
||||
|
||||
@@ -15,6 +15,20 @@ describe('AppDataV22MigrationService', () => {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
key: 'members',
|
||||
value: {
|
||||
members: [
|
||||
{
|
||||
id: 'member-pm',
|
||||
name: 'Product manager',
|
||||
username: 'pm',
|
||||
email: '',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
key: 'requirements',
|
||||
value: {
|
||||
@@ -26,6 +40,7 @@ describe('AppDataV22MigrationService', () => {
|
||||
projectId: 'project-1',
|
||||
versionId: 'version-1',
|
||||
title: 'Customer import',
|
||||
creator: 'member-pm',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
@@ -64,10 +79,98 @@ describe('AppDataV22MigrationService', () => {
|
||||
products: 1,
|
||||
projects: 1,
|
||||
versions: 1,
|
||||
users: 1,
|
||||
requirements: 1,
|
||||
devTasks: 1,
|
||||
}),
|
||||
);
|
||||
expect(preview.skipped).toEqual([]);
|
||||
});
|
||||
|
||||
it('imports mapped rows in foreign-key-safe order with duplicate protection', async () => {
|
||||
const calls: string[] = [];
|
||||
const delegate = (name: string) => ({
|
||||
createMany: jest.fn().mockImplementation(async () => {
|
||||
calls.push(name);
|
||||
return { count: 1 };
|
||||
}),
|
||||
});
|
||||
const tx = {
|
||||
user: delegate('user'),
|
||||
product: delegate('product'),
|
||||
project: delegate('project'),
|
||||
version: delegate('version'),
|
||||
taskCategory: delegate('taskCategory'),
|
||||
requirement: delegate('requirement'),
|
||||
versionPlan: delegate('versionPlan'),
|
||||
devTask: delegate('devTask'),
|
||||
testCase: delegate('testCase'),
|
||||
bug: delegate('bug'),
|
||||
workActivity: delegate('workActivity'),
|
||||
taskWorklog: delegate('taskWorklog'),
|
||||
overtimeRecord: delegate('overtimeRecord'),
|
||||
xiaobaoRiskSnapshot: delegate('xiaobaoRiskSnapshot'),
|
||||
xiaobaoRiskInsight: delegate('xiaobaoRiskInsight'),
|
||||
xiaobaoRiskSummary: delegate('xiaobaoRiskSummary'),
|
||||
};
|
||||
const prisma = {
|
||||
$transaction: jest.fn().mockImplementation(async (callback: (client: typeof tx) => Promise<unknown>) => callback(tx)),
|
||||
appData: { findMany: jest.fn() },
|
||||
};
|
||||
const service = new AppDataV22MigrationService(prisma as any);
|
||||
const mapped = service.mapSnapshot({
|
||||
'products-overview': [
|
||||
{
|
||||
id: 'product-1',
|
||||
name: 'FTB',
|
||||
projects: [{ id: 'project-1', name: 'CRM', description: '', createdAt: '2026-01-01T00:00:00.000Z' }],
|
||||
versions: [{ id: 'version-1', name: 'CRM V1.0', createdAt: '2026-01-01T00:00:00.000Z' }],
|
||||
},
|
||||
],
|
||||
members: {
|
||||
members: [{ id: 'member-pm', name: 'Product manager', username: 'pm', email: '', createdAt: '2026-01-01' }],
|
||||
},
|
||||
'task-categories': [
|
||||
{
|
||||
id: 'cat-fe',
|
||||
code: 'frontend_development',
|
||||
name: 'Frontend development',
|
||||
group: 'development',
|
||||
isSystem: true,
|
||||
},
|
||||
],
|
||||
requirements: {
|
||||
requirements: [
|
||||
{
|
||||
id: 'req-1',
|
||||
code: 'REQ-001',
|
||||
productId: 'product-1',
|
||||
projectId: 'project-1',
|
||||
versionId: 'version-1',
|
||||
title: 'Customer import',
|
||||
creator: 'member-pm',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const result = await service.importMappedRows(mapped);
|
||||
|
||||
expect(result.readyToImport).toBe(true);
|
||||
expect(result.inserted).toEqual(expect.objectContaining({ users: 1, products: 1, requirements: 1 }));
|
||||
expect(calls.slice(0, 6)).toEqual(['user', 'product', 'project', 'version', 'taskCategory', 'requirement']);
|
||||
expect(tx.user.createMany).toHaveBeenCalledWith({
|
||||
data: [expect.objectContaining({
|
||||
id: 'member-pm',
|
||||
email: 'pm@local.ftb',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
})],
|
||||
skipDuplicates: true,
|
||||
});
|
||||
expect(tx.requirement.createMany).toHaveBeenCalledWith({
|
||||
data: [expect.objectContaining({ creatorId: 'member-pm' })],
|
||||
skipDuplicates: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
import { APP_DATA_KEYS } from '../data/data-keys';
|
||||
import { mapAppDataToV22Rows, type V22MappedRows } from './app-data-v22.mapper';
|
||||
|
||||
const COUNT_KEYS = [
|
||||
'users',
|
||||
'products',
|
||||
'projects',
|
||||
'versions',
|
||||
'requirements',
|
||||
'taskCategories',
|
||||
'requirements',
|
||||
'versionPlans',
|
||||
'devTasks',
|
||||
'testCases',
|
||||
@@ -23,6 +24,72 @@ const COUNT_KEYS = [
|
||||
] as const;
|
||||
|
||||
type CountKey = (typeof COUNT_KEYS)[number];
|
||||
type DelegateName =
|
||||
| 'user'
|
||||
| 'product'
|
||||
| 'project'
|
||||
| 'version'
|
||||
| 'taskCategory'
|
||||
| 'requirement'
|
||||
| 'versionPlan'
|
||||
| 'devTask'
|
||||
| 'testCase'
|
||||
| 'bug'
|
||||
| 'workActivity'
|
||||
| 'taskWorklog'
|
||||
| 'overtimeRecord'
|
||||
| 'xiaobaoRiskSnapshot'
|
||||
| 'xiaobaoRiskInsight'
|
||||
| 'xiaobaoRiskSummary';
|
||||
|
||||
type CreateManyDelegate = {
|
||||
createMany(args: { data: unknown[]; skipDuplicates: boolean }): Promise<{ count: number }>;
|
||||
};
|
||||
|
||||
type MigrationTransaction = Record<DelegateName, CreateManyDelegate>;
|
||||
|
||||
const DELEGATE_BY_COUNT_KEY: Record<CountKey, DelegateName> = {
|
||||
users: 'user',
|
||||
products: 'product',
|
||||
projects: 'project',
|
||||
versions: 'version',
|
||||
taskCategories: 'taskCategory',
|
||||
requirements: 'requirement',
|
||||
versionPlans: 'versionPlan',
|
||||
devTasks: 'devTask',
|
||||
testCases: 'testCase',
|
||||
bugs: 'bug',
|
||||
workActivities: 'workActivity',
|
||||
taskWorklogs: 'taskWorklog',
|
||||
overtimeRecords: 'overtimeRecord',
|
||||
xiaobaoRiskSnapshots: 'xiaobaoRiskSnapshot',
|
||||
xiaobaoRiskInsights: 'xiaobaoRiskInsight',
|
||||
xiaobaoRiskSummaries: 'xiaobaoRiskSummary',
|
||||
aiLogs: 'user',
|
||||
};
|
||||
|
||||
const DATE_TIME_KEYS = new Set([
|
||||
'createdAt',
|
||||
'updatedAt',
|
||||
'releaseDate',
|
||||
'expectedStartAt',
|
||||
'expectedEndAt',
|
||||
'actualStartAt',
|
||||
'completedAt',
|
||||
'startDate',
|
||||
'plannedTestAt',
|
||||
'plannedEndAt',
|
||||
'startedAt',
|
||||
'plannedFixAt',
|
||||
'resolvedAt',
|
||||
'closedAt',
|
||||
'startAt',
|
||||
'endAt',
|
||||
'occurredAt',
|
||||
'aiDraftAt',
|
||||
'recomputedAt',
|
||||
'forecastReleaseDate',
|
||||
]);
|
||||
|
||||
export interface AppDataV22MigrationPreview {
|
||||
readyToImport: boolean;
|
||||
@@ -30,6 +97,10 @@ export interface AppDataV22MigrationPreview {
|
||||
skipped: V22MappedRows['skipped'];
|
||||
}
|
||||
|
||||
export interface AppDataV22MigrationImportResult extends AppDataV22MigrationPreview {
|
||||
inserted: Record<CountKey, number>;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class AppDataV22MigrationService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
@@ -54,8 +125,65 @@ export class AppDataV22MigrationService {
|
||||
skipped: mapped.skipped,
|
||||
};
|
||||
}
|
||||
|
||||
async importCurrentAppData(): Promise<AppDataV22MigrationImportResult> {
|
||||
return this.importMappedRows(this.mapSnapshot(await this.loadSnapshot()));
|
||||
}
|
||||
|
||||
async importMappedRows(mapped: V22MappedRows): Promise<AppDataV22MigrationImportResult> {
|
||||
if (mapped.skipped.length > 0) {
|
||||
throw new BadRequestException({
|
||||
code: 'APP_DATA_V22_MIGRATION_HAS_SKIPPED_ROWS',
|
||||
message: 'Resolve skipped AppData rows before importing V2.2 relation tables.',
|
||||
skipped: mapped.skipped,
|
||||
});
|
||||
}
|
||||
|
||||
const inserted = await this.prisma.$transaction(async (tx) => {
|
||||
const client = tx as unknown as MigrationTransaction;
|
||||
const result = emptyCounts();
|
||||
for (const key of COUNT_KEYS) {
|
||||
if (key === 'aiLogs') continue;
|
||||
result[key] = await createMany(client[DELEGATE_BY_COUNT_KEY[key]], mapped[key]);
|
||||
}
|
||||
return result;
|
||||
});
|
||||
|
||||
return {
|
||||
readyToImport: true,
|
||||
counts: countRows(mapped),
|
||||
skipped: [],
|
||||
inserted,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function countRows(mapped: V22MappedRows): Record<CountKey, number> {
|
||||
return Object.fromEntries(COUNT_KEYS.map((key) => [key, mapped[key].length])) as Record<CountKey, number>;
|
||||
}
|
||||
|
||||
function emptyCounts(): Record<CountKey, number> {
|
||||
return Object.fromEntries(COUNT_KEYS.map((key) => [key, 0])) as Record<CountKey, number>;
|
||||
}
|
||||
|
||||
async function createMany(delegate: CreateManyDelegate, data: unknown[]): Promise<number> {
|
||||
if (data.length === 0) return 0;
|
||||
const result = await delegate.createMany({ data: data.map(normalizeDateTimeFields), skipDuplicates: true });
|
||||
return result.count;
|
||||
}
|
||||
|
||||
function normalizeDateTimeFields(row: unknown): unknown {
|
||||
if (typeof row !== 'object' || row === null || Array.isArray(row)) return row;
|
||||
return Object.fromEntries(
|
||||
Object.entries(row).map(([key, value]) => [
|
||||
key,
|
||||
DATE_TIME_KEYS.has(key) ? normalizeDateTimeValue(value) : value,
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
function normalizeDateTimeValue(value: unknown): unknown {
|
||||
if (typeof value !== 'string') return value;
|
||||
if (/^\d{4}-\d{2}-\d{2}$/.test(value)) return `${value}T00:00:00.000Z`;
|
||||
return value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user