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
module MariaDb (MigrationService : Migration.Sig.SERVICE) : Sig.REPOSITORY = struct
module Sql = struct
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_opt ctx ~value =
Database.Service.query ctx (fun (module Connection : Caqti_lwt.CONNECTION) ->
Connection.find_opt find_request value)
;;
let find_by_id_request =
Caqti_request.find
Database.Id.t_string
Model.t
{sql|
SELECT
uuid,
token_value,
token_data,
token_kind,
status,
expires_at,
created_at
FROM token_tokens
WHERE token_tokens.uuid = ?
|sql}
;;
let find_by_id_opt ctx ~id =
Database.Service.query ctx (fun (module Connection : Caqti_lwt.CONNECTION) ->
Connection.find_opt find_by_id_request id)
;;
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 =
Database.Service.query ctx (fun (module Connection : Caqti_lwt.CONNECTION) ->
Connection.exec insert_request token)
;;
let update_request =
Caqti_request.exec
Model.t
{sql|
UPDATE token_tokens
SET
token_data = $3,
token_kind = $4,
status = $5,
expires_at = $6,
created_at = $7
WHERE token_tokens.token_value = $2
|sql}
;;
let update ctx ~token =
Database.Service.query ctx (fun (module Connection : Caqti_lwt.CONNECTION) ->
Connection.exec update_request token)
;;
let clean_request = Caqti_request.exec Caqti_type.unit "TRUNCATE token_tokens;"
let clean ctx =
Database.Service.query ctx (fun (module Connection : Caqti_lwt.CONNECTION) ->
Connection.exec 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 migration () =
Migration.(empty "tokens" |> add_step fix_collation |> add_step create_tokens_table)
;;
end
let register_migration () = MigrationService.register (Migration.migration ())
let register_cleaner () = Repository.Service.register_cleaner Sql.clean
let find_opt = Sql.find_opt
let find_by_id_opt = Sql.find_by_id_opt
let insert = Sql.insert
let update = Sql.update
end