From d5e4a1b1adf24d9154c2ee2e1c6f8baf5a02578b Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Thu, 24 Aug 2023 13:51:46 +0200 Subject: [PATCH] fix: Soft delete existing associations of soft deleted users --- ...t_delete_soft_deleted_user_associations.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 packages/backend/src/db/migrations/20230824105813_soft_delete_soft_deleted_user_associations.ts diff --git a/packages/backend/src/db/migrations/20230824105813_soft_delete_soft_deleted_user_associations.ts b/packages/backend/src/db/migrations/20230824105813_soft_delete_soft_deleted_user_associations.ts new file mode 100644 index 00000000..dd69a3d8 --- /dev/null +++ b/packages/backend/src/db/migrations/20230824105813_soft_delete_soft_deleted_user_associations.ts @@ -0,0 +1,35 @@ +import { Knex } from 'knex'; + +export async function up(knex: Knex): Promise { + const users = await knex('users').whereNotNull('deleted_at'); + const userIds = users.map((user) => user.id); + + const flows = await knex('flows').whereIn('user_id', userIds); + const flowIds = flows.map((flow) => flow.id); + const executions = await knex('executions').whereIn('flow_id', flowIds); + const executionIds = executions.map((execution) => execution.id); + + await knex('execution_steps').whereIn('execution_id', executionIds).update({ + deleted_at: knex.fn.now(), + }); + + await knex('executions').whereIn('id', executionIds).update({ + deleted_at: knex.fn.now(), + }); + + await knex('steps').whereIn('flow_id', flowIds).update({ + deleted_at: knex.fn.now(), + }); + + await knex('flows').whereIn('id', flowIds).update({ + deleted_at: knex.fn.now(), + }); + + await knex('connections').whereIn('user_id', userIds).update({ + deleted_at: knex.fn.now(), + }); +} + +export async function down(): Promise { + // void +}