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,7 +64,6 @@ export class RunCommand extends CommandRunner {
continue; continue;
} }
try {
this.logger.log(`Running ${migration.name}...`); this.logger.log(`Running ${migration.name}...`);
const record = await this.db.dataMigration.create({ const record = await this.db.dataMigration.create({
data: { data: {
@@ -72,7 +71,20 @@ export class RunCommand extends CommandRunner {
startedAt: new Date(), startedAt: new Date(),
}, },
}); });
try {
await migration.up(this.db); await migration.up(this.db);
} catch (e) {
await this.db.dataMigration.delete({
where: {
id: record.id,
},
});
await migration.down(this.db);
this.logger.error('Failed to run data migration', e);
process.exit(1);
}
await this.db.dataMigration.update({ await this.db.dataMigration.update({
where: { where: {
id: record.id, id: record.id,
@@ -82,10 +94,6 @@ export class RunCommand extends CommandRunner {
}, },
}); });
done.push(migration); done.push(migration);
} catch (e) {
this.logger.error('Failed to run data migration', e);
process.exit(1);
}
} }
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,7 +59,8 @@ export class PagePermission1699005339766 {
}); });
if (!existed) { if (!existed) {
await db.workspaceUserPermission.create({ await db.workspaceUserPermission
.create({
select: null, select: null,
data: { data: {
// this id is used at invite email, should keep // this id is used at invite email, should keep
@@ -68,6 +70,9 @@ export class PagePermission1699005339766 {
type: oldPermission.type, type: oldPermission.type,
accepted: oldPermission.accepted, accepted: oldPermission.accepted,
}, },
})
.catch(() => {
// duplicated
}); });
} }
} else { } else {
@@ -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({});
} }
} }