-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.sql
More file actions
224 lines (187 loc) · 11 KB
/
create.sql
File metadata and controls
224 lines (187 loc) · 11 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
/*
============================================================================
██╗ ██╗███████╗██████╗ ██████╗ ███████╗██╗ ██╗ ██████╗ ██████╗
██║ ██║██╔════╝██╔══██╗██╔════╝ ██╔════╝██║ ██║██╔═══██╗██╔════╝
███████║█████╗ ██║ ██║██║ ███╗█████╗ ███████║██║ ██║██║ ███╗
██╔══██║██╔══╝ ██║ ██║██║ ██║██╔══╝ ██╔══██║██║ ██║██║ ██║
██║ ██║███████╗██████╔╝╚██████╔╝███████╗██║ ██║╚██████╔╝╚██████╔╝
╚═╝ ╚═╝╚══════╝╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝
██████╗ █████╗ ████████╗ █████╗ ██████╗ █████╗ ███████╗███████╗
██╔══██╗██╔══██╗╚══██╔══╝██╔══██╗██╔══██╗██╔══██╗██╔════╝██╔════╝
██║ ██║███████║ ██║ ███████║██████╔╝███████║███████╗█████╗
██║ ██║██╔══██║ ██║ ██╔══██║██╔══██╗██╔══██║╚════██║██╔══╝
██████╔╝██║ ██║ ██║ ██║ ██║██████╔╝██║ ██║███████║███████╗
╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═════╝ ╚═╝ ╚═╝╚══════╝╚══════╝
Database Name: hedgehogdb
Version: 1.1.0
Created: 2026
Description: Database for Server Management System
Copyright: © 2026 batacek.eu
License: Apache-2.0 license (Check LICENSE file)
Database Engine: PostgreSQL
Extension Used: pgcrypto (UUID generation and password hashing)
============================================================================
*/
-- Enable pgcrypto extension for UUID generation and hashing
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
-- Drop tables if they exist to start fresh
/*
DROP TABLE IF EXISTS service_owners CASCADE;
DROP TABLE IF EXISTS server_owners CASCADE;
DROP TABLE IF EXISTS services CASCADE;
DROP TABLE IF EXISTS servers CASCADE;
DROP TABLE IF EXISTS user_groups CASCADE;
DROP TABLE IF EXISTS groups CASCADE;
DROP TABLE IF EXISTS users CASCADE;
DROP TABLE IF EXISTS user_security_events CASCADE;
*/
-- Users
CREATE TABLE users (
uuid UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username VARCHAR NOT NULL UNIQUE,
email VARCHAR NOT NULL UNIQUE,
firstname VARCHAR,
middlename VARCHAR,
lastname VARCHAR,
password_hash VARCHAR NOT NULL, -- Stores bcrypt hashes (via pgcrypto crypt())
created_at TIMESTAMP DEFAULT now()
);
-- Groups
CREATE TABLE groups (
uuid UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT now()
);
-- Users <-> Groups (many-to-many)
CREATE TABLE user_groups (
user_uuid UUID NOT NULL,
group_uuid UUID NOT NULL,
priority INT NOT NULL DEFAULT 0,
assigned_at TIMESTAMP DEFAULT now(),
PRIMARY KEY (user_uuid, group_uuid),
FOREIGN KEY (user_uuid) REFERENCES users(uuid) ON DELETE CASCADE,
FOREIGN KEY (group_uuid) REFERENCES groups(uuid) ON DELETE CASCADE
);
-- Servers
CREATE TABLE servers (
uuid UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT now()
);
-- Services
CREATE TABLE services (
uuid UUID PRIMARY KEY DEFAULT gen_random_uuid(),
server_uuid UUID NOT NULL,
name VARCHAR NOT NULL,
type SMALLINT NOT NULL, -- small integer for service type
description TEXT,
created_at TIMESTAMP DEFAULT now(),
FOREIGN KEY (server_uuid) REFERENCES servers(uuid) ON DELETE CASCADE
);
-- Server owners (many-to-many: users & groups)
CREATE TABLE server_owners (
server_uuid UUID NOT NULL,
user_uuid UUID,
group_uuid UUID,
assigned_at TIMESTAMP DEFAULT now(),
FOREIGN KEY (server_uuid) REFERENCES servers(uuid) ON DELETE CASCADE,
FOREIGN KEY (user_uuid) REFERENCES users(uuid) ON DELETE CASCADE,
FOREIGN KEY (group_uuid) REFERENCES groups(uuid) ON DELETE CASCADE,
CHECK (
(user_uuid IS NOT NULL AND group_uuid IS NULL)
OR (user_uuid IS NULL AND group_uuid IS NOT NULL)
)
);
-- Partial unique indexes for server_owners
CREATE UNIQUE INDEX server_owners_user_uniq ON server_owners(server_uuid, user_uuid) WHERE user_uuid IS NOT NULL;
CREATE UNIQUE INDEX server_owners_group_uniq ON server_owners(server_uuid, group_uuid) WHERE group_uuid IS NOT NULL;
-- Service owners (many-to-many: users & groups)
CREATE TABLE service_owners (
service_uuid UUID NOT NULL,
user_uuid UUID,
group_uuid UUID,
assigned_at TIMESTAMP DEFAULT now(),
FOREIGN KEY (service_uuid) REFERENCES services(uuid) ON DELETE CASCADE,
FOREIGN KEY (user_uuid) REFERENCES users(uuid) ON DELETE CASCADE,
FOREIGN KEY (group_uuid) REFERENCES groups(uuid) ON DELETE CASCADE,
CHECK (
(user_uuid IS NOT NULL AND group_uuid IS NULL)
OR (user_uuid IS NULL AND group_uuid IS NOT NULL)
)
);
-- Partial unique indexes for service_owners
CREATE UNIQUE INDEX service_owners_user_uniq ON service_owners(service_uuid, user_uuid) WHERE user_uuid IS NOT NULL;
CREATE UNIQUE INDEX service_owners_group_uniq ON service_owners(service_uuid, group_uuid) WHERE group_uuid IS NOT NULL;
-- Indexes for user_groups table (for performance)
CREATE INDEX idx_user_groups_user ON user_groups(user_uuid);
CREATE INDEX idx_user_groups_group ON user_groups(group_uuid);
CREATE INDEX idx_user_groups_user_priority ON user_groups(user_uuid, priority DESC);
-- Indexes on foreign keys for performance
CREATE INDEX idx_server_owners_user ON server_owners(user_uuid);
CREATE INDEX idx_server_owners_group ON server_owners(group_uuid);
CREATE INDEX idx_service_owners_user ON service_owners(user_uuid);
CREATE INDEX idx_service_owners_group ON service_owners(group_uuid);
CREATE INDEX idx_services_server ON services(server_uuid);
CREATE TABLE user_security_events (
id UUID PRIMARY KEY,
user_id UUID NULL,
actor_user_id UUID NULL,
event_type VARCHAR(100) NOT NULL,
occurred_at_utc TIMESTAMPTZ NOT NULL,
ip_address VARCHAR(45) NOT NULL,
user_agent TEXT NULL,
success BOOLEAN NOT NULL,
metadata JSONB NULL
);
CREATE INDEX idx_user_security_events_user_time
ON user_security_events (user_id, occurred_at_utc DESC);
CREATE INDEX idx_user_security_events_event_time
ON user_security_events (event_type, occurred_at_utc DESC);
CREATE INDEX idx_user_security_events_actor_time
ON user_security_events (actor_user_id, occurred_at_utc DESC);
-- Insert default users with hashed passwords (using bcrypt)
INSERT INTO users (username, email, firstname, middlename, lastname, password_hash, created_at)
VALUES
('admin', 'admin@example.batacek.eu', 'Admin', NULL, 'User', crypt('admin123', gen_salt('bf')), NOW()),
('default_user', 'default@example.batacek.eu', 'Default', NULL, 'User', crypt('user123', gen_salt('bf')), NOW())
ON CONFLICT (username) DO NOTHING;
-- Check inserted users
SELECT * FROM users;
-- Insert default servers
INSERT INTO servers (name, description, created_at)
VALUES ('Main Server', 'Primary production server', NOW()),
('Backup Server', 'Backup and redundancy server', NOW()),
('Development Server', 'Development and testing environment', NOW());
-- Assign servers to admin user (using subquery to get admin's UUID)
INSERT INTO server_owners (server_uuid, user_uuid, group_uuid)
SELECT s.uuid, u.uuid, NULL
FROM servers s
CROSS JOIN users u
WHERE u.username = 'admin';
-- Check inserted servers and their ownership
SELECT s.name, u.username, s.description
FROM servers s
JOIN server_owners so ON s.uuid = so.server_uuid
JOIN users u ON so.user_uuid = u.uuid;
-- Insert default groups
INSERT INTO groups (name, description, created_at)
VALUES ('admin', 'Administrators', NOW()),
('default', 'Default users', NOW());
-- Add users to their respective groups
INSERT INTO user_groups (user_uuid, group_uuid, priority)
SELECT u.uuid, g.uuid, 100
FROM users u, groups g
WHERE u.username = 'admin' AND g.name = 'admin';
INSERT INTO user_groups (user_uuid, group_uuid, priority)
SELECT u.uuid, g.uuid, 0
FROM users u, groups g
WHERE u.username = 'default_user' AND g.name = 'default';
-- Check inserted groups and memberships
SELECT name, description FROM groups;
SELECT u.username, g.name AS group_name, ug.priority
FROM user_groups ug
JOIN users u ON ug.user_uuid = u.uuid
JOIN groups g ON ug.group_uuid = g.uuid
ORDER BY u.username, ug.priority DESC;