-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg.sql
227 lines (193 loc) · 7.95 KB
/
pg.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
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
create schema user_s;
-- ensure uuid-ossp extension is created for 'uuid_generate_v4()' function
create extension if not exists "uuid-ossp";
-- create 'generate_edited_at_on_update()' function for the schema
-- this function sets the 'edited_at' column to the current time upon update
create or replace function user_s.generate_edited_at_on_update()
returns trigger as
$$
begin
new.edited_at = now();
return new;
end;
$$ language plpgsql;
-- create user_role table
-- represents a user's role within the system
create table user_s.user_role
(
id serial not null primary key,
name varchar(255) not null
constraint user_role_name_uidx unique,
description text,
created_at timestamp not null default now(),
edited_at timestamp not null default now(),
deleted_at timestamp
);
-- create trigger on user_role table to execute the 'generate_edited_at_on_update()' function
create trigger user_role_set_timestamp_trig
before update
on user_s.user_role
for each row
execute procedure user_s.generate_edited_at_on_update();
-- insert common user roles into the system
insert into user_s.user_role (name, description)
values ('Administrator', 'An internal-user with access to everything.'),
('Manager', 'An internal-user with access to everything that is non-destructive.'),
('Staff', 'An internal-user with access to basic services.');
-- create user_account_type table
-- represents a type of user account
create table user_s.user_account_type
(
id serial not null primary key,
name varchar(255) not null
constraint user_account_type_name_uidx unique,
description text,
created_at timestamp not null default now(),
edited_at timestamp not null default now(),
deleted_at timestamp
);
-- create trigger on user_account_type table to execute the 'generate_edited_at_on_update()' function
create trigger user_account_type_set_timestamp_trig
before update
on user_s.user_account_type
for each row
execute procedure user_s.generate_edited_at_on_update();
-- insert basic account types into store
insert into user_s.user_account_type (name, description)
values ('Employee', 'An employee of the organization.'),
('Customer', 'A customer of the organization.');
-- create user_account table
-- represents a single user
create table user_s.user_account
(
id serial not null primary key,
public_id uuid not null default uuid_generate_v4(),
type_id int not null
constraint user_account_type_id_fk references user_s.user_account_type (id),
is_active boolean not null default false,
created_at timestamp not null default now(),
edited_at timestamp not null default now(),
deleted_at timestamp
);
-- create trigger on user_account table to execute the 'generate_edited_at_on_update()' function
create trigger user_account_set_timestamp_trig
before update
on user_s.user_account
for each row
execute procedure user_s.generate_edited_at_on_update();
-- create user_activiation_token table
-- represents a single activation token
create table user_s.user_activation_token
(
id serial not null primary key,
token text not null
constraint user_activation_token_uidx unique,
activated_at timestamp,
created_at timestamp not null default now(),
edited_at timestamp not null default now(),
deleted_at timestamp
);
-- create trigger on user_activation_token table to execute the 'generate_edited_at_on_update()' function
create trigger user_activation_token_set_timestamp_trig
before update
on user_s.user_activation_token
for each row
execute procedure user_s.generate_edited_at_on_update();
-- create user_profile table
-- represents all of the data associated with a user
create table user_s.user_profile
(
id int not null primary key
constraint user_profile_user_account_id_fk references user_s.user_account (id),
first_name varchar(255) not null,
last_name varchar(255) not null,
date_of_birth date not null,
additional_details json,
created_at timestamp not null default now(),
edited_at timestamp not null default now(),
deleted_at timestamp
);
-- create trigger on user_profile table to execute the 'generate_edited_at_on_update()' function
create trigger user_profile_set_timestamp_trig
before update
on user_s.user_profile
for each row
execute procedure user_s.generate_edited_at_on_update();
-- create user_credential_type
create type user_s.user_credential_type as enum ('EMAIL', 'PHONE', 'USERNAME', 'UUID');
-- create user_credential table
-- represents a single set of credentials associated with a user account
create table user_s.user_credential
(
id serial not null primary key,
user_id int not null
constraint user_credential_user_account_id_fk references user_s.user_account (id),
type user_s.user_credential_type not null default 'EMAIL',
password_hash text not null,
password_salt text,
hashing_algorithm varchar(100) not null default 'BCRYPT',
created_at timestamp not null default now(),
edited_at timestamp not null default now(),
deleted_at timestamp
);
-- create trigger on user_credential table to execute the 'generate_edited_at_on_update()' function
create trigger user_credential_set_timestamp_trig
before update
on user_s.user_credential
for each row
execute procedure user_s.generate_edited_at_on_update();
-- create user_identity_provider table
-- represents a single user identity provider like Google, Apple, or Facebook
create table user_s.user_identity_provider
(
id serial not null primary key,
name varchar(255) not null
constraint user_identity_provider_name_uidx unique,
details json,
created_at timestamp not null default now(),
edited_at timestamp not null default now(),
deleted_at timestamp
);
-- create trigger on user_identity_provider table to execute the 'generate_edited_at_on_update()' function
create trigger user_identity_provider_set_timestamp_trig
before update
on user_s.user_identity_provider
for each row
execute procedure user_s.generate_edited_at_on_update();
-- create user_identity table
-- represents a social identity associated with a user account
create table user_s.user_identity
(
id serial not null primary key,
user_id int not null
constraint user_identity_user_account_id_fk references user_s.user_account (id),
provider_id int not null
constraint user_identity_provider_id_fk references user_s.user_identity_provider (id),
external_id text not null,
token text not null,
created_at timestamp not null default now(),
edited_at timestamp not null default now(),
deleted_at timestamp
);
-- create trigger on user_identity table to execute the 'generate_edited_at_on_update()' function
create trigger user_identity_set_timestamp_trig
before update
on user_s.user_identity
for each row
execute procedure user_s.generate_edited_at_on_update();
-- create user_login table
-- represents a single login attempt
create table user_s.user_login (
id serial not null primary key,
credential_used text not null,
details json,
created_at timestamp not null default now(),
edited_at timestamp not null default now(),
deleted_at timestamp
);
-- create trigger on user_login table to execute the 'generate_edited_at_on_update()' function
create trigger user_login_set_timestamp_trig
before update
on user_s.user_login
for each row
execute procedure user_s.generate_edited_at_on_update();