chore: update name in one shot (#7567)

This commit is contained in:
darkskygit
2024-07-22 11:03:41 +00:00
parent 55db9f9719
commit 7d7399a9eb

View File

@@ -1,25 +1,13 @@
import { PrismaClient, User } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
export class RefreshUnnamedUser1721299086340 { export class RefreshUnnamedUser1721299086340 {
// do the migration // do the migration
static async up(db: PrismaClient) { static async up(db: PrismaClient) {
await db.$transaction(async tx => { await db.$executeRaw`
// only find users with unnamed names UPDATE users
const users = await db.$queryRaw< SET name = split_part(email, '@', 1)
User[] WHERE name = 'Unnamed' AND position('@' in email) > 0;
>`SELECT * FROM users WHERE name = 'Unnamed';`; `;
await Promise.all(
users.map(({ id, email }) =>
tx.user.update({
where: { id },
data: {
name: email.split('@')[0],
},
})
)
);
});
} }
// revert the migration // revert the migration