Refactor migrations
This commit is contained in:
parent
3b81c52c59
commit
e2a489a74d
6 changed files with 93 additions and 121 deletions
19
bin/migrations/grej.ml
Normal file
19
bin/migrations/grej.ml
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
(* Grej is utilities *)
|
||||||
|
open Rresult.R.Infix
|
||||||
|
|
||||||
|
let set_version version =
|
||||||
|
Caqti_request.exec ~oneshot:true
|
||||||
|
Caqti_type.unit
|
||||||
|
(Printf.sprintf "PRAGMA user_version = %Ld" version)
|
||||||
|
|
||||||
|
let check_version
|
||||||
|
?application_id:(desired_application_id=Builder_db.application_id)
|
||||||
|
~user_version:desired_user_version
|
||||||
|
(module Db : Caqti_blocking.CONNECTION) =
|
||||||
|
Db.find Builder_db.get_application_id () >>= fun application_id ->
|
||||||
|
Db.find Builder_db.get_version () >>= fun user_version ->
|
||||||
|
if application_id <> desired_application_id || user_version <> desired_user_version
|
||||||
|
then Error (`Wrong_version (application_id, user_version))
|
||||||
|
else Ok ()
|
||||||
|
|
||||||
|
|
|
@ -6,11 +6,6 @@ let set_application_id =
|
||||||
Caqti_type.unit
|
Caqti_type.unit
|
||||||
(Printf.sprintf "PRAGMA application_id = %ld" Builder_db.application_id)
|
(Printf.sprintf "PRAGMA application_id = %ld" Builder_db.application_id)
|
||||||
|
|
||||||
let set_version version =
|
|
||||||
Caqti_request.exec ~oneshot:true
|
|
||||||
Caqti_type.unit
|
|
||||||
(Printf.sprintf "PRAGMA user_version = %Ld" version)
|
|
||||||
|
|
||||||
let alter_build =
|
let alter_build =
|
||||||
Caqti_request.exec ~oneshot:true
|
Caqti_request.exec ~oneshot:true
|
||||||
Caqti_type.unit
|
Caqti_type.unit
|
||||||
|
@ -35,31 +30,26 @@ let set_main_binary =
|
||||||
|
|
||||||
let migrate (module Db : Caqti_blocking.CONNECTION) =
|
let migrate (module Db : Caqti_blocking.CONNECTION) =
|
||||||
let open Rresult.R.Infix in
|
let open Rresult.R.Infix in
|
||||||
Db.find Builder_db.get_application_id () >>= fun application_id ->
|
Grej.check_version ~application_id:0l ~user_version:0L (module Db) >>= fun () ->
|
||||||
Db.find Builder_db.get_version () >>= fun user_version ->
|
Db.exec alter_build () >>= fun () ->
|
||||||
if application_id <> 0l || user_version <> 0L
|
Db.collect_list all_builds () >>= fun builds ->
|
||||||
then
|
List.fold_left (fun r build ->
|
||||||
Error (`Wrong_version (application_id, user_version))
|
r >>= fun () ->
|
||||||
else
|
Db.collect_list bin_artifact build >>= function
|
||||||
Db.exec alter_build () >>= fun () ->
|
| [_id, main_binary] ->
|
||||||
Db.collect_list all_builds () >>= fun builds ->
|
Db.exec set_main_binary (build, Some main_binary)
|
||||||
List.fold_left (fun r build ->
|
| [] ->
|
||||||
r >>= fun () ->
|
Logs.debug (fun m -> m "No binaries for build id %Ld" build);
|
||||||
Db.collect_list bin_artifact build >>= function
|
Ok ()
|
||||||
| [_id, main_binary] ->
|
| binaries ->
|
||||||
Db.exec set_main_binary (build, Some main_binary)
|
Logs.warn (fun m -> m "More than one binary for build id %Ld" build);
|
||||||
| [] ->
|
Logs.debug (fun m -> m "binaries: [%a]" Fmt.(list ~sep:(any ";") string)
|
||||||
Logs.debug (fun m -> m "No binaries for build id %Ld" build);
|
(List.map snd binaries));
|
||||||
Ok ()
|
Ok ())
|
||||||
| binaries ->
|
(Ok ())
|
||||||
Logs.warn (fun m -> m "More than one binary for build id %Ld" build);
|
builds >>= fun () ->
|
||||||
Logs.debug (fun m -> m "binaries: [%a]" Fmt.(list ~sep:(any ";") string)
|
Db.exec Builder_db.set_application_id () >>= fun () ->
|
||||||
(List.map snd binaries));
|
Db.exec (Grej.set_version new_user_version) ()
|
||||||
Ok ())
|
|
||||||
(Ok ())
|
|
||||||
builds >>= fun () ->
|
|
||||||
Db.exec Builder_db.set_application_id () >>= fun () ->
|
|
||||||
Db.exec (set_version new_user_version) ()
|
|
||||||
|
|
||||||
let rename_build =
|
let rename_build =
|
||||||
Caqti_request.exec
|
Caqti_request.exec
|
||||||
|
@ -98,13 +88,8 @@ let rollback_data =
|
||||||
|
|
||||||
let rollback (module Db : Caqti_blocking.CONNECTION) =
|
let rollback (module Db : Caqti_blocking.CONNECTION) =
|
||||||
let open Rresult.R.Infix in
|
let open Rresult.R.Infix in
|
||||||
Db.find Builder_db.get_application_id () >>= fun application_id ->
|
Grej.check_version ~user_version:new_user_version (module Db) >>= fun () ->
|
||||||
Db.find Builder_db.get_version () >>= fun user_version ->
|
Db.exec rename_build () >>= fun () ->
|
||||||
if application_id <> Builder_db.application_id || user_version <> new_user_version
|
Db.exec create_build () >>= fun () ->
|
||||||
then
|
Db.exec rollback_data () >>= fun () ->
|
||||||
Error (`Wrong_version (application_id, user_version))
|
Db.exec (Grej.set_version 0L) ()
|
||||||
else
|
|
||||||
Db.exec rename_build () >>= fun () ->
|
|
||||||
Db.exec create_build () >>= fun () ->
|
|
||||||
Db.exec rollback_data () >>= fun () ->
|
|
||||||
Db.exec (set_version 0L) ()
|
|
||||||
|
|
|
@ -6,11 +6,8 @@ let migrate (module Db : Caqti_blocking.CONNECTION) =
|
||||||
Caqti_type.unit
|
Caqti_type.unit
|
||||||
"CREATE INDEX job_build_idx ON build(job)";
|
"CREATE INDEX job_build_idx ON build(job)";
|
||||||
in
|
in
|
||||||
Db.find Builder_db.get_application_id () >>= fun application_id ->
|
Grej.check_version ~user_version:1L (module Db) >>= fun () ->
|
||||||
Db.find Builder_db.get_version () >>= fun user_version ->
|
Db.exec job_build_idx ()
|
||||||
if application_id <> Builder_db.application_id || user_version <> 1L
|
|
||||||
then Error (`Wrong_version (application_id, user_version))
|
|
||||||
else Db.exec job_build_idx ()
|
|
||||||
|
|
||||||
let rollback (module Db : Caqti_blocking.CONNECTION) =
|
let rollback (module Db : Caqti_blocking.CONNECTION) =
|
||||||
let q =
|
let q =
|
||||||
|
@ -18,9 +15,5 @@ let rollback (module Db : Caqti_blocking.CONNECTION) =
|
||||||
Caqti_type.unit
|
Caqti_type.unit
|
||||||
"DROP INDEX IF EXISTS job_build_idx"
|
"DROP INDEX IF EXISTS job_build_idx"
|
||||||
in
|
in
|
||||||
Db.find Builder_db.get_application_id () >>= fun application_id ->
|
Grej.check_version ~user_version:1L (module Db) >>= fun () ->
|
||||||
Db.find Builder_db.get_version () >>= fun user_version ->
|
Db.exec q ()
|
||||||
if application_id <> Builder_db.application_id || user_version <> 1L
|
|
||||||
then Error (`Wrong_version (application_id, user_version))
|
|
||||||
else
|
|
||||||
Db.exec q ()
|
|
||||||
|
|
|
@ -1,11 +1,6 @@
|
||||||
let old_user_version = 1L
|
let old_user_version = 1L
|
||||||
let new_user_version = 2L
|
let new_user_version = 2L
|
||||||
|
|
||||||
let set_version version =
|
|
||||||
Caqti_request.exec ~oneshot:true
|
|
||||||
Caqti_type.unit
|
|
||||||
(Printf.sprintf "PRAGMA user_version = %Ld" version)
|
|
||||||
|
|
||||||
let drop_user =
|
let drop_user =
|
||||||
Caqti_request.exec ~oneshot:true
|
Caqti_request.exec ~oneshot:true
|
||||||
Caqti_type.unit
|
Caqti_type.unit
|
||||||
|
@ -39,22 +34,14 @@ let old_user =
|
||||||
|
|
||||||
let migrate (module Db : Caqti_blocking.CONNECTION) =
|
let migrate (module Db : Caqti_blocking.CONNECTION) =
|
||||||
let open Rresult.R.Infix in
|
let open Rresult.R.Infix in
|
||||||
Db.find Builder_db.get_application_id () >>= fun application_id ->
|
Grej.check_version ~user_version:old_user_version (module Db) >>= fun () ->
|
||||||
Db.find Builder_db.get_version () >>= fun user_version ->
|
Db.exec drop_user () >>= fun () ->
|
||||||
if application_id <> Builder_db.application_id || user_version <> old_user_version
|
Db.exec new_user () >>= fun () ->
|
||||||
then Error (`Wrong_version (application_id, user_version))
|
Db.exec (Grej.set_version new_user_version) ()
|
||||||
else
|
|
||||||
Db.exec drop_user () >>= fun () ->
|
|
||||||
Db.exec new_user () >>= fun () ->
|
|
||||||
Db.exec (set_version new_user_version) ()
|
|
||||||
|
|
||||||
let rollback (module Db : Caqti_blocking.CONNECTION) =
|
let rollback (module Db : Caqti_blocking.CONNECTION) =
|
||||||
let open Rresult.R.Infix in
|
let open Rresult.R.Infix in
|
||||||
Db.find Builder_db.get_application_id () >>= fun application_id ->
|
Grej.check_version ~user_version:new_user_version (module Db) >>= fun () ->
|
||||||
Db.find Builder_db.get_version () >>= fun user_version ->
|
Db.exec drop_user () >>= fun () ->
|
||||||
if application_id <> Builder_db.application_id || user_version <> new_user_version
|
Db.exec old_user () >>= fun () ->
|
||||||
then Error (`Wrong_version (application_id, user_version))
|
Db.exec (Grej.set_version old_user_version) ()
|
||||||
else
|
|
||||||
Db.exec drop_user () >>= fun () ->
|
|
||||||
Db.exec old_user () >>= fun () ->
|
|
||||||
Db.exec (set_version old_user_version) ()
|
|
||||||
|
|
|
@ -1,11 +1,6 @@
|
||||||
let old_user_version = 2L
|
let old_user_version = 2L
|
||||||
let new_user_version = 3L
|
let new_user_version = 3L
|
||||||
|
|
||||||
let set_version version =
|
|
||||||
Caqti_request.exec ~oneshot:true
|
|
||||||
Caqti_type.unit
|
|
||||||
(Printf.sprintf "PRAGMA user_version = %Ld" version)
|
|
||||||
|
|
||||||
let new_build_artifact =
|
let new_build_artifact =
|
||||||
Caqti_request.exec ~oneshot:true
|
Caqti_request.exec ~oneshot:true
|
||||||
Caqti_type.unit
|
Caqti_type.unit
|
||||||
|
@ -86,38 +81,34 @@ let rename_build_file =
|
||||||
|
|
||||||
let migrate (module Db : Caqti_blocking.CONNECTION) =
|
let migrate (module Db : Caqti_blocking.CONNECTION) =
|
||||||
let open Rresult.R.Infix in
|
let open Rresult.R.Infix in
|
||||||
Db.find Builder_db.get_application_id () >>= fun application_id ->
|
Grej.check_version ~user_version:old_user_version (module Db) >>= fun () ->
|
||||||
Db.find Builder_db.get_version () >>= fun user_version ->
|
Db.exec new_build_artifact () >>= fun () ->
|
||||||
if application_id <> Builder_db.application_id || user_version <> old_user_version
|
Db.rev_collect_list collect_build_artifact () >>= fun build_artifacts ->
|
||||||
then Error (`Wrong_version (application_id, user_version))
|
List.fold_left
|
||||||
else
|
(fun r (id, (filepath, localpath, sha256), build) ->
|
||||||
Db.exec new_build_artifact () >>= fun () ->
|
r >>= fun () ->
|
||||||
Db.rev_collect_list collect_build_artifact () >>= fun build_artifacts ->
|
let stats = Unix.stat localpath in
|
||||||
List.fold_left
|
Db.exec insert_new_build_artifact
|
||||||
(fun r (id, (filepath, localpath, sha256), build) ->
|
(id, (filepath, localpath, sha256, Int64.of_int stats.st_size), build))
|
||||||
r >>= fun () ->
|
(Ok ())
|
||||||
let stats = Unix.stat localpath in
|
build_artifacts >>= fun () ->
|
||||||
Db.exec insert_new_build_artifact
|
Db.exec drop_build_artifact () >>= fun () ->
|
||||||
(id, (filepath, localpath, sha256, Int64.of_int stats.st_size), build))
|
Db.exec rename_build_artifact () >>= fun () ->
|
||||||
(Ok ())
|
|
||||||
build_artifacts >>= fun () ->
|
|
||||||
Db.exec drop_build_artifact () >>= fun () ->
|
|
||||||
Db.exec rename_build_artifact () >>= fun () ->
|
|
||||||
|
|
||||||
Db.exec new_build_file () >>= fun () ->
|
Db.exec new_build_file () >>= fun () ->
|
||||||
Db.rev_collect_list collect_build_file () >>= fun build_files ->
|
Db.rev_collect_list collect_build_file () >>= fun build_files ->
|
||||||
List.fold_left
|
List.fold_left
|
||||||
(fun r (id, (filepath, localpath, sha256), build) ->
|
(fun r (id, (filepath, localpath, sha256), build) ->
|
||||||
r >>= fun () ->
|
r >>= fun () ->
|
||||||
let stats = Unix.stat localpath in
|
let stats = Unix.stat localpath in
|
||||||
Db.exec insert_new_build_file
|
Db.exec insert_new_build_file
|
||||||
(id, (filepath, localpath, sha256, Int64.of_int stats.st_size), build))
|
(id, (filepath, localpath, sha256, Int64.of_int stats.st_size), build))
|
||||||
(Ok ())
|
(Ok ())
|
||||||
build_files >>= fun () ->
|
build_files >>= fun () ->
|
||||||
Db.exec drop_build_file () >>= fun () ->
|
Db.exec drop_build_file () >>= fun () ->
|
||||||
Db.exec rename_build_file () >>= fun () ->
|
Db.exec rename_build_file () >>= fun () ->
|
||||||
|
|
||||||
Db.exec (set_version new_user_version) ()
|
Db.exec (Grej.set_version new_user_version) ()
|
||||||
|
|
||||||
let old_build_artifact =
|
let old_build_artifact =
|
||||||
Caqti_request.exec ~oneshot:true
|
Caqti_request.exec ~oneshot:true
|
||||||
|
@ -161,19 +152,15 @@ let copy_build_file =
|
||||||
|
|
||||||
let rollback (module Db : Caqti_blocking.CONNECTION) =
|
let rollback (module Db : Caqti_blocking.CONNECTION) =
|
||||||
let open Rresult.R.Infix in
|
let open Rresult.R.Infix in
|
||||||
Db.find Builder_db.get_application_id () >>= fun application_id ->
|
Grej.check_version ~user_version:new_user_version (module Db) >>= fun () ->
|
||||||
Db.find Builder_db.get_version () >>= fun user_version ->
|
Db.exec old_build_artifact () >>= fun () ->
|
||||||
if application_id <> Builder_db.application_id || user_version <> new_user_version
|
Db.exec copy_build_artifact () >>= fun () ->
|
||||||
then Error (`Wrong_version (application_id, user_version))
|
Db.exec drop_build_artifact () >>= fun () ->
|
||||||
else
|
Db.exec rename_build_artifact () >>= fun () ->
|
||||||
Db.exec old_build_artifact () >>= fun () ->
|
|
||||||
Db.exec copy_build_artifact () >>= fun () ->
|
|
||||||
Db.exec drop_build_artifact () >>= fun () ->
|
|
||||||
Db.exec rename_build_artifact () >>= fun () ->
|
|
||||||
|
|
||||||
Db.exec old_build_file () >>= fun () ->
|
Db.exec old_build_file () >>= fun () ->
|
||||||
Db.exec copy_build_file () >>= fun () ->
|
Db.exec copy_build_file () >>= fun () ->
|
||||||
Db.exec drop_build_file () >>= fun () ->
|
Db.exec drop_build_file () >>= fun () ->
|
||||||
Db.exec rename_build_file () >>= fun () ->
|
Db.exec rename_build_file () >>= fun () ->
|
||||||
|
|
||||||
Db.exec (set_version old_user_version) ()
|
Db.exec (Grej.set_version old_user_version) ()
|
||||||
|
|
|
@ -12,6 +12,7 @@ let broken_builds =
|
||||||
|
|
||||||
let fixup (module Db : Caqti_blocking.CONNECTION) =
|
let fixup (module Db : Caqti_blocking.CONNECTION) =
|
||||||
let open Rresult.R.Infix in
|
let open Rresult.R.Infix in
|
||||||
|
Grej.check_version ~user_version:3L (module Db) >>= fun () ->
|
||||||
Db.rev_collect_list broken_builds () >>= fun broken_builds ->
|
Db.rev_collect_list broken_builds () >>= fun broken_builds ->
|
||||||
List.fold_left
|
List.fold_left
|
||||||
(fun r ((build, uuid, job_name) : Rep.id * Uuidm.t * string) ->
|
(fun r ((build, uuid, job_name) : Rep.id * Uuidm.t * string) ->
|
||||||
|
|
Loading…
Reference in a new issue