package sihl-email

  1. Overview
  2. Docs

Source file template_repo_sql.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
module type Sig = sig
  val lifecycles : Sihl.Container.lifecycle list
  val register_migration : unit -> unit
  val register_cleaner : unit -> unit

  val get
    :  ?ctx:(string * string) list
    -> string
    -> Sihl.Contract.Email_template.t option Lwt.t

  val get_by_label
    :  ?ctx:(string * string) list
    -> ?language:string
    -> string
    -> Sihl.Contract.Email_template.t option Lwt.t

  val insert
    :  ?ctx:(string * string) list
    -> Sihl.Contract.Email_template.t
    -> unit Lwt.t

  val update
    :  ?ctx:(string * string) list
    -> Sihl.Contract.Email_template.t
    -> unit Lwt.t
end

let template =
  let open Sihl.Contract.Email_template in
  let encode m =
    Ok
      ( m.id
      , (m.label, (m.language, (m.text, (m.html, (m.created_at, m.updated_at)))))
      )
  in
  let decode (id, (label, (language, (text, (html, (created_at, updated_at))))))
    =
    Ok { id; label; language; text; html; created_at; updated_at }
  in
  Caqti_type.(
    custom
      ~encode
      ~decode
      (tup2
         string
         (tup2
            string
            (tup2
               (option string)
               (tup2 string (tup2 (option string) (tup2 ptime ptime)))))))
;;

module MakeMariaDb (MigrationService : Sihl.Contract.Migration.Sig) : Sig =
struct
  let lifecycles = [ Sihl.Database.lifecycle; MigrationService.lifecycle ]

  module Sql = struct
    module Model = Sihl.Contract.Email_template

    let get_request =
      let open Caqti_request.Infix in
      {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)
           )),
          label,
          language,
          content_text,
          content_html,
          created_at,
          updated_at
        FROM email_templates
        WHERE email_templates.uuid = UNHEX(REPLACE(?, '-', ''))
      |sql}
      |> Caqti_type.(string ->? template)
    ;;

    let get ?ctx id = Sihl.Database.find_opt ?ctx get_request id

    let get_by_label_request ?(with_language = false) ctype =
      let open Caqti_request.Infix in
      {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)
           )),
          label,
          language,
          content_text,
          content_html,
          created_at,
          updated_at
        FROM email_templates
        WHERE email_templates.label = ?
      |sql}
      |> (fun sql ->
           if with_language
           then
             {sql| AND email_templates.language = ? |sql}
             |> Format.asprintf "%s\n%s" sql
           else sql)
      |> ctype ->? template
    ;;

    let get_by_label ?ctx ?language label =
      match language with
      | None ->
        Sihl.Database.find_opt
          ?ctx
          (get_by_label_request Caqti_type.string)
          label
      | Some language ->
        Sihl.Database.find_opt
          ?ctx
          (get_by_label_request
             ~with_language:true
             Caqti_type.(tup2 string string))
          (label, language)
    ;;

    let insert_request =
      let open Caqti_request.Infix in
      {sql|
        INSERT INTO email_templates (
          uuid,
          label,
          language,
          content_text,
          content_html,
          created_at,
          updated_at
        ) VALUES (
          UNHEX(REPLACE(?, '-', '')),
          ?,
          ?,
          ?,
          ?,
          ?,
          ?
        )
      |sql}
      |> template ->. Caqti_type.unit
    ;;

    let insert ?ctx template = Sihl.Database.exec ?ctx insert_request template

    let update_request =
      let open Caqti_request.Infix in
      {sql|
        UPDATE email_templates
        SET
          label = $2,
          language = $3,
          content_text = $4,
          content_html = $5,
          created_at = $6,
          updated_at = $7
        WHERE email_templates.uuid = UNHEX(REPLACE($1, '-', ''))
      |sql}
      |> template ->. Caqti_type.unit
    ;;

    let update ?ctx template = Sihl.Database.exec ?ctx update_request template

    let clean_request =
      let open Caqti_request.Infix in
      "TRUNCATE TABLE email_templates" |> Caqti_type.(unit ->. unit)
    ;;

    let clean ?ctx () = Sihl.Database.exec ?ctx clean_request ()
  end

  module Migration = struct
    let fix_collation =
      Sihl.Database.Migration.create_step
        ~label:"fix collation"
        "SET collation_server = 'utf8mb4_unicode_ci'"
    ;;

    let create_templates_table =
      Sihl.Database.Migration.create_step
        ~label:"create templates table"
        {sql|
          CREATE TABLE IF NOT EXISTS email_templates (
            id BIGINT UNSIGNED AUTO_INCREMENT,
            uuid BINARY(16) NOT NULL,
            name VARCHAR(128) NOT NULL,
            content_text TEXT NOT NULL,
            content_html TEXT NOT NULL,
            created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            CONSTRAINT unique_uuid UNIQUE KEY (uuid),
            CONSTRAINT unique_name UNIQUE KEY (name)
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
        |sql}
    ;;

    let rename_name_column =
      Sihl.Database.Migration.create_step
        ~label:"rename name column"
        {sql|
          ALTER TABLE email_templates
            CHANGE COLUMN `name` label VARCHAR(128) NOT NULL
        |sql}
    ;;

    let add_updated_at_column =
      Sihl.Database.Migration.create_step
        ~label:"add updated_at column"
        {sql|
          ALTER TABLE email_templates
            ADD COLUMN updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
        |sql}
    ;;

    let make_html_nullable =
      Sihl.Database.Migration.create_step
        ~label:"make html nullable"
        {sql|
          ALTER TABLE email_templates
            MODIFY content_html TEXT NULL
        |sql}
    ;;

    let add_language_column =
      Sihl.Database.Migration.create_step
        ~label:"add language column"
        {sql|
          ALTER TABLE email_templates
            ADD COLUMN language VARCHAR(128) NULL
              AFTER `label`
        |sql}
    ;;

    let remove_unique_label_constraint =
      Sihl.Database.Migration.create_step
        ~label:"remove unique label constraint"
        {sql|
          ALTER TABLE email_templates
            DROP CONSTRAINT unique_name
        |sql}
    ;;

    let make_label_language_combination_unique =
      Sihl.Database.Migration.create_step
        ~label:"make label language combination unique"
        {sql|
          ALTER TABLE email_templates
            ADD CONSTRAINT unique_label_language UNIQUE(label, language)
        |sql}
    ;;

    let migration () =
      Sihl.Database.Migration.(
        empty "email"
        |> add_step fix_collation
        |> add_step create_templates_table
        |> add_step rename_name_column
        |> add_step add_updated_at_column
        |> add_step make_html_nullable
        |> add_step add_language_column
        |> add_step remove_unique_label_constraint
        |> add_step make_label_language_combination_unique)
    ;;
  end

  let register_migration () =
    MigrationService.register_migration (Migration.migration ())
  ;;

  let register_cleaner () = Sihl.Cleaner.register_cleaner Sql.clean
  let get = Sql.get
  let get_by_label = Sql.get_by_label
  let insert = Sql.insert
  let update = Sql.update
end

module MakePostgreSql (MigrationService : Sihl.Contract.Migration.Sig) : Sig =
struct
  let lifecycles = [ Sihl.Database.lifecycle; MigrationService.lifecycle ]

  module Sql = struct
    module Model = Sihl.Contract.Email_template

    let get_request =
      let open Caqti_request.Infix in
      {sql|
        SELECT
          uuid,
          label,
          language,
          content_text,
          content_html,
          created_at,
          updated_at
        FROM email_templates
        WHERE email_templates.uuid = $1::uuid
      |sql}
      |> Caqti_type.string ->? template
    ;;

    let get ?ctx id = Sihl.Database.find_opt ?ctx get_request id

    let get_by_label_request ?(with_language = false) ctype =
      let open Caqti_request.Infix in
      {sql|
        SELECT
          uuid,
          label,
          language,
          content_text,
          content_html,
          created_at,
          updated_at
        FROM email_templates
        WHERE email_templates.label = ?
      |sql}
      |> (fun sql ->
           if with_language
           then
             {sql| AND email_templates.language = ? |sql}
             |> Format.asprintf "%s\n%s" sql
           else sql)
      |> ctype ->? template
    ;;

    let get_by_label ?ctx ?language label =
      match language with
      | None ->
        Sihl.Database.find_opt
          ?ctx
          (get_by_label_request Caqti_type.string)
          label
      | Some language ->
        Sihl.Database.find_opt
          ?ctx
          (get_by_label_request
             ~with_language:true
             Caqti_type.(tup2 string string))
          (label, language)
    ;;

    let insert_request =
      let open Caqti_request.Infix in
      {sql|
        INSERT INTO email_templates (
          uuid,
          label,
          language,
          content_text,
          content_html,
          created_at,
          updated_at
        ) VALUES (
          $1::uuid,
          $2,
          $3,
          $4,
          $5,
          $6 AT TIME ZONE 'UTC',
          $7 AT TIME ZONE 'UTC'
        )
      |sql}
      |> template ->. Caqti_type.unit
    ;;

    let insert ?ctx template = Sihl.Database.exec ?ctx insert_request template

    let update_request =
      let open Caqti_request.Infix in
      {sql|
        UPDATE email_templates
        SET
          label = $2,
          language = $3,
          content_text = $4,
          content_html = $5,
          created_at = $6 AT TIME ZONE 'UTC',
          updated_at = $7 AT TIME ZONE 'UTC'
        WHERE email_templates.uuid = $1::uuid
      |sql}
      |> template ->. Caqti_type.unit
    ;;

    let update ?ctx template = Sihl.Database.exec ?ctx update_request template

    let clean_request =
      let open Caqti_request.Infix in
      "TRUNCATE TABLE email_templates CASCADE" |> Caqti_type.(unit ->. unit)
    ;;

    let clean ?ctx () = Sihl.Database.exec ?ctx clean_request ()
  end

  module Migration = struct
    let create_templates_table =
      Sihl.Database.Migration.create_step
        ~label:"create templates table"
        {sql|
          CREATE TABLE IF NOT EXISTS email_templates (
            id SERIAL,
            uuid UUID NOT NULL,
            name VARCHAR(128) NOT NULL,
            content_text TEXT NOT NULL,
            content_html TEXT NOT NULL,
            created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
            PRIMARY KEY (id),
            UNIQUE (uuid),
            UNIQUE (name)
          )
        |sql}
    ;;

    let rename_name_column =
      Sihl.Database.Migration.create_step
        ~label:"rename name column"
        {sql|
          ALTER TABLE email_templates
            RENAME COLUMN name TO label
        |sql}
    ;;

    let add_updated_at_column =
      Sihl.Database.Migration.create_step
        ~label:"add updated_at column"
        {sql|
          ALTER TABLE email_templates
            ADD COLUMN updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
        |sql}
    ;;

    let make_html_nullable =
      Sihl.Database.Migration.create_step
        ~label:"make html nullable"
        {sql|
          ALTER TABLE email_templates
            ALTER COLUMN content_html DROP NOT NULL
        |sql}
    ;;

    let remove_timezone =
      Sihl.Database.Migration.create_step
        ~label:"remove timezone info from timestamps"
        {sql|
          ALTER TABLE email_templates
            ALTER COLUMN created_at TYPE TIMESTAMP,
            ALTER COLUMN updated_at TYPE TIMESTAMP
        |sql}
    ;;

    let add_language_column =
      Sihl.Database.Migration.create_step
        ~label:"add language column"
        {sql|
          ALTER TABLE email_templates
            ADD COLUMN language VARCHAR(128) NULL
        |sql}
    ;;

    let remove_unique_label_constraint =
      Sihl.Database.Migration.create_step
        ~label:"remove unique label constraint"
        {sql|
          ALTER TABLE email_templates
            DROP CONSTRAINT email_templates_name_key
        |sql}
    ;;

    let make_label_language_combination_unique =
      Sihl.Database.Migration.create_step
        ~label:"make label language combination unique"
        {sql|
          ALTER TABLE email_templates
            ADD CONSTRAINT email_templates_label_language_key UNIQUE(label, language)
        |sql}
    ;;

    let migration () =
      Sihl.Database.Migration.(
        empty "email"
        |> add_step create_templates_table
        |> add_step rename_name_column
        |> add_step add_updated_at_column
        |> add_step make_html_nullable
        |> add_step remove_timezone
        |> add_step add_language_column
        |> add_step remove_unique_label_constraint
        |> add_step make_label_language_combination_unique)
    ;;
  end

  let register_migration () =
    MigrationService.register_migration (Migration.migration ())
  ;;

  let register_cleaner () = Sihl.Cleaner.register_cleaner Sql.clean
  let get = Sql.get
  let get_by_label = Sql.get_by_label
  let insert = Sql.insert
  let update = Sql.update
end