Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

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

liuyi7305530d9715
diff --git a/packages/backend/server/src/data/commands/run.ts b/packages/backend/server/src/data/commands/run.ts
index 5168d9b493..f58c655fca 100644
--- a/packages/backend/server/src/data/commands/run.ts
+++ b/packages/backend/server/src/data/commands/run.ts
@@ -64,28 +64,36 @@ export class RunCommand extends CommandRunner {
continue;
}
+ this.logger.log(`Running ${migration.name}...`);
+ const record = await this.db.dataMigration.create({
+ data: {
+ name: migration.name,
+ startedAt: new Date(),
+ },
+ });
+
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 this.db.dataMigration.update({
+ } catch (e) {
+ await this.db.dataMigration.delete({
where: {
id: record.id,
},
- data: {
- finishedAt: new Date(),
- },
});
- done.push(migration);
- } catch (e) {
+ await migration.down(this.db);
this.logger.error('Failed to run data migration', e);
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`);
diff --git a/packages/backend/server/src/data/migrations/1699005339766-page-permission.ts b/packages/backend/server/src/data/migrations/1699005339766-page-permission.ts
index 86c9afc42d..ae8c5620fd 100644
--- a/packages/backend/server/src/data/migrations/1699005339766-page-permission.ts
+++ b/packages/backend/server/src/data/migrations/1699005339766-page-permission.ts
@@ -3,8 +3,8 @@ import { PrismaService } from '../../prisma';
export class PagePermission1699005339766 {
// do the migration
static async up(db: PrismaService) {
- const turn = 0;
- const lastTurnCount = 50;
+ let turn = 0;
+ let lastTurnCount = 50;
const done = new Set<string>();
while (lastTurnCount === 50) {
@@ -15,6 +15,7 @@ export class PagePermission1699005339766 {
createdAt: 'asc',
},
});
+ lastTurnCount = workspaces.length;
for (const workspace of workspaces) {
if (done.has(workspace.id)) {
@@ -58,17 +59,21 @@ export class PagePermission1699005339766 {
});
if (!existed) {
- await db.workspaceUserPermission.create({
- select: null,
- data: {
- // this id is used at invite email, should keep
- id: oldPermission.id,
- workspaceId: oldPermission.workspaceId,
- userId: oldPermission.userId,
- type: oldPermission.type,
- accepted: oldPermission.accepted,
- },
- });
+ await db.workspaceUserPermission
+ .create({
+ select: null,
+ data: {
+ // this id is used at invite email, should keep
+ id: oldPermission.id,
+ workspaceId: oldPermission.workspaceId,
+ userId: oldPermission.userId,
+ type: oldPermission.type,
+ accepted: oldPermission.accepted,
+ },
+ })
+ .catch(() => {
+ // duplicated
+ });
}
} else {
// ignore wrong data
@@ -77,11 +82,14 @@ export class PagePermission1699005339766 {
done.add(workspace.id);
}
+
+ turn++;
}
}
// revert the migration
- static async down() {
- //
+ static async down(db: PrismaService) {
+ await db.workspaceUserPermission.deleteMany({});
+ await db.workspacePageUserPermission.deleteMany({});
}
}