-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
75 lines (69 loc) · 2.27 KB
/
Copy pathschema.sql
File metadata and controls
75 lines (69 loc) · 2.27 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
-- TABLES
-- Users table
CREATE TABLE IF NOT EXISTS users (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
email VARCHAR(255) UNIQUE NOT NULL,
name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
password VARCHAR(60) NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
-- Addresses table
-- CREATE TABLE IF NOT EXISTS addresses (
-- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
-- user_id uuid,
-- address_line1 VARCHAR(255) NOT NULL,
-- address_line2 VARCHAR(255),
-- city VARCHAR(100) NOT NULL,
-- state VARCHAR(100) NOT NULL,
-- postal_code VARCHAR(20) NOT NULL,
-- country VARCHAR(100) NOT NULL,
-- created_at TIMESTAMP DEFAULT NOW(),
-- CONSTRAINT fk_addresses_user_id FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
-- );
-- Products table
CREATE TABLE IF NOT EXISTS products (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
price NUMERIC(10, 2) NOT NULL,
stock INT NOT NULL DEFAULT 0,
created_at TIMESTAMP DEFAULT NOW()
);
-- Orders table
CREATE TABLE IF NOT EXISTS orders (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid NOT NULL,
external_transaction_id uuid NOT NULL,
status VARCHAR(50) NOT NULL,
total NUMERIC(10, 2) NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW(),
CONSTRAINT fk_orders_user_id FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
-- Order items table
CREATE TABLE IF NOT EXISTS order_items (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
order_id uuid NOT NULL,
product_id uuid NOT NULL,
quantity INT NOT NULL,
price NUMERIC(10, 2) NOT NULL,
CONSTRAINT fk_order_items_order_id FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE,
CONSTRAINT fk_order_items_product_id FOREIGN KEY (product_id) REFERENCES products(id)
);
-- TRIGGERS
-- Trigger function to update updated_at only if status changes
CREATE OR REPLACE FUNCTION update_orders_updated_at()
RETURNS TRIGGER AS $$
BEGIN
IF NEW.status IS DISTINCT FROM OLD.status THEN
NEW.updated_at := NOW();
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Create the trigger on the orders table
CREATE TRIGGER trg_update_orders_updated_at
BEFORE UPDATE ON orders
FOR EACH ROW
EXECUTE FUNCTION update_orders_updated_at();