Refactor migrations

This commit is contained in:
Reynir Björnsson 2021-03-10 10:50:15 +01:00
parent 3b81c52c59
commit e2a489a74d
6 changed files with 93 additions and 121 deletions

19
bin/migrations/grej.ml Normal file
View 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 ()

View file

@ -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,12 +30,7 @@ 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 ->
if application_id <> 0l || user_version <> 0L
then
Error (`Wrong_version (application_id, user_version))
else
Db.exec alter_build () >>= fun () -> Db.exec alter_build () >>= fun () ->
Db.collect_list all_builds () >>= fun builds -> Db.collect_list all_builds () >>= fun builds ->
List.fold_left (fun r build -> List.fold_left (fun r build ->
@ -59,7 +49,7 @@ let migrate (module Db : Caqti_blocking.CONNECTION) =
(Ok ()) (Ok ())
builds >>= fun () -> builds >>= fun () ->
Db.exec Builder_db.set_application_id () >>= fun () -> Db.exec Builder_db.set_application_id () >>= fun () ->
Db.exec (set_version new_user_version) () Db.exec (Grej.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 ->
if application_id <> Builder_db.application_id || user_version <> new_user_version
then
Error (`Wrong_version (application_id, user_version))
else
Db.exec rename_build () >>= fun () -> Db.exec rename_build () >>= fun () ->
Db.exec create_build () >>= fun () -> Db.exec create_build () >>= fun () ->
Db.exec rollback_data () >>= fun () -> Db.exec rollback_data () >>= fun () ->
Db.exec (set_version 0L) () Db.exec (Grej.set_version 0L) ()

View file

@ -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 ->
if application_id <> Builder_db.application_id || user_version <> 1L
then Error (`Wrong_version (application_id, user_version))
else
Db.exec q () Db.exec q ()

View file

@ -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 ->
if application_id <> Builder_db.application_id || user_version <> old_user_version
then Error (`Wrong_version (application_id, user_version))
else
Db.exec drop_user () >>= fun () -> Db.exec drop_user () >>= fun () ->
Db.exec new_user () >>= fun () -> Db.exec new_user () >>= fun () ->
Db.exec (set_version new_user_version) () Db.exec (Grej.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 ->
if application_id <> Builder_db.application_id || user_version <> new_user_version
then Error (`Wrong_version (application_id, user_version))
else
Db.exec drop_user () >>= fun () -> Db.exec drop_user () >>= fun () ->
Db.exec old_user () >>= fun () -> Db.exec old_user () >>= fun () ->
Db.exec (set_version old_user_version) () Db.exec (Grej.set_version old_user_version) ()

View file

@ -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,11 +81,7 @@ 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 ->
if application_id <> Builder_db.application_id || user_version <> old_user_version
then Error (`Wrong_version (application_id, user_version))
else
Db.exec new_build_artifact () >>= fun () -> Db.exec new_build_artifact () >>= fun () ->
Db.rev_collect_list collect_build_artifact () >>= fun build_artifacts -> Db.rev_collect_list collect_build_artifact () >>= fun build_artifacts ->
List.fold_left List.fold_left
@ -117,7 +108,7 @@ let migrate (module Db : Caqti_blocking.CONNECTION) =
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,11 +152,7 @@ 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 ->
if application_id <> Builder_db.application_id || user_version <> new_user_version
then Error (`Wrong_version (application_id, user_version))
else
Db.exec old_build_artifact () >>= fun () -> Db.exec old_build_artifact () >>= fun () ->
Db.exec copy_build_artifact () >>= fun () -> Db.exec copy_build_artifact () >>= fun () ->
Db.exec drop_build_artifact () >>= fun () -> Db.exec drop_build_artifact () >>= fun () ->
@ -176,4 +163,4 @@ let rollback (module Db : Caqti_blocking.CONNECTION) =
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) ()

View file

@ -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) ->