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