Skip to content

Commit

Permalink
fix: fixed too many sql variables issue
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Oct 29, 2024
1 parent a42650b commit f4cbef9
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions helpers/get-database.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,25 +752,39 @@ async function getDatabase(
}
}

// NOTE: this only works if there's at least one hash from existing messages
// (otherwise to do it for all we'd just remove this conditional check)
// TODO: <https://github.com/nodemailer/wildduck/issues/750>
if (hashSet.size > 0) {
// TODO: rewrite this to avoid "Too many variables" error
// iterate over all attachments from the past
// and if the hash is not in the array then remove it
const sql = builder.build({
type: 'remove',
type: 'select',
table: 'Attachments',
condition: {
created_at: {
$lte: now
},
counterUpdated: {
$lte: now
},
hash: {
$nin: [...hashSet]
}
}
created_at: { $lte: now },
counterUpdated: { $lte: now }
},
fields: ['hash']
});
db.prepare(sql.query).run(sql.values);

const existingHashes = db.prepare(sql.query).pluck().all(sql.values);

db.transaction((hashes) => {
for (const hash of hashes) {
if (hashSet.has(hash)) continue;
const sql = builder.build({
type: 'remove',
table: 'Attachments',
condition: {
created_at: { $lte: now },
counterUpdated: { $lte: now },
hash
}
});
db.prepare(sql.query).run(sql.values);
}
}).immediate(existingHashes);
}
} catch (err) {
logger.fatal(err, { session });
Expand Down

0 comments on commit f4cbef9

Please sign in to comment.