Files
shiny_form/modules/global_options.R

116 lines
3.4 KiB
R
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#' @export
#' @description костыли для упрощения работы себе
set_global_options = function(
SYMBOL_DELIM = "; ",
APP.DEBUG = FALSE,
# APP.FILE_DB = fs::path("data.sqlite"),
shiny.host = "127.0.0.1",
shiny.port = 1337,
...
) {
options(
SYMBOL_DELIM = SYMBOL_DELIM,
APP.DEBUG = APP.DEBUG,
# APP.FILE_DB = APP.FILE_DB,
shiny.host = shiny.host,
shiny.port = shiny.port,
...
)
}
#' @export
enabled_schemas <- c(
`Тестовая база данных` = "example_of_scheme"
# `D2TRA (для отладки)` = "d2tra_t"
)
#' @export
check_and_init_scheme = function() {
cli::cli_inform(c("*" = "проверка схемы..."))
files_to_watch <- c(
"modules/scheme_generator.R"
)
scheme_names <- enabled_schemas
scheme_file <- paste0("configs/schemas/", scheme_names, ".xlsx")
scheme_file <- stats::setNames(scheme_file, scheme_names)
if (!all(file.exists(scheme_file))) cli::cli_abort(c("Отсутствуют файлы схем для следующих наименований:", paste("-", names(scheme_file)[!file.exists(scheme_file)])))
db_files <- paste0("db/", scheme_names, ".sqlite")
hash_file <- "temp/schema_hash.rds"
#
exist_hash <- tools::md5sum(c(scheme_file, files_to_watch))
# если первый запуск (нет файла с кешем) инициализация схемы
if (!file.exists(hash_file) | !file.exists("scheme.rds") | !all(file.exists(db_files))) {
init_scheme(scheme_file)
# в ином случае - проверяем кэш
} else {
saved_hash <- readRDS(hash_file)
# если данные были изменены проводим реинициализацию таблицы и схемы
if (!all(exist_hash == saved_hash)) {
cli::cli_inform(c(">" = "Данные схемы были изменены..."))
init_scheme(scheme_file)
} else {
cli::cli_alert_success("изменений нет")
}
}
# перезаписываем файл
if (!dir.exists("temp")) dir.create("temp")
saveRDS(exist_hash, hash_file)
}
init_scheme = function(scheme_file) {
options(box.path = here::here())
box::use(
modules/db,
modules/scheme_generator[scheme_R6]
)
if (!dir.exists("db")) dir.create("db")
cli::cli_h1("Инициализация схемы")
schms <- purrr::map2(
.x = scheme_file,
.y = names(scheme_file),
\(x, y) {
con <- db$make_db_connection(y)
on.exit(db$close_db_connection(con), add = TRUE)
schm <- scheme_R6$new(x)
db$check_if_table_is_exist_and_init_if_not(schm, con)
schm
}
)
# проверка на наличие дублирующихся названий вложенных таблиц
nested_tables_ids <- purrr::map(
names(schms),
\(x) schms[[x]]$nested_tables_names
)
nested_tables_ids <- unlist(nested_tables_ids)
tab <- table(nested_tables_ids)
# если встречается хоть одно значение несколько раз - начать истошно кричать (могут возникнуть пробемы при вызове всплывающих окон в формах)
if (!all(!tab > 1)) {
cli::cli_abort(c("В одной или нескольких схемах наименования вложенных форм совпадают:", paste("-", names(tab)[tab > 1])))
}
saveRDS(schms, "scheme.rds")
}