Fix migration M20210712c

This commit is contained in:
Robur 2021-09-07 08:38:55 +00:00
parent 94feffdcc2
commit 4126cab805
2 changed files with 11 additions and 8 deletions

View file

@ -1,4 +1,4 @@
(executable (executable
(public_name builder-migrations) (public_name builder-migrations)
(name builder_migrations) (name builder_migrations)
(libraries builder_db caqti caqti-driver-sqlite3 caqti.blocking cmdliner logs logs.cli logs.fmt opam-format bos)) (libraries builder_db caqti caqti-driver-sqlite3 caqti.blocking cmdliner logs logs.cli logs.fmt opam-format bos duration))

View file

@ -3,6 +3,8 @@ and identifier = "2021-07-12c"
and migrate_doc = "store script, console on disk" and migrate_doc = "store script, console on disk"
and rollback_doc = "store script, console in database" and rollback_doc = "store script, console in database"
open Rresult.R.Infix
module Asn = struct module Asn = struct
let decode_strict codec cs = let decode_strict codec cs =
match Asn.decode codec cs with match Asn.decode codec cs with
@ -99,7 +101,7 @@ let old_build_console_script =
Caqti_request.collect Caqti_request.collect
Caqti_type.unit Caqti_type.unit
Caqti_type.(tup4 (Builder_db.Rep.id (`build : [ `build ])) Caqti_type.(tup4 (Builder_db.Rep.id (`build : [ `build ]))
(tup2 string Builder_db.Rep.uuid) octets string) (tup2 string Builder_db.Rep.uuid) Builder_db.Rep.cstruct string)
"SELECT b.id, job.name, b.uuid, b.console, b.script FROM build b, job WHERE b.job = job.id" "SELECT b.id, job.name, b.uuid, b.console, b.script FROM build b, job WHERE b.job = job.id"
let update_new_build_console_script = let update_new_build_console_script =
@ -115,7 +117,7 @@ let new_build_console_script =
let update_old_build_console_script = let update_old_build_console_script =
Caqti_request.exec Caqti_request.exec
Caqti_type.(tup3 (Builder_db.Rep.id (`build : [ `build ])) octets string) Caqti_type.(tup3 (Builder_db.Rep.id (`build : [ `build ])) Builder_db.Rep.cstruct string)
"UPDATE new_build SET console = ?2, script = ?3 WHERE id = ?1" "UPDATE new_build SET console = ?2, script = ?3 WHERE id = ?1"
let drop_build = let drop_build =
@ -129,9 +131,10 @@ let rename_build =
"ALTER TABLE new_build RENAME TO build" "ALTER TABLE new_build RENAME TO build"
let console_to_string console = let console_to_string console =
Asn.console_of_cs console >>| fun console -> Asn.console_of_cs console
|> Rresult.R.reword_error (fun s -> `Msg s) >>| fun console ->
List.map (fun (delta, data) -> List.map (fun (delta, data) ->
Printf.sprintf "%.3fs:%s\n" (Duration.to_f delta) data) Printf.sprintf "%.3fs:%s\n" (Duration.to_f (Int64.of_int delta)) data)
console console
|> String.concat "" |> String.concat ""
@ -147,7 +150,7 @@ let console_of_string data =
match String.split_on_char ':' line with match String.split_on_char ':' line with
| ts :: tail -> | ts :: tail ->
let delta = float_of_string (String.sub ts 0 (String.length ts - 1)) in let delta = float_of_string (String.sub ts 0 (String.length ts - 1)) in
Duration.of_f delta, String.concat ":" tail Int64.to_int (Duration.of_f delta), String.concat ":" tail
| _ -> assert false) | _ -> assert false)
lines lines
in in
@ -158,7 +161,7 @@ let save_console_and_script datadir job_name uuid console script =
let script_out = out "script" in let script_out = out "script" in
Bos.OS.File.write script_out script >>= fun () -> Bos.OS.File.write script_out script >>= fun () ->
let console_out = out "console" in let console_out = out "console" in
let console_data = console_to_string console in console_to_string console >>= fun console_data ->
Bos.OS.File.write console_out console_data >>= fun () -> Bos.OS.File.write console_out console_data >>= fun () ->
Ok (console_out, script_out) Ok (console_out, script_out)
@ -167,7 +170,7 @@ let read_console_and_script datadir console_file script_file =
and script_file = Fpath.append datadir script_file and script_file = Fpath.append datadir script_file
in in
Bos.OS.File.read console_file >>= fun console -> Bos.OS.File.read console_file >>= fun console ->
Bos.OS.File.read script_fle >>= fun script -> Bos.OS.File.read script_file >>= fun script ->
let console = console_of_string console in let console = console_of_string console in
Bos.OS.File.delete console_file >>= fun () -> Bos.OS.File.delete console_file >>= fun () ->
Bos.OS.File.delete script_file >>= fun () -> Bos.OS.File.delete script_file >>= fun () ->