feat(server): user connected accounts migration (#6103)
This commit is contained in:
@@ -0,0 +1,39 @@
|
|||||||
|
import { PrismaClient } from '@prisma/client';
|
||||||
|
|
||||||
|
import { loop } from './utils/loop';
|
||||||
|
|
||||||
|
export class Oauth1710319359062 {
|
||||||
|
// do the migration
|
||||||
|
static async up(db: PrismaClient) {
|
||||||
|
await loop(async (skip, take) => {
|
||||||
|
const oldRecords = await db.deprecatedNextAuthAccount.findMany({
|
||||||
|
skip,
|
||||||
|
take,
|
||||||
|
orderBy: {
|
||||||
|
providerAccountId: 'asc',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await db.connectedAccount.createMany({
|
||||||
|
data: oldRecords.map(record => ({
|
||||||
|
userId: record.userId,
|
||||||
|
provider: record.provider,
|
||||||
|
scope: record.scope,
|
||||||
|
providerAccountId: record.providerAccountId,
|
||||||
|
accessToken: record.access_token,
|
||||||
|
refreshToken: record.refresh_token,
|
||||||
|
expiresAt: record.expires_at
|
||||||
|
? new Date(record.expires_at * 1000)
|
||||||
|
: null,
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
|
||||||
|
return oldRecords.length;
|
||||||
|
}, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// revert the migration
|
||||||
|
static async down(db: PrismaClient) {
|
||||||
|
await db.connectedAccount.deleteMany({});
|
||||||
|
}
|
||||||
|
}
|
||||||
13
packages/backend/server/src/data/migrations/utils/loop.ts
Normal file
13
packages/backend/server/src/data/migrations/utils/loop.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
export async function loop(
|
||||||
|
batchFn: (skip: number, take: number) => Promise<number>,
|
||||||
|
chunkSize: number = 100
|
||||||
|
) {
|
||||||
|
let turn = 0;
|
||||||
|
let last = chunkSize;
|
||||||
|
|
||||||
|
while (last === chunkSize) {
|
||||||
|
last = await batchFn(chunkSize * turn, chunkSize);
|
||||||
|
|
||||||
|
turn++;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -160,8 +160,17 @@ export class OAuthController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await this.user.fulfillUser(externalAccount.email, {
|
await this.user.fulfillUser(externalAccount.email, {
|
||||||
|
emailVerifiedAt: new Date(),
|
||||||
registered: true,
|
registered: true,
|
||||||
});
|
});
|
||||||
|
await this.db.connectedAccount.create({
|
||||||
|
data: {
|
||||||
|
userId: user.id,
|
||||||
|
provider,
|
||||||
|
providerAccountId: externalAccount.id,
|
||||||
|
...tokens,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
} else {
|
} else {
|
||||||
@@ -191,7 +200,7 @@ export class OAuthController {
|
|||||||
) {
|
) {
|
||||||
return this.user.createUser({
|
return this.user.createUser({
|
||||||
email: externalAccount.email,
|
email: externalAccount.email,
|
||||||
name: 'Unnamed',
|
name: externalAccount.email.split('@')[0],
|
||||||
avatarUrl: externalAccount.avatarUrl,
|
avatarUrl: externalAccount.avatarUrl,
|
||||||
emailVerifiedAt: new Date(),
|
emailVerifiedAt: new Date(),
|
||||||
connectedAccounts: {
|
connectedAccounts: {
|
||||||
|
|||||||
Reference in New Issue
Block a user