package sihl

  1. Overview
  2. Docs

Source file database_migration_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
module Migration = struct
  type t =
    { namespace : string
    ; version : int
    ; dirty : bool
    }
  [@@deriving fields]

  let create ~namespace = { namespace; version = 0; dirty = true }
  let mark_dirty state = { state with dirty = true }
  let mark_clean state = { state with dirty = false }
  let increment state = { state with version = state.version + 1 }

  let steps_to_apply (namespace, steps) { version; _ } =
    namespace, CCList.drop version steps
  ;;

  let of_tuple (namespace, version, dirty) = { namespace; version; dirty }
  let to_tuple state = state.namespace, state.version, state.dirty
  let dirty state = state.dirty
end

module type Sig = sig
  module Migration = Migration

  val create_table_if_not_exists
    :  ?ctx:(string * string) list
    -> string
    -> unit Lwt.t

  val get
    :  ?ctx:(string * string) list
    -> string
    -> namespace:string
    -> Migration.t option Lwt.t

  val get_all : ?ctx:(string * string) list -> string -> Migration.t list Lwt.t

  val upsert
    :  ?ctx:(string * string) list
    -> string
    -> Migration.t
    -> unit Lwt.t
end

(* Common functions *)
let get_request table =
  Caqti_request.find_opt
    Caqti_type.string
    Caqti_type.(tup3 string int bool)
    (Format.sprintf
       {sql|
       SELECT
         namespace,
         version,
         dirty
       FROM %s
       WHERE namespace = ?;
       |sql}
       table)
;;

let get ?ctx table ~namespace =
  Database.find_opt ?ctx (get_request table) namespace
  |> Lwt.map (Option.map Migration.of_tuple)
;;

let get_all_request table =
  Caqti_request.collect
    Caqti_type.unit
    Caqti_type.(tup3 string int bool)
    (Format.sprintf
       {sql|
       SELECT
         namespace,
         version,
         dirty
       FROM %s;
       |sql}
       table)
;;

let get_all ?ctx table =
  Database.collect ?ctx (get_all_request table) ()
  |> Lwt.map (List.map Migration.of_tuple)
;;

module MariaDb : Sig = struct
  module Migration = Migration

  let create_request table =
    Caqti_request.exec
      Caqti_type.unit
      (Format.sprintf
         {sql|
       CREATE TABLE IF NOT EXISTS %s (
         namespace VARCHAR(128) NOT NULL,
         version INTEGER,
         dirty BOOL NOT NULL,
       PRIMARY KEY (namespace)
       ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
      |sql}
         table)
  ;;

  let create_table_if_not_exists ?ctx table =
    Database.exec ?ctx (create_request table) ()
  ;;

  let get = get
  let get_all = get_all

  let upsert_request table =
    Caqti_request.exec
      Caqti_type.(tup3 string int bool)
      (Format.sprintf
         {sql|
       INSERT INTO %s (
         namespace,
         version,
         dirty
       ) VALUES (
         ?,
         ?,
         ?
       ) ON DUPLICATE KEY UPDATE
         version = VALUES(version),
         dirty = VALUES(dirty)
       |sql}
         table)
  ;;

  let upsert ?ctx table state =
    Database.exec ?ctx (upsert_request table) (Migration.to_tuple state)
  ;;
end

module PostgreSql : Sig = struct
  module Migration = Migration

  let create_request table =
    Caqti_request.exec
      Caqti_type.unit
      (Format.sprintf
         {sql|
       CREATE TABLE IF NOT EXISTS %s (
         namespace VARCHAR(128) NOT NULL PRIMARY KEY,
         version INTEGER,
         dirty BOOL NOT NULL
       );
       |sql}
         table)
  ;;

  let create_table_if_not_exists ?ctx table =
    Database.exec ?ctx (create_request table) ()
  ;;

  let get = get
  let get_all = get_all

  let upsert_request table =
    Caqti_request.exec
      Caqti_type.(tup3 string int bool)
      (Format.sprintf
         {sql|
       INSERT INTO %s (
         namespace,
         version,
         dirty
       ) VALUES (
         ?,
         ?,
         ?
       ) ON CONFLICT (namespace)
       DO UPDATE SET version = EXCLUDED.version,
         dirty = EXCLUDED.dirty
       |sql}
         table)
  ;;

  let upsert ?ctx table state =
    Database.exec ?ctx (upsert_request table) (Migration.to_tuple state)
  ;;
end
OCaml

Innovation. Community. Security.