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) {
await db.$transaction(async tx => {
for (const prompt of prompts) {
await tx.aiPrompt.upsert({
create: {
name: prompt.name,
action: prompt.action,
model: prompt.model,
messages: {
create: prompt.messages.map((message, idx) => ({
idx,
role: message.role,
content: message.content,
params: message.params,
})),
},
for (const prompt of prompts) {
await db.aiPrompt.upsert({
create: {
name: prompt.name,
action: prompt.action,
model: prompt.model,
messages: {
create: prompt.messages.map((message, idx) => ({
idx,
role: message.role,
content: message.content,
params: message.params,
})),
},
where: { name: prompt.name },
update: {
action: prompt.action,
model: prompt.model,
messages: {
deleteMany: {},
create: prompt.messages.map((message, idx) => ({
idx,
role: message.role,
content: message.content,
params: message.params,
})),
},
},
where: { name: prompt.name },
update: {
action: prompt.action,
model: prompt.model,
messages: {
deleteMany: {},
create: prompt.messages.map((message, idx) => ({
idx,
role: message.role,
content: message.content,
params: message.params,
})),
},
});
}
});
},
});
}
}

View File

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