Source file repo.ml
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
module Database = Sihl.Database
module Cleaner = Sihl.Cleaner
module Migration = Sihl.Database.Migration
module Model = struct
module Data = struct
type t = (string * string) list [@@deriving yojson]
let to_string data = data |> to_yojson |> Yojson.Safe.to_string
let of_string str = str |> Yojson.Safe.from_string |> of_yojson
end
module Status = struct
type t =
| Active
| Inactive
let to_string = function
| Active -> "active"
| Inactive -> "inactive"
;;
let of_string str =
match str with
| "active" -> Ok Active
| "inactive" -> Ok Inactive
| _ -> Error (Printf.sprintf "Invalid token status %s provided" str)
;;
end
type t =
{ id : string
; value : string
; data : Data.t
; status : Status.t
; expires_at : Ptime.t
; created_at : Ptime.t
}
let t =
let ( let* ) = Result.bind in
let encode m =
let status = Status.to_string m.status in
let data = Data.to_string m.data in
Ok (m.id, (m.value, (data, (status, (m.expires_at, m.created_at)))))
in
let decode (id, (value, (data, (status, (expires_at, created_at))))) =
let* status = Status.of_string status in
let* data = Data.of_string data in
Ok { id; value; data; status; expires_at; created_at }
in
Caqti_type.(
custom
~encode
~decode
(tup2
string
(tup2 string (tup2 string (tup2 string (tup2 ptime ptime))))))
;;
end
module type Sig = sig
val lifecycles : Sihl.Container.lifecycle list
val register_migration : unit -> unit
val register_cleaner : unit -> unit
val find : ?ctx:(string * string) list -> string -> Model.t Lwt.t
val find_opt : ?ctx:(string * string) list -> string -> Model.t option Lwt.t
val find_by_id : ?ctx:(string * string) list -> string -> Model.t Lwt.t
val insert : ?ctx:(string * string) list -> Model.t -> unit Lwt.t
val update : ?ctx:(string * string) list -> Model.t -> unit Lwt.t
module Model = Model
end
module MariaDb (MigrationService : Sihl.Contract.Migration.Sig) : Sig = struct
let lifecycles = [ Database.lifecycle; MigrationService.lifecycle ]
module Model = Model
module Sql = struct
let find_request =
Caqti_request.find
Caqti_type.string
Model.t
{sql|
SELECT
LOWER(CONCAT(
SUBSTR(HEX(uuid), 1, 8), '-',
SUBSTR(HEX(uuid), 9, 4), '-',
SUBSTR(HEX(uuid), 13, 4), '-',
SUBSTR(HEX(uuid), 17, 4), '-',
SUBSTR(HEX(uuid), 21)
)),
token_value,
token_data,
status,
expires_at,
created_at
FROM token_tokens
WHERE token_tokens.token_value = ?
|sql}
;;
let find ?ctx value = Database.find ?ctx find_request value
let find_request_opt =
Caqti_request.find_opt
Caqti_type.string
Model.t
{sql|
SELECT
LOWER(CONCAT(
SUBSTR(HEX(uuid), 1, 8), '-',
SUBSTR(HEX(uuid), 9, 4), '-',
SUBSTR(HEX(uuid), 13, 4), '-',
SUBSTR(HEX(uuid), 17, 4), '-',
SUBSTR(HEX(uuid), 21)
)),
token_value,
token_data,
status,
expires_at,
created_at
FROM token_tokens
WHERE token_tokens.token_value = ?
|sql}
;;
let find_opt ?ctx value = Database.find_opt ?ctx find_request_opt value
let find_by_id_request =
Caqti_request.find
Caqti_type.string
Model.t
{sql|
SELECT
LOWER(CONCAT(
SUBSTR(HEX(uuid), 1, 8), '-',
SUBSTR(HEX(uuid), 9, 4), '-',
SUBSTR(HEX(uuid), 13, 4), '-',
SUBSTR(HEX(uuid), 17, 4), '-',
SUBSTR(HEX(uuid), 21)
)),
token_value,
token_data,
status,
expires_at,
created_at
FROM token_tokens
WHERE token_tokens.uuid = UNHEX(REPLACE(?, '-', ''))
|sql}
;;
let find_by_id ?ctx id = Database.find ?ctx find_by_id_request id
let insert_request =
Caqti_request.exec
Model.t
{sql|
INSERT INTO token_tokens (
uuid,
token_value,
token_data,
status,
expires_at,
created_at
) VALUES (
UNHEX(REPLACE($1, '-', '')),
$2,
$3,
$4,
$5,
$6
)
|sql}
;;
let insert ?ctx token = Database.exec ?ctx insert_request token
let update_request =
Caqti_request.exec
Model.t
{sql|
UPDATE token_tokens
SET
token_data = $3,
status = $4,
expires_at = $5,
created_at = $6
WHERE token_tokens.token_value = $2
|sql}
;;
let update ?ctx token = Database.exec ?ctx update_request token
let clean_request =
Caqti_request.exec Caqti_type.unit "TRUNCATE token_tokens;"
;;
let clean ?ctx () = Database.exec ?ctx clean_request ()
end
module Migration = struct
let fix_collation =
Migration.create_step
~label:"fix collation"
"SET collation_server = 'utf8mb4_unicode_ci'"
;;
let create_tokens_table =
Migration.create_step
~label:"create tokens table"
{sql|
CREATE TABLE IF NOT EXISTS token_tokens (
id BIGINT UNSIGNED AUTO_INCREMENT,
uuid BINARY(16) NOT NULL,
token_value VARCHAR(128) NOT NULL,
token_data VARCHAR(1024),
token_kind VARCHAR(128) NOT NULL,
status VARCHAR(128) NOT NULL,
expires_at TIMESTAMP NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
CONSTRAINT unqiue_uuid UNIQUE KEY (uuid),
CONSTRAINT unique_value UNIQUE KEY (token_value)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|sql}
;;
let remove_token_kind_column =
Migration.create_step
~label:"remove token kind column"
"ALTER TABLE token_tokens DROP COLUMN token_kind;"
;;
let migration () =
Migration.(
empty "tokens"
|> add_step fix_collation
|> add_step create_tokens_table
|> add_step remove_token_kind_column)
;;
end
let register_migration () =
MigrationService.register_migration (Migration.migration ())
;;
let register_cleaner () = Cleaner.register_cleaner Sql.clean
let find = Sql.find
let find_opt = Sql.find_opt
let find_by_id = Sql.find_by_id
let insert = Sql.insert
let update = Sql.update
end
module PostgreSql (MigrationService : Sihl.Contract.Migration.Sig) : Sig =
struct
let lifecycles = [ Database.lifecycle; MigrationService.lifecycle ]
module Model = Model
module Sql = struct
let find_request =
Caqti_request.find
Caqti_type.string
Model.t
{sql|
SELECT
uuid,
token_value,
token_data,
status,
expires_at,
created_at
FROM token_tokens
WHERE token_tokens.token_value = ?::text
|sql}
;;
let find ?ctx value = Database.find ?ctx find_request value
let find_request_opt =
Caqti_request.find_opt
Caqti_type.string
Model.t
{sql|
SELECT
uuid,
token_value,
token_data,
status,
expires_at,
created_at
FROM token_tokens
WHERE token_tokens.token_value = ?::text
|sql}
;;
let find_opt ?ctx value = Database.find_opt ?ctx find_request_opt value
let find_by_id_request =
Caqti_request.find
Caqti_type.string
Model.t
{sql|
SELECT
uuid,
token_value,
token_data,
status,
expires_at,
created_at
FROM token_tokens
WHERE token_tokens.uuid = ?::uuid
|sql}
;;
let find_by_id ?ctx id = Database.find ?ctx find_by_id_request id
let insert_request =
Caqti_request.exec
Model.t
{sql|
INSERT INTO token_tokens (
uuid,
token_value,
token_data,
status,
expires_at,
created_at
) VALUES (
$1::uuid,
$2,
$3,
$4,
$5 AT TIME ZONE 'UTC',
$6 AT TIME ZONE 'UTC'
)
|sql}
;;
let insert ?ctx token = Database.exec ?ctx insert_request token
let update_request =
Caqti_request.exec
Model.t
{sql|
UPDATE token_tokens
SET
uuid = $1::uuid,
token_data = $3,
status = $4,
expires_at = $5 AT TIME ZONE 'UTC',
created_at = $6 AT TIME ZONE 'UTC'
WHERE token_tokens.token_value = $2
|sql}
;;
let update ?ctx token = Database.exec ?ctx update_request token
let clean_request =
Caqti_request.exec Caqti_type.unit "TRUNCATE token_tokens CASCADE;"
;;
let clean ?ctx () = Database.exec ?ctx clean_request ()
end
module Migration = struct
let create_tokens_table =
Migration.create_step
~label:"create tokens table"
{sql|
CREATE TABLE IF NOT EXISTS token_tokens (
id serial,
uuid uuid NOT NULL,
token_value VARCHAR(128) NOT NULL,
token_data VARCHAR(1024),
status VARCHAR(128) NOT NULL,
expires_at TIMESTAMP NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE (uuid),
UNIQUE (token_value)
);
|sql}
;;
let remove_timezone =
Sihl.Database.Migration.create_step
~label:"remove timezone info from timestamps"
{sql|
ALTER TABLE token_tokens
ALTER COLUMN created_at TYPE TIMESTAMP;
|sql}
;;
let migration () =
Migration.(
empty "tokens"
|> add_step create_tokens_table
|> add_step remove_timezone)
;;
end
let register_migration () =
MigrationService.register_migration (Migration.migration ())
;;
let register_cleaner () = Cleaner.register_cleaner Sql.clean
let find = Sql.find
let find_opt = Sql.find_opt
let find_by_id = Sql.find_by_id
let insert = Sql.insert
let update = Sql.update
end