fix(plugin-workflow): fix migration (#6667)

* fix(plugin-workflow): fix migration

* fix(plugin-workflow): change findOrCreate to simple logic
This commit is contained in:
Junyi 2025-04-15 21:31:05 +08:00 committed by GitHub
parent 9199061c5a
commit bc519a118c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 9 deletions

View File

@ -115,11 +115,13 @@ export default class PluginWorkflowServer extends Plugin {
private onAfterCreate = async (model: WorkflowModel, { transaction }) => { private onAfterCreate = async (model: WorkflowModel, { transaction }) => {
const WorkflowStatsModel = this.db.getModel('workflowStats'); const WorkflowStatsModel = this.db.getModel('workflowStats');
const [stats, created] = await WorkflowStatsModel.findOrCreate({ let stats = await WorkflowStatsModel.findOne({
where: { key: model.key }, where: { key: model.key },
defaults: { key: model.key },
transaction, transaction,
}); });
if (!stats) {
stats = await model.createStats({ executed: 0 }, { transaction });
}
model.stats = stats; model.stats = stats;
model.versionStats = await model.createVersionStats({ id: model.id }, { transaction }); model.versionStats = await model.createVersionStats({ id: model.id }, { transaction });
if (model.enabled) { if (model.enabled) {

View File

@ -26,16 +26,21 @@ export default class extends Migration {
const groupCounts: { [key: string]: { key: string; executed: number } } = {}; const groupCounts: { [key: string]: { key: string; executed: number } } = {};
for (const workflow of workflows) { for (const workflow of workflows) {
await WorkflowVersionStatsModel.findOrCreate({ const versionStats = await WorkflowVersionStatsModel.findOne({
where: { where: {
id: workflow.id, id: workflow.id,
}, },
defaults: {
id: workflow.id,
executed: workflow.get('executed'),
},
transaction, transaction,
}); });
if (!versionStats) {
await WorkflowVersionStatsModel.create(
{
id: workflow.id,
executed: workflow.get('executed'),
},
{ transaction },
);
}
const key = workflow.get('key'); const key = workflow.get('key');
groupCounts[key] = { groupCounts[key] = {
@ -44,13 +49,15 @@ export default class extends Migration {
}; };
} }
for (const values of Object.values(groupCounts)) { for (const values of Object.values(groupCounts)) {
await WorkflowStatsModel.findOrCreate({ const stats = await WorkflowStatsModel.findOne({
where: { where: {
key: values.key, key: values.key,
}, },
defaults: values,
transaction, transaction,
}); });
if (!stats) {
await WorkflowStatsModel.create(values, { transaction });
}
} }
}); });
} }