stacker.news/prisma/migrations/20221207212053_donate_func/migration.sql

25 lines
559 B
MySQL
Raw Normal View History

2022-12-08 00:04:02 +00:00
CREATE OR REPLACE FUNCTION donate(sats INTEGER, user_id INTEGER)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
user_sats INTEGER;
BEGIN
PERFORM ASSERT_SERIALIZED();
SELECT msats / 1000
INTO user_sats
FROM users WHERE id = user_id;
IF sats > user_sats THEN
RAISE EXCEPTION 'SN_INSUFFICIENT_FUNDS';
END IF;
UPDATE users SET msats = msats - (sats * 1000) WHERE id = user_id;
INSERT INTO "Donate" (sats, "userId", created_at, updated_at)
VALUES (sats, user_id, now_utc(), now_utc());
RETURN sats;
END;
$$;