Source file token_service_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
module Sig = Token_service_sig
module MakeMariaDb
(DbService : Data.Db.Service.Sig.SERVICE)
(RepoService : Data.Repo.Service.Sig.SERVICE)
(MigrationService : Data.Migration.Service.Sig.SERVICE) : Sig.REPOSITORY =
struct
module Sql = struct
module Model = Token_core
let find_request =
Caqti_request.find Caqti_type.string Model.t
{sql|
SELECT
uuid,
token_value,
token_data,
token_kind,
status,
expires_at,
created_at
FROM token_tokens
WHERE token_tokens.token_value = ?
|sql}
let find ctx ~value =
DbService.query ctx (fun (module Connection : Caqti_lwt.CONNECTION) ->
Connection.find find_request value)
let find_opt ctx ~value =
DbService.query ctx (fun (module Connection : Caqti_lwt.CONNECTION) ->
Connection.find_opt find_request value)
let insert_request =
Caqti_request.exec Model.t
{sql|
INSERT INTO token_tokens (
uuid,
token_value,
token_data,
token_kind,
status,
expires_at,
created_at
) VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7
)
|sql}
let insert ctx ~token =
DbService.query ctx (fun (module Connection : Caqti_lwt.CONNECTION) ->
Connection.exec insert_request token)
let clean_request =
Caqti_request.exec Caqti_type.unit "TRUNCATE token_tokens;"
let clean ctx =
DbService.query ctx (fun (module Connection : Caqti_lwt.CONNECTION) ->
Connection.exec clean_request ())
end
module Migration = struct
let fix_collation =
Data.Migration.create_step ~label:"fix collation"
"SET collation_server = 'utf8mb4_unicode_ci'"
let create_tokens_table =
Data.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 migration () =
Data.Migration.(
empty "tokens" |> add_step fix_collation |> add_step create_tokens_table)
end
let register_migration () = MigrationService.register (Migration.migration ())
let register_cleaner () = RepoService.register_cleaner Sql.clean
let find = Sql.find
let find_opt = Sql.find_opt
let insert = Sql.insert
end