-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrds_postgres_schema.sql
44 lines (39 loc) · 1.08 KB
/
rds_postgres_schema.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-- Create the products_with_increment table
CREATE TABLE products_with_increment (
id serial PRIMARY KEY,
created_at timestamp NOT NULL DEFAULT now(),
name character varying(255),
sku character varying(255),
description text,
price money,
last_updated date,
uuid character varying(255)
);
-- Create or replace the notify_change function
CREATE OR REPLACE FUNCTION notify_change() RETURNS TRIGGER AS $$
BEGIN
PERFORM pg_notify(
'table_change',
json_build_object(
'table', TG_TABLE_NAME,
'action', TG_OP,
'old', OLD,
'new', NEW
)::text
);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Create triggers for the products_with_increment table
CREATE TRIGGER products_deleted
AFTER DELETE ON products_with_increment
FOR EACH ROW
EXECUTE FUNCTION notify_change();
CREATE TRIGGER products_inserted
AFTER INSERT ON products_with_increment
FOR EACH ROW
EXECUTE FUNCTION notify_change();
CREATE TRIGGER products_updated
AFTER UPDATE ON products_with_increment
FOR EACH ROW
EXECUTE FUNCTION notify_change();