builder-web/bin/migrations/m20210218.ml

150 lines
5.2 KiB
OCaml
Raw Normal View History

let old_version = 2L
let new_version = 3L
let identifier = "2021-02-18"
let migrate_doc = "add column size to build_file and build_artifact"
let rollback_doc = "remove column size to build_file and build_artifact"
2021-02-23 14:37:30 +00:00
2022-04-04 16:30:21 +00:00
open Grej.Infix
2021-02-24 08:37:07 +00:00
let new_build_artifact =
2022-04-04 16:30:21 +00:00
Caqti_type.unit ->. Caqti_type.unit @@
{| CREATE TABLE new_build_artifact (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
filepath TEXT NOT NULL, -- the path as in the build
localpath TEXT NOT NULL, -- local path to the file on disk
sha256 BLOB NOT NULL,
size INTEGER NOT NULL,
build INTEGER NOT NULL,
FOREIGN KEY(build) REFERENCES build(id),
UNIQUE(build, filepath)
)
|}
2021-02-24 08:37:07 +00:00
let new_build_file =
2022-04-04 16:30:21 +00:00
Caqti_type.unit ->. Caqti_type.unit @@
{| CREATE TABLE new_build_file (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
filepath TEXT NOT NULL, -- the path as in the build
localpath TEXT NOT NULL, -- local path to the file on disk
sha256 BLOB NOT NULL,
size INTEGER NOT NULL,
build INTEGER NOT NULL,
FOREIGN KEY(build) REFERENCES build(id),
UNIQUE(build, filepath)
)
|}
2021-02-23 14:37:30 +00:00
2021-02-24 08:37:07 +00:00
let collect_build_artifact =
2022-04-04 16:30:21 +00:00
Caqti_type.unit ->* Caqti_type.(tup3 int64 (tup3 string string octets) int64) @@
"SELECT id, filepath, localpath, sha256, build FROM build_artifact"
2021-02-23 14:37:30 +00:00
2021-02-24 08:37:07 +00:00
let collect_build_file =
2022-04-04 16:30:21 +00:00
Caqti_type.unit ->* Caqti_type.(tup3 int64 (tup3 string string octets) int64) @@
"SELECT id, filepath, localpath, sha256, build FROM build_file"
2021-02-24 08:37:07 +00:00
let insert_new_build_artifact =
2022-04-04 16:30:21 +00:00
Caqti_type.(tup3 int64 (tup4 string string octets int64) int64) ->. Caqti_type.unit @@
{| INSERT INTO new_build_artifact (id, filepath, localpath, sha256, size, build)
VALUES (?, ?, ?, ?, ?, ?)
|}
2021-02-24 08:37:07 +00:00
let insert_new_build_file =
2022-04-04 16:30:21 +00:00
Caqti_type.(tup3 int64 (tup4 string string octets int64) int64) ->. Caqti_type.unit @@
{| INSERT INTO new_build_file (id, filepath, localpath, sha256, size, build)
VALUES (?, ?, ?, ?, ?, ?)
|}
2021-02-24 08:37:07 +00:00
let drop_build_artifact =
2022-04-04 16:30:21 +00:00
Caqti_type.unit ->. Caqti_type.unit @@
"DROP TABLE build_artifact"
2021-02-24 08:37:07 +00:00
let drop_build_file =
2022-04-04 16:30:21 +00:00
Caqti_type.unit ->. Caqti_type.unit @@
"DROP TABLE build_file"
2021-02-23 14:37:30 +00:00
2021-02-24 08:37:07 +00:00
let rename_build_artifact =
2022-04-04 16:30:21 +00:00
Caqti_type.unit ->. Caqti_type.unit @@
"ALTER TABLE new_build_artifact RENAME TO build_artifact"
2021-02-23 14:37:30 +00:00
2021-02-24 08:37:07 +00:00
let rename_build_file =
2022-04-04 16:30:21 +00:00
Caqti_type.unit ->. Caqti_type.unit @@
"ALTER TABLE new_build_file RENAME TO build_file"
2021-02-23 14:37:30 +00:00
let migrate _datadir (module Db : Caqti_blocking.CONNECTION) =
Grej.check_version ~user_version:old_version (module Db) >>= fun () ->
2021-03-10 09:50:15 +00:00
Db.exec new_build_artifact () >>= fun () ->
Db.rev_collect_list collect_build_artifact () >>= fun build_artifacts ->
2021-03-12 08:47:28 +00:00
Grej.list_iter_result
(fun (id, (filepath, localpath, sha256), build) ->
2021-03-10 09:50:15 +00:00
let stats = Unix.stat localpath in
Db.exec insert_new_build_artifact
(id, (filepath, localpath, sha256, Int64.of_int stats.st_size), build))
build_artifacts >>= fun () ->
Db.exec drop_build_artifact () >>= fun () ->
Db.exec rename_build_artifact () >>= fun () ->
Db.exec new_build_file () >>= fun () ->
Db.rev_collect_list collect_build_file () >>= fun build_files ->
2021-03-12 08:47:28 +00:00
Grej.list_iter_result
(fun (id, (filepath, localpath, sha256), build) ->
2021-03-10 09:50:15 +00:00
let stats = Unix.stat localpath in
Db.exec insert_new_build_file
(id, (filepath, localpath, sha256, Int64.of_int stats.st_size), build))
build_files >>= fun () ->
Db.exec drop_build_file () >>= fun () ->
Db.exec rename_build_file () >>= fun () ->
Db.exec (Grej.set_version new_version) ()
2021-02-23 14:37:30 +00:00
2021-02-24 14:07:15 +00:00
let old_build_artifact =
2022-04-04 16:30:21 +00:00
Caqti_type.unit ->. Caqti_type.unit @@
{| CREATE TABLE new_build_artifact (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
filepath TEXT NOT NULL, -- the path as in the build
localpath TEXT NOT NULL, -- local path to the file on disk
sha256 BLOB NOT NULL,
build INTEGER NOT NULL,
FOREIGN KEY(build) REFERENCES build(id),
UNIQUE(build, filepath)
)
|}
2021-02-24 14:07:15 +00:00
let old_build_file =
2022-04-04 16:30:21 +00:00
Caqti_type.unit ->. Caqti_type.unit @@
{| CREATE TABLE new_build_file (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
filepath TEXT NOT NULL, -- the path as in the build
localpath TEXT NOT NULL, -- local path to the file on disk
sha256 BLOB NOT NULL,
build INTEGER NOT NULL,
FOREIGN KEY(build) REFERENCES build(id),
UNIQUE(build, filepath)
)
|}
2021-02-24 14:07:15 +00:00
let copy_build_artifact =
2022-04-04 16:30:21 +00:00
Caqti_type.unit ->. Caqti_type.unit @@
"INSERT INTO new_build_artifact SELECT id, filepath, localpath, sha256, build FROM build_artifact"
2021-02-24 14:07:15 +00:00
let copy_build_file =
2022-04-04 16:30:21 +00:00
Caqti_type.unit ->. Caqti_type.unit @@
"INSERT INTO new_build_file SELECT id, filepath, localpath, sha256, build FROM build_file"
2021-02-24 14:07:15 +00:00
let rollback _datadir (module Db : Caqti_blocking.CONNECTION) =
Grej.check_version ~user_version:new_version (module Db) >>= fun () ->
2021-03-10 09:50:15 +00:00
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 copy_build_file () >>= fun () ->
Db.exec drop_build_file () >>= fun () ->
Db.exec rename_build_file () >>= fun () ->
Db.exec (Grej.set_version old_version) ()