Last change
on this file was d715225, checked in by ManuelTrajcev <manueltrajcev7@…>, 11 days ago |
deleted customer log trigger
|
-
Property mode
set to
100644
|
File size:
811 bytes
|
Line | |
---|
1 | DROP TRIGGER IF EXISTS trg_customer_deletion ON customer;
|
---|
2 | DROP FUNCTION IF EXISTS customer_deletion();
|
---|
3 |
|
---|
4 | CREATE OR REPLACE FUNCTION customer_deletion()
|
---|
5 | RETURNS TRIGGER AS $$
|
---|
6 | DECLARE
|
---|
7 | total_spent NUMERIC(10, 2);
|
---|
8 | invoice_count INTEGER;
|
---|
9 | BEGIN
|
---|
10 | SELECT COALESCE(SUM(total), 0), COUNT(*)
|
---|
11 | INTO total_spent, invoice_count
|
---|
12 | FROM invoice
|
---|
13 | WHERE customer_id = OLD.customer_id;
|
---|
14 |
|
---|
15 | INSERT INTO deleted_customer_log (
|
---|
16 | first_name, last_name, deleted_at, total_spent, invoice_count
|
---|
17 | )
|
---|
18 | VALUES (
|
---|
19 | OLD.first_name, OLD.last_name, NOW(), total_spent, invoice_count
|
---|
20 | );
|
---|
21 |
|
---|
22 | RETURN OLD;
|
---|
23 | END;
|
---|
24 | $$ LANGUAGE plpgsql;
|
---|
25 |
|
---|
26 | DROP TRIGGER IF EXISTS trg_customer_deletion ON customer;
|
---|
27 |
|
---|
28 | CREATE TRIGGER trg_customer_deletion
|
---|
29 | BEFORE DELETE ON customer
|
---|
30 | FOR EACH ROW
|
---|
31 | EXECUTE FUNCTION customer_deletion();
|
---|
Note:
See
TracBrowser
for help on using the repository browser.