2021-06-10 10:08:14 +00:00
|
|
|
type action = Fpath.t -> Caqti_blocking.connection ->
|
|
|
|
(unit, [ Caqti_error.call_or_retrieve | `Wrong_version of int32 * int64 | `Msg of string ]) result
|
|
|
|
|
|
|
|
module type MIGRATION = sig
|
|
|
|
val new_version : int64
|
|
|
|
val old_version : int64
|
|
|
|
val identifier : string
|
|
|
|
val migrate_doc : string
|
|
|
|
val rollback_doc : string
|
|
|
|
val migrate : action
|
|
|
|
val rollback : action
|
|
|
|
end
|
|
|
|
|
2021-10-20 09:10:43 +00:00
|
|
|
|
2021-01-27 20:25:51 +00:00
|
|
|
let pp_error ppf = function
|
|
|
|
| #Caqti_error.load_or_connect | #Caqti_error.call_or_retrieve as e ->
|
|
|
|
Caqti_error.pp ppf e
|
|
|
|
| `Wrong_version (application_id, user_version) ->
|
|
|
|
Format.fprintf ppf "wrong version { application_id: %ld, user_version: %Ld }"
|
|
|
|
application_id user_version
|
2021-06-01 15:43:55 +00:00
|
|
|
| `Msg m ->
|
|
|
|
Format.fprintf ppf "%s" m
|
2021-01-27 20:25:51 +00:00
|
|
|
|
|
|
|
let or_die exit_code = function
|
|
|
|
| Ok r -> r
|
|
|
|
| Error e ->
|
2021-11-12 13:01:40 +00:00
|
|
|
Format.eprintf "Database error: %a\n" pp_error e;
|
2021-01-27 20:25:51 +00:00
|
|
|
exit exit_code
|
|
|
|
|
2021-06-01 15:43:55 +00:00
|
|
|
let do_database_action action () datadir =
|
2021-10-20 09:10:43 +00:00
|
|
|
let ( let* ) = Result.bind in
|
2021-06-01 15:43:55 +00:00
|
|
|
let datadir = Fpath.v datadir in
|
|
|
|
let dbpath = Fpath.(datadir / "builder.sqlite3") in
|
2021-01-27 20:25:51 +00:00
|
|
|
Logs.debug (fun m -> m "Connecting to database...");
|
|
|
|
let ((module Db : Caqti_blocking.CONNECTION) as conn) =
|
|
|
|
Caqti_blocking.connect
|
2021-06-01 15:43:55 +00:00
|
|
|
(Uri.make ~scheme:"sqlite3" ~path:(Fpath.to_string dbpath) ~query:["create", ["false"]] ())
|
2021-01-27 20:25:51 +00:00
|
|
|
|> or_die 1
|
|
|
|
in
|
|
|
|
Logs.debug (fun m -> m "Connected!");
|
|
|
|
let r =
|
2021-10-20 09:10:43 +00:00
|
|
|
let* () = Db.start () in
|
2021-01-27 20:25:51 +00:00
|
|
|
Logs.debug (fun m -> m "Started database transaction");
|
2021-06-01 15:43:55 +00:00
|
|
|
match action datadir conn with
|
2021-01-27 20:25:51 +00:00
|
|
|
| Ok () ->
|
|
|
|
Logs.debug (fun m -> m "Committing database transaction");
|
|
|
|
Db.commit ()
|
|
|
|
| Error _ as e ->
|
|
|
|
Logs.debug (fun m -> m "Rolling back database transaction");
|
2021-10-20 09:10:43 +00:00
|
|
|
let* () = Db.rollback () in
|
2021-01-27 20:25:51 +00:00
|
|
|
e
|
|
|
|
in
|
|
|
|
or_die 2 r
|
|
|
|
|
|
|
|
let help man_format migrations = function
|
|
|
|
| None -> `Help (man_format, None)
|
|
|
|
| Some migration ->
|
|
|
|
if List.mem migration migrations
|
|
|
|
then `Help (man_format, Some migration)
|
|
|
|
else `Error (true, "Unknown migration: " ^ migration)
|
|
|
|
|
2021-06-01 15:43:55 +00:00
|
|
|
let datadir =
|
|
|
|
let doc = "data directory containing builder.sqlite3 and data files" in
|
2021-01-27 20:25:51 +00:00
|
|
|
Cmdliner.Arg.(value &
|
2021-11-12 13:21:12 +00:00
|
|
|
opt dir Builder_system.default_datadir &
|
2022-02-21 13:08:58 +00:00
|
|
|
info ~doc ["datadir"; "d"])
|
2021-01-27 20:25:51 +00:00
|
|
|
|
|
|
|
let setup_log =
|
|
|
|
let setup_log level =
|
|
|
|
Logs.set_level level;
|
|
|
|
Logs.set_reporter (Logs_fmt.reporter ~dst:Format.std_formatter ());
|
|
|
|
in
|
|
|
|
Cmdliner.Term.(const setup_log $ Logs_cli.level ())
|
|
|
|
|
2022-02-21 13:46:57 +00:00
|
|
|
open Cmdliner
|
|
|
|
|
2021-06-10 10:08:14 +00:00
|
|
|
let actions (module M : MIGRATION) =
|
|
|
|
let c s = s ^ "-" ^ M.identifier in
|
|
|
|
let v doc from_ver to_ver = Printf.sprintf "%s (DB version %Ld -> %Ld)" doc from_ver to_ver in
|
2022-02-21 13:46:57 +00:00
|
|
|
let migrate_cmd =
|
|
|
|
let term = Term.(
|
|
|
|
const do_database_action $ const M.migrate $ setup_log $ datadir) in
|
|
|
|
let info = Cmd.info ~doc:(v M.migrate_doc M.old_version M.new_version)
|
|
|
|
(c "migrate") in
|
|
|
|
Cmd.v info term
|
|
|
|
in
|
|
|
|
let rollback_cmd =
|
|
|
|
let term = Term.(
|
|
|
|
const do_database_action $ const M.rollback $ setup_log $ datadir) in
|
|
|
|
let info = Cmd.info ~doc:(v M.rollback_doc M.new_version M.old_version)
|
|
|
|
(c "rollback") in
|
|
|
|
Cmd.v info term
|
|
|
|
in
|
|
|
|
[ migrate_cmd; rollback_cmd ]
|
2021-02-24 14:07:15 +00:00
|
|
|
|
2021-03-08 16:01:00 +00:00
|
|
|
let f20210308 =
|
|
|
|
let doc = "Remove broken builds as fixed in commit a57798f4c02eb4d528b90932ec26fb0b718f1a13. \
|
|
|
|
Note that the files on disk have to be removed manually." in
|
2022-02-21 13:46:57 +00:00
|
|
|
let term = Term.(const do_database_action $ const M20210308.fixup $ setup_log $ datadir) in
|
|
|
|
let info = Cmd.info ~doc "fixup-2021-03-08" in
|
|
|
|
Cmd.v info term
|
2021-03-08 16:01:00 +00:00
|
|
|
|
2021-07-07 10:46:53 +00:00
|
|
|
let f20210707a =
|
|
|
|
let doc = "Remove orb.deb and orb.txz that ended up in the build." in
|
2022-02-21 13:46:57 +00:00
|
|
|
let term = Term.(const do_database_action $ const M20210707a.fixup $ setup_log $ datadir) in
|
|
|
|
let info = Cmd.info ~doc "fixup-2021-07-07a" in
|
|
|
|
Cmd.v info term
|
2021-07-07 10:46:53 +00:00
|
|
|
|
2021-07-07 11:28:24 +00:00
|
|
|
let f20210707b =
|
|
|
|
let doc = "Move *.deb.debug to bin/*.deb and remove the earlier bin/*.deb. Adjust main_binary of build." in
|
2022-02-21 13:46:57 +00:00
|
|
|
let term = Term.(const do_database_action $ const M20210707b.fixup $ setup_log $ datadir) in
|
|
|
|
let info = Cmd.info ~doc "fixup-2021-07-07b" in
|
|
|
|
Cmd.v info term
|
2021-07-07 11:28:24 +00:00
|
|
|
|
2021-07-07 12:36:30 +00:00
|
|
|
let f20210707c =
|
|
|
|
let doc = "Strip bin/*.{hvt,xen} if no *.{hvt,xen} exists. Adjust build_artifact table and main_binary of build." in
|
2022-02-21 13:46:57 +00:00
|
|
|
let term = Term.(const do_database_action $ const M20210707c.fixup $ setup_log $ datadir) in
|
|
|
|
let info = Cmd.info ~doc "fixup-2021-07-07c" in
|
|
|
|
Cmd.v info term
|
2021-07-07 12:36:30 +00:00
|
|
|
|
2021-07-07 13:00:57 +00:00
|
|
|
let f20210707d =
|
|
|
|
let doc = "Remove ./ from filepath." in
|
2022-02-21 13:46:57 +00:00
|
|
|
let term = Term.(const do_database_action $ const M20210707d.fixup $ setup_log $ datadir) in
|
|
|
|
let info = Cmd.info ~doc "fixup-2021-07-07d" in
|
|
|
|
Cmd.v info term
|
2021-07-07 13:00:57 +00:00
|
|
|
|
2021-07-12 14:10:54 +00:00
|
|
|
let f20210712b =
|
|
|
|
let doc = "Remove build-hashes and README from artifacts." in
|
2022-02-21 13:46:57 +00:00
|
|
|
let term = Term.(const do_database_action $ const M20210712b.fixup $ setup_log $ datadir) in
|
|
|
|
let info = Cmd.info ~doc "fixup-2021-07-12b" in
|
|
|
|
Cmd.v info term
|
2021-07-12 14:10:54 +00:00
|
|
|
|
2021-09-10 10:52:31 +00:00
|
|
|
let f20210910 =
|
|
|
|
let doc = "Undo builds with script and console mixed up." in
|
2022-02-21 13:46:57 +00:00
|
|
|
let term = Term.(const do_database_action $ const M20210910.fixup $ setup_log $ datadir) in
|
|
|
|
let info = Cmd.info ~doc "fixup-2021-09-10" in
|
|
|
|
Cmd.v info term
|
2021-09-10 10:52:31 +00:00
|
|
|
|
2021-01-27 20:25:51 +00:00
|
|
|
let help_cmd =
|
|
|
|
let topic =
|
|
|
|
let doc = "Migration to get help on" in
|
|
|
|
Cmdliner.Arg.(value & pos 0 (some string) None & info ~doc ~docv:"MIGRATION" [])
|
|
|
|
in
|
|
|
|
let doc = "Builder migration help" in
|
2022-02-21 13:46:57 +00:00
|
|
|
let term = Term.(ret (const help $ Arg.man_format $ choice_names $ topic)) in
|
|
|
|
let info = Cmd.info ~doc "help" in
|
|
|
|
Cmd.v info term
|
2021-01-27 20:25:51 +00:00
|
|
|
|
|
|
|
let () =
|
2022-02-21 13:46:57 +00:00
|
|
|
let doc = "Builder migration command" in
|
|
|
|
let default_term = Term.(ret (const help $ Arg.man_format $ choice_names $ const None)) in
|
|
|
|
let default_info = Cmd.info ~doc "builder-migrations" in
|
|
|
|
Cmd.group
|
|
|
|
~default:default_term default_info
|
2021-06-10 10:08:14 +00:00
|
|
|
(List.concat [
|
|
|
|
[ help_cmd ];
|
|
|
|
actions (module M20210126);
|
|
|
|
actions (module M20210202);
|
|
|
|
actions (module M20210216);
|
|
|
|
actions (module M20210218);
|
|
|
|
[ f20210308 ];
|
|
|
|
actions (module M20210427);
|
|
|
|
actions (module M20210531);
|
|
|
|
actions (module M20210602);
|
|
|
|
actions (module M20210608);
|
|
|
|
actions (module M20210609);
|
2021-06-25 10:26:03 +00:00
|
|
|
actions (module M20210625);
|
2021-06-29 14:59:08 +00:00
|
|
|
actions (module M20210629);
|
2021-06-30 12:47:30 +00:00
|
|
|
actions (module M20210630);
|
2021-07-01 08:31:32 +00:00
|
|
|
actions (module M20210701);
|
2021-07-06 13:41:26 +00:00
|
|
|
actions (module M20210706);
|
2021-07-07 10:46:53 +00:00
|
|
|
[ f20210707a ];
|
2021-07-07 11:28:24 +00:00
|
|
|
[ f20210707b ];
|
2021-07-07 12:36:30 +00:00
|
|
|
[ f20210707c ];
|
2021-07-07 13:00:57 +00:00
|
|
|
[ f20210707d ];
|
2021-07-12 14:10:54 +00:00
|
|
|
actions (module M20210712a);
|
|
|
|
[ f20210712b ];
|
2021-09-07 08:31:10 +00:00
|
|
|
actions (module M20210712c);
|
2021-09-10 10:52:31 +00:00
|
|
|
[ f20210910 ];
|
2021-11-05 10:45:26 +00:00
|
|
|
actions (module M20211105);
|
2022-05-09 19:13:32 +00:00
|
|
|
actions (module M20220509);
|
2021-06-10 10:08:14 +00:00
|
|
|
])
|
2022-02-21 13:46:57 +00:00
|
|
|
|> Cmd.eval
|
|
|
|
|> exit
|