mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00:00
fix: large transaction (#6695)
This commit is contained in:
@@ -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,
|
||||
})),
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user