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) {} +}