-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseline.sql
More file actions
228 lines (181 loc) · 4.79 KB
/
baseline.sql
File metadata and controls
228 lines (181 loc) · 4.79 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
--
-- Name: user_status; Type: TYPE; Schema: -; Owner: -
--
CREATE TYPE user_status AS ENUM (
'active',
'inactive'
);
--
-- Name: order_status; Type: TYPE; Schema: -; Owner: -
--
CREATE TYPE order_status AS ENUM (
'pending',
'completed'
);
--
-- Name: address; Type: TYPE; Schema: -; Owner: -
--
CREATE TYPE address AS (street text, city text);
-- Include domain types (constrained base types)
--
-- Name: email_address; Type: DOMAIN; Schema: -; Owner: -
--
CREATE DOMAIN email_address AS text
CONSTRAINT email_address_check CHECK (VALUE ~~ '%@%');
--
-- Name: positive_integer; Type: DOMAIN; Schema: -; Owner: -
--
CREATE DOMAIN positive_integer AS integer
CONSTRAINT positive_integer_check CHECK (VALUE > 0);
-- Include sequences (may be used by tables)
--
-- Name: global_id_seq; Type: SEQUENCE; Schema: -; Owner: -
--
CREATE SEQUENCE IF NOT EXISTS global_id_seq;
--
-- Name: order_number_seq; Type: SEQUENCE; Schema: -; Owner: -
--
CREATE SEQUENCE IF NOT EXISTS order_number_seq;
-- Include trigger function (needed by users table trigger)
--
-- Name: update_timestamp; Type: FUNCTION; Schema: -; Owner: -
--
CREATE OR REPLACE FUNCTION update_timestamp()
RETURNS trigger
LANGUAGE plpgsql
SECURITY INVOKER
STABLE
AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$;
-- Include core tables (with their constraints, indexes, and policies)
--
-- Name: users; Type: TABLE; Schema: -; Owner: -
--
CREATE TABLE IF NOT EXISTS users (
id integer PRIMARY KEY,
email text NOT NULL CHECK (email LIKE '%@%'),
name text NOT NULL
);
COMMENT ON TABLE users IS 'User accounts';
COMMENT ON COLUMN users.email IS 'User email address';
--
-- Name: idx_users_email; Type: INDEX; Schema: -; Owner: -
--
CREATE INDEX IF NOT EXISTS idx_users_email ON users (email);
--
-- Name: idx_users_name; Type: INDEX; Schema: -; Owner: -
--
CREATE INDEX IF NOT EXISTS idx_users_name ON users (name);
--
-- Name: users; Type: RLS; Schema: -; Owner: -
--
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
--
-- Name: users_policy; Type: POLICY; Schema: -; Owner: -
--
CREATE POLICY users_policy ON users TO PUBLIC USING (true);
--
-- Name: users_update_trigger; Type: TRIGGER; Schema: -; Owner: -
--
CREATE OR REPLACE TRIGGER users_update_trigger
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE FUNCTION update_timestamp();
--
-- Name: orders; Type: TABLE; Schema: -; Owner: -
--
CREATE TABLE IF NOT EXISTS orders (
id integer PRIMARY KEY,
user_id integer NOT NULL REFERENCES users(id),
status text DEFAULT 'pending' NOT NULL CHECK (status IN ('pending', 'completed')),
amount numeric(10,2) DEFAULT 0.00
);
COMMENT ON TABLE orders IS 'Customer orders';
COMMENT ON COLUMN orders.user_id IS 'Reference to user';
--
-- Name: idx_orders_status; Type: INDEX; Schema: -; Owner: -
--
CREATE INDEX IF NOT EXISTS idx_orders_status ON orders (status);
--
-- Name: idx_orders_user_id; Type: INDEX; Schema: -; Owner: -
--
CREATE INDEX IF NOT EXISTS idx_orders_user_id ON orders (user_id);
--
-- Name: orders; Type: RLS; Schema: -; Owner: -
--
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
--
-- Name: orders_policy; Type: POLICY; Schema: -; Owner: -
--
CREATE POLICY orders_policy ON orders TO PUBLIC USING (user_id = 1);
-- Include other functions (after tables that they reference)
--
-- Name: get_user_count; Type: FUNCTION; Schema: -; Owner: -
--
CREATE OR REPLACE FUNCTION get_user_count()
RETURNS integer
LANGUAGE sql
SECURITY INVOKER
VOLATILE
AS $$
SELECT COUNT(*) FROM users;
$$;
--
-- Name: get_order_count; Type: FUNCTION; Schema: -; Owner: -
--
CREATE OR REPLACE FUNCTION get_order_count(
user_id_param integer
)
RETURNS integer
LANGUAGE sql
SECURITY INVOKER
VOLATILE
AS $$
SELECT COUNT(*) FROM orders WHERE user_id = user_id_param;
$$;
-- Include procedures
--
-- Name: cleanup_orders; Type: PROCEDURE; Schema: -; Owner: -
--
CREATE OR REPLACE PROCEDURE cleanup_orders()
LANGUAGE sql
AS $$
DELETE FROM orders WHERE status = 'completed';
$$;
--
-- Name: update_status; Type: PROCEDURE; Schema: -; Owner: -
--
CREATE OR REPLACE PROCEDURE update_status(
user_id_param integer,
new_status text
)
LANGUAGE sql
AS $$
UPDATE orders SET status = new_status WHERE user_id = user_id_param;
$$;
-- Include views (depend on tables and functions)
--
-- Name: user_summary; Type: VIEW; Schema: -; Owner: -
--
CREATE OR REPLACE VIEW user_summary AS
SELECT u.id,
u.name,
count(o.id) AS order_count
FROM users u
JOIN orders o ON u.id = o.user_id
GROUP BY u.id, u.name;;
COMMENT ON VIEW user_summary IS 'User order summary';
--
-- Name: order_details; Type: VIEW; Schema: -; Owner: -
--
CREATE OR REPLACE VIEW order_details AS
SELECT o.id,
o.status,
u.name AS user_name
FROM orders o
JOIN users u ON o.user_id = u.id;;
COMMENT ON VIEW order_details IS 'Order details with user info';