-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgre_queries_triggers.sql
110 lines (94 loc) · 4.25 KB
/
postgre_queries_triggers.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
-- Users Table
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_users_email ON users(email);
-- Groups Table
CREATE TABLE groups (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
description TEXT,
created_by INTEGER REFERENCES users(id),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_groups_created_by ON groups(created_by);
-- User_Group Table (for many-to-many relationship between users and groups)
CREATE TABLE user_group (
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
group_id INTEGER REFERENCES groups(id) ON DELETE CASCADE,
role VARCHAR(20) DEFAULT 'MEMBER',
joined_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_id, group_id)
);
CREATE INDEX idx_user_group_user_id ON user_group(user_id);
CREATE INDEX idx_user_group_group_id ON user_group(group_id);
-- Expenses Table
CREATE TABLE expenses (
id SERIAL PRIMARY KEY,
group_id INTEGER REFERENCES groups(id) ON DELETE CASCADE,
payer_id INTEGER REFERENCES users(id),
amount DECIMAL(10, 2) NOT NULL,
description TEXT NOT NULL,
date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_expenses_group_id ON expenses(group_id);
CREATE INDEX idx_expenses_payer_id ON expenses(payer_id);
-- Expense_Splits Table
CREATE TABLE expense_splits (
id SERIAL PRIMARY KEY,
expense_id INTEGER REFERENCES expenses(id) ON DELETE CASCADE,
user_id INTEGER REFERENCES users(id),
amount DECIMAL(10, 2) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_expense_splits_expense_id ON expense_splits(expense_id);
CREATE INDEX idx_expense_splits_user_id ON expense_splits(user_id);
-- Settlements Table
CREATE TABLE settlements (
id SERIAL PRIMARY KEY,
payer_id INTEGER REFERENCES users(id),
payee_id INTEGER REFERENCES users(id),
amount DECIMAL(10, 2) NOT NULL,
group_id INTEGER REFERENCES groups(id),
settled_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_settlements_payer_id ON settlements(payer_id);
CREATE INDEX idx_settlements_payee_id ON settlements(payee_id);
CREATE INDEX idx_settlements_group_id ON settlements(group_id);
-- Categories Table
CREATE TABLE categories (
id SERIAL PRIMARY KEY,
name VARCHAR(50) UNIQUE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Add category_id to Expenses Table
ALTER TABLE expenses
ADD COLUMN category_id INTEGER REFERENCES categories(id);
CREATE INDEX idx_expenses_category_id ON expenses(category_id);
-- Create a function to update the updated_at timestamp
CREATE OR REPLACE FUNCTION update_modified_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ language 'plpgsql';
-- Create triggers to automatically update the updated_at column
CREATE TRIGGER update_user_modtime BEFORE UPDATE ON users FOR EACH ROW EXECUTE FUNCTION update_modified_column();
CREATE TRIGGER update_group_modtime BEFORE UPDATE ON groups FOR EACH ROW EXECUTE FUNCTION update_modified_column();
CREATE TRIGGER update_expense_modtime BEFORE UPDATE ON expenses FOR EACH ROW EXECUTE FUNCTION update_modified_column();
CREATE TRIGGER update_expense_split_modtime BEFORE UPDATE ON expense_splits FOR EACH ROW EXECUTE FUNCTION update_modified_column();
CREATE TRIGGER update_settlement_modtime BEFORE UPDATE ON settlements FOR EACH ROW EXECUTE FUNCTION update_modified_column();
CREATE TRIGGER update_category_modtime BEFORE UPDATE ON categories FOR EACH ROW EXECUTE FUNCTION update_modified_column();