Repository navigation
Expand file tree
/
Copy pathinit-db.sql
More file actions
145 lines (125 loc) · 4.83 KB
/
Copy pathinit-db.sql
File metadata and controls
145 lines (125 loc) · 4.83 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
-- Database initialization script for AIRIS platform
-- This script creates the required databases and schemas for all microservices
-- Create databases for each service
CREATE DATABASE airis_auth;
CREATE DATABASE airis_storage;
CREATE DATABASE airis_monitoring;
-- Connect to auth database and create schema
\c airis_auth;
-- Create users table
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
hashed_password VARCHAR(255) NOT NULL,
full_name VARCHAR(255),
is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create roles table
CREATE TABLE IF NOT EXISTS roles (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(50) UNIQUE NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create user_roles table
CREATE TABLE IF NOT EXISTS user_roles (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
role_id UUID REFERENCES roles(id) ON DELETE CASCADE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(user_id, role_id)
);
-- Create user_sessions table
CREATE TABLE IF NOT EXISTS user_sessions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
session_token VARCHAR(255) UNIQUE NOT NULL,
expires_at TIMESTAMP NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Insert default roles
INSERT INTO roles (name, description) VALUES
('admin', 'Administrator with full access'),
('user', 'Regular user with limited access'),
('guest', 'Guest user with read-only access')
ON CONFLICT (name) DO NOTHING;
-- Connect to storage database and create schema
\c airis_storage;
-- Create files table
CREATE TABLE IF NOT EXISTS files (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
filename VARCHAR(255) NOT NULL,
original_filename VARCHAR(255) NOT NULL,
file_path VARCHAR(500) NOT NULL,
file_size BIGINT NOT NULL,
mime_type VARCHAR(100),
user_id UUID NOT NULL,
is_public BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create file_shares table
CREATE TABLE IF NOT EXISTS file_shares (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
file_id UUID REFERENCES files(id) ON DELETE CASCADE,
shared_by UUID NOT NULL,
shared_with UUID NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_files_user_id ON files(user_id);
CREATE INDEX IF NOT EXISTS idx_files_created_at ON files(created_at);
CREATE INDEX IF NOT EXISTS idx_file_shares_file_id ON file_shares(file_id);
-- Connect to monitoring database and create schema
\c airis_monitoring;
-- Create metrics table
CREATE TABLE IF NOT EXISTS metrics (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
metric_name VARCHAR(100) NOT NULL,
metric_value DOUBLE PRECISION NOT NULL,
metric_type VARCHAR(50),
service_name VARCHAR(50),
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create alerts table
CREATE TABLE IF NOT EXISTS alerts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
alert_name VARCHAR(100) NOT NULL,
alert_message TEXT,
severity VARCHAR(20) NOT NULL,
status VARCHAR(20) DEFAULT 'active',
service_name VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
resolved_at TIMESTAMP
);
-- Create health_checks table
CREATE TABLE IF NOT EXISTS health_checks (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
service_name VARCHAR(50) NOT NULL,
status VARCHAR(20) NOT NULL,
response_time_ms INTEGER,
last_check TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
details JSONB
);
-- Create indexes for monitoring
CREATE INDEX IF NOT EXISTS idx_metrics_service_name ON metrics(service_name);
CREATE INDEX IF NOT EXISTS idx_metrics_timestamp ON metrics(timestamp);
CREATE INDEX IF NOT EXISTS idx_alerts_status ON alerts(status);
CREATE INDEX IF NOT EXISTS idx_health_checks_service ON health_checks(service_name);
-- Grant permissions
GRANT ALL PRIVILEGES ON DATABASE airis_auth TO airis;
GRANT ALL PRIVILEGES ON DATABASE airis_storage TO airis;
GRANT ALL PRIVILEGES ON DATABASE airis_monitoring TO airis;
-- Grant schema permissions
\c airis_auth;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO airis;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO airis;
\c airis_storage;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO airis;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO airis;
\c airis_monitoring;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO airis;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO airis;