Skip to content

Commit

Permalink
chore: update name in one shot (#7567)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Jul 22, 2024
1 parent 55db9f9 commit 7d7399a
Showing 1 changed file with 6 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 7d7399a

Please sign in to comment.