From cbc41c0d992c862bd9b0c292bbbee325e0fa9b15 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Tue, 5 Aug 2025 00:07:47 +0200 Subject: [PATCH] Fix wallet_updated_at_trigger on wallet delete (#2394) --- .../migration.sql | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 prisma/migrations/20250804201438_fix_wallets_updated_at_trigger_on_delete/migration.sql diff --git a/prisma/migrations/20250804201438_fix_wallets_updated_at_trigger_on_delete/migration.sql b/prisma/migrations/20250804201438_fix_wallets_updated_at_trigger_on_delete/migration.sql new file mode 100644 index 00000000..bde679e0 --- /dev/null +++ b/prisma/migrations/20250804201438_fix_wallets_updated_at_trigger_on_delete/migration.sql @@ -0,0 +1,23 @@ +-- fix trigger when wallet is deleted +CREATE OR REPLACE FUNCTION wallet_updated_at_trigger() RETURNS TRIGGER AS $$ +DECLARE + user_id INT; +BEGIN + IF TG_TABLE_NAME = 'WalletProtocol' THEN + SELECT w."userId" INTO user_id + FROM "Wallet" w + WHERE w.id = CASE + WHEN TG_OP = 'DELETE' THEN OLD."walletId" + ELSE NEW."walletId" + END; + ELSE + user_id := CASE WHEN TG_OP = 'DELETE' THEN OLD."userId" ELSE NEW."userId" END; + END IF; + + UPDATE "users" u + SET "walletsUpdatedAt" = NOW() + WHERE u.id = user_id; + + RETURN NULL; +END; +$$ LANGUAGE plpgsql;