Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update name in one shot #7567

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading