fix: large transaction (#6695)

This commit is contained in:
darkskygit
2024-04-25 08:33:20 +00:00
parent 74b7d024be
commit ea3f427918
2 changed files with 77 additions and 73 deletions

View File

@@ -593,37 +593,35 @@ export const prompts: Prompt[] = [
]; ];
export async function refreshPrompts(db: PrismaClient) { export async function refreshPrompts(db: PrismaClient) {
await db.$transaction(async tx => { for (const prompt of prompts) {
for (const prompt of prompts) { await db.aiPrompt.upsert({
await tx.aiPrompt.upsert({ create: {
create: { name: prompt.name,
name: prompt.name, action: prompt.action,
action: prompt.action, model: prompt.model,
model: prompt.model, messages: {
messages: { create: prompt.messages.map((message, idx) => ({
create: prompt.messages.map((message, idx) => ({ idx,
idx, role: message.role,
role: message.role, content: message.content,
content: message.content, params: message.params,
params: message.params, })),
})),
},
}, },
where: { name: prompt.name }, },
update: { where: { name: prompt.name },
action: prompt.action, update: {
model: prompt.model, action: prompt.action,
messages: { model: prompt.model,
deleteMany: {}, messages: {
create: prompt.messages.map((message, idx) => ({ deleteMany: {},
idx, create: prompt.messages.map((message, idx) => ({
role: message.role, idx,
content: message.content, role: message.role,
params: message.params, content: message.content,
})), params: message.params,
}, })),
}, },
}); },
} });
}); }
} }

View File

@@ -13,56 +13,62 @@ export async function upgradeQuotaVersion(
// add new quota // add new quota
await upsertFeature(db, quota); await upsertFeature(db, quota);
// migrate all users that using old quota to new quota // migrate all users that using old quota to new quota
await db.$transaction(async tx => { await db.$transaction(
const latestQuotaVersion = await tx.features.findFirstOrThrow({ async tx => {
where: { feature: quota.feature }, const latestQuotaVersion = await tx.features.findFirstOrThrow({
orderBy: { version: 'desc' }, where: { feature: quota.feature },
select: { id: true }, orderBy: { version: 'desc' },
}); select: { id: true },
});
// find all users that have old free plan // find all users that have old free plan
const userIds = await tx.user.findMany({ const userIds = await tx.user.findMany({
where: { where: {
features: { features: {
some: { some: {
feature: { feature: {
type: FeatureKind.Quota, type: FeatureKind.Quota,
feature: quota.feature, feature: quota.feature,
version: { lt: quota.version }, version: { lt: quota.version },
},
activated: true,
}, },
activated: true,
}, },
}, },
}, select: { id: true },
select: { id: true }, });
});
// deactivate all old quota for the user // deactivate all old quota for the user
await tx.userFeatures.updateMany({ await tx.userFeatures.updateMany({
where: { where: {
id: undefined, id: undefined,
userId: { userId: {
in: userIds.map(({ id }) => id), in: userIds.map(({ id }) => id),
},
feature: {
type: FeatureKind.Quota,
},
activated: true,
}, },
feature: { data: {
type: FeatureKind.Quota, activated: false,
}, },
activated: true, });
},
data: {
activated: false,
},
});
await tx.userFeatures.createMany({ await tx.userFeatures.createMany({
data: userIds.map(({ id: userId }) => ({ data: userIds.map(({ id: userId }) => ({
userId, userId,
featureId: latestQuotaVersion.id, featureId: latestQuotaVersion.id,
reason, reason,
activated: true, activated: true,
})), })),
}); });
}); },
{
maxWait: 10000,
timeout: 20000,
}
);
} }
export async function upsertLatestQuotaVersion( export async function upsertLatestQuotaVersion(