Skip to content

Commit 39df23f

Browse files
authored
fix: template base losing link record (#2358)
relative issueId: T1491
1 parent 83e2c1f commit 39df23f

File tree

3 files changed

+659
-12
lines changed

3 files changed

+659
-12
lines changed

apps/nestjs-backend/src/features/base/base-duplicate.service.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,14 +462,14 @@ export class BaseDuplicateService {
462462
const prisma = this.prismaService.txClient();
463463
const tableId2DbTableNameMap: Record<string, string> = {};
464464
const allTableId = Object.keys(tableIdMap).concat(Object.values(tableIdMap));
465-
const sourceTableRaws = await this.prismaService.txClient().tableMeta.findMany({
465+
const sourceTableRaws = await prisma.tableMeta.findMany({
466466
where: { id: { in: allTableId }, deletedTime: null },
467467
select: {
468468
id: true,
469469
dbTableName: true,
470470
},
471471
});
472-
const targetTableRaws = await this.prismaService.txClient().tableMeta.findMany({
472+
const targetTableRaws = await prisma.tableMeta.findMany({
473473
where: { id: { in: allTableId }, deletedTime: null },
474474
select: {
475475
id: true,
@@ -503,9 +503,10 @@ export class BaseDuplicateService {
503503
}[];
504504

505505
// delete foreign keys if(exist) then duplicate table data
506+
// 1. collect foreign keys info
506507
for (const dbTableName of dbTableNames) {
507508
const foreignKeysInfoSql = this.dbProvider.getForeignKeysInfo(dbTableName);
508-
const foreignKeysInfo = await this.prismaService.txClient().$queryRawUnsafe<
509+
const foreignKeysInfo = await prisma.$queryRawUnsafe<
509510
{
510511
constraint_name: string;
511512
column_name: string;
@@ -521,6 +522,7 @@ export class BaseDuplicateService {
521522
allForeignKeyInfos.push(...newForeignKeyInfos);
522523
}
523524

525+
// 2. drop foreign keys
524526
for (const { constraint_name, column_name, dbTableName } of allForeignKeyInfos) {
525527
const dropForeignKeyQuery = this.knex.schema
526528
.alterTable(dbTableName, (table) => {
@@ -531,6 +533,7 @@ export class BaseDuplicateService {
531533
await prisma.$executeRawUnsafe(dropForeignKeyQuery);
532534
}
533535

536+
// 3. duplicate table data
534537
for (const tableId of oldTableId) {
535538
const newTableId = tableIdMap[tableId];
536539
const oldDbTableName = tableId2DbTableNameMap[tableId];
@@ -544,6 +547,7 @@ export class BaseDuplicateService {
544547
);
545548
}
546549

550+
// 4. repair foreign keys
547551
for (const {
548552
constraint_name: constraintName,
549553
column_name: columnName,

apps/nestjs-backend/src/features/table/table-duplicate.service.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,21 @@ export class TableDuplicateService {
160160

161161
const newColumnsInfoQuery = this.dbProvider.columnInfo(targetDbTableName);
162162

163-
const oldOriginColumns = (await prisma.$queryRawUnsafe<{ name: string }[]>(columnInfoQuery))
164-
.map(({ name }) => name)
165-
.filter((name) =>
166-
crossBaseLinkInfo
167-
.map(({ selfKeyName }) => selfKeyName)
168-
.filter((selfKeyName) => selfKeyName !== '__id' && selfKeyName)
169-
.includes(name)
170-
);
163+
const allSourceColumns = (
164+
await prisma.$queryRawUnsafe<{ name: string }[]>(columnInfoQuery)
165+
).map(({ name }) => name);
166+
167+
// Only filter by crossBaseLinkInfo if it's not empty
168+
// When crossBaseLinkInfo is empty (normal table duplication), include all columns
169+
const oldOriginColumns =
170+
crossBaseLinkInfo.length === 0
171+
? allSourceColumns
172+
: allSourceColumns.filter((name) =>
173+
crossBaseLinkInfo
174+
.map(({ selfKeyName }) => selfKeyName)
175+
.filter((selfKeyName) => selfKeyName !== '__id' && selfKeyName)
176+
.includes(name)
177+
);
171178

172179
const crossBaseLinkDbFieldNames = crossBaseLinkInfo.map(
173180
({ dbFieldName, isMultipleCellValue }) => ({
@@ -239,7 +246,7 @@ export class TableDuplicateService {
239246
'__last_modified_by',
240247
];
241248

242-
const excludeFields = await this.prismaService.txClient().field.findMany({
249+
const excludeFields = await prisma.field.findMany({
243250
where: {
244251
id: {
245252
in: Object.keys(sourceToTargetFieldMap),

0 commit comments

Comments
 (0)