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.

feat: add migration for rename unamed accounts (#5434)

DarkSkyb3dbac3d4c6a
diff --git a/packages/backend/server/src/data/migrations/1703756315970-unamed-account.ts b/packages/backend/server/src/data/migrations/1703756315970-unamed-account.ts
new file mode 100644
index 0000000000..bd8b7b8d4c
--- /dev/null
+++ b/packages/backend/server/src/data/migrations/1703756315970-unamed-account.ts
@@ -0,0 +1,31 @@
+import type { UserType } from '../../modules/users';
+import { PrismaService } from '../../prisma';
+
+export class UnamedAccount1703756315970 {
+ // do the migration
+ static async up(db: PrismaService) {
+ await db.$transaction(async tx => {
+ // only find users with empty names
+ const users = await db.$queryRaw<
+ UserType[]
+ >`SELECT * FROM users WHERE name ~ E'^[\\s\\u2000-\\u200F]*$';`;
+ console.log(
+ `renaming ${users.map(({ email }) => email).join('|')} users`
+ );
+
+ await Promise.all(
+ users.map(({ id, email }) =>
+ tx.user.update({
+ where: { id },
+ data: {
+ name: email.split('@')[0],
+ },
+ })
+ )
+ );
+ });
+ }
+
+ // revert the migration
+ static async down(_db: PrismaService) {}
+}