fix(server): wrong data migration (#4855)

This commit is contained in:
liuyi
2023-11-07 17:20:42 +08:00
committed by GitHub
parent b99ac51624
commit 7305530d97
2 changed files with 44 additions and 28 deletions

View File

@@ -64,28 +64,36 @@ export class RunCommand extends CommandRunner {
continue; continue;
} }
this.logger.log(`Running ${migration.name}...`);
const record = await this.db.dataMigration.create({
data: {
name: migration.name,
startedAt: new Date(),
},
});
try { try {
this.logger.log(`Running ${migration.name}...`);
const record = await this.db.dataMigration.create({
data: {
name: migration.name,
startedAt: new Date(),
},
});
await migration.up(this.db); await migration.up(this.db);
await this.db.dataMigration.update({ } catch (e) {
await this.db.dataMigration.delete({
where: { where: {
id: record.id, id: record.id,
}, },
data: {
finishedAt: new Date(),
},
}); });
done.push(migration); await migration.down(this.db);
} catch (e) {
this.logger.error('Failed to run data migration', e); this.logger.error('Failed to run data migration', e);
process.exit(1); process.exit(1);
} }
await this.db.dataMigration.update({
where: {
id: record.id,
},
data: {
finishedAt: new Date(),
},
});
done.push(migration);
} }
this.logger.log(`Done ${done.length} migrations`); this.logger.log(`Done ${done.length} migrations`);

View File

@@ -3,8 +3,8 @@ import { PrismaService } from '../../prisma';
export class PagePermission1699005339766 { export class PagePermission1699005339766 {
// do the migration // do the migration
static async up(db: PrismaService) { static async up(db: PrismaService) {
const turn = 0; let turn = 0;
const lastTurnCount = 50; let lastTurnCount = 50;
const done = new Set<string>(); const done = new Set<string>();
while (lastTurnCount === 50) { while (lastTurnCount === 50) {
@@ -15,6 +15,7 @@ export class PagePermission1699005339766 {
createdAt: 'asc', createdAt: 'asc',
}, },
}); });
lastTurnCount = workspaces.length;
for (const workspace of workspaces) { for (const workspace of workspaces) {
if (done.has(workspace.id)) { if (done.has(workspace.id)) {
@@ -58,17 +59,21 @@ export class PagePermission1699005339766 {
}); });
if (!existed) { if (!existed) {
await db.workspaceUserPermission.create({ await db.workspaceUserPermission
select: null, .create({
data: { select: null,
// this id is used at invite email, should keep data: {
id: oldPermission.id, // this id is used at invite email, should keep
workspaceId: oldPermission.workspaceId, id: oldPermission.id,
userId: oldPermission.userId, workspaceId: oldPermission.workspaceId,
type: oldPermission.type, userId: oldPermission.userId,
accepted: oldPermission.accepted, type: oldPermission.type,
}, accepted: oldPermission.accepted,
}); },
})
.catch(() => {
// duplicated
});
} }
} else { } else {
// ignore wrong data // ignore wrong data
@@ -77,11 +82,14 @@ export class PagePermission1699005339766 {
done.add(workspace.id); done.add(workspace.id);
} }
turn++;
} }
} }
// revert the migration // revert the migration
static async down() { static async down(db: PrismaService) {
// await db.workspaceUserPermission.deleteMany({});
await db.workspacePageUserPermission.deleteMany({});
} }
} }