diff --git a/server/src/scripts/delete-memberships-of-deleted-users.js b/server/src/scripts/delete-memberships-of-deleted-users.js new file mode 100644 index 000000000..73542c350 --- /dev/null +++ b/server/src/scripts/delete-memberships-of-deleted-users.js @@ -0,0 +1,17 @@ +// @flow + +import { dbQuery, SQL } from '../database/database'; +import { main } from './utils'; + +async function deleteMemberships() { + const query = SQL` + DELETE m + FROM memberships m + LEFT JOIN users u ON u.id = m.user + WHERE m.role = -1 AND u.id IS NULL + `; + + await dbQuery(query); +} + +main([deleteMemberships]); diff --git a/server/src/scripts/utils.js b/server/src/scripts/utils.js index 4a9062abc..33b91c986 100644 --- a/server/src/scripts/utils.js +++ b/server/src/scripts/utils.js @@ -1,14 +1,26 @@ // @flow import { getPool } from '../database/database'; import { endFirebase, endAPNs } from '../push/utils'; import { publisher } from '../socket/redis'; function endScript() { getPool().end(); publisher.end(); endFirebase(); endAPNs(); } -export { endScript }; +async function main(functions: $ReadOnlyArray<() => Promise>) { + try { + for (const f of functions) { + await f(); + } + } catch (e) { + console.warn(e); + } finally { + endScript(); + } +} + +export { endScript, main };