feat: allow user to disable database validation safe_mode (#20)

* feat: allow user to disable database validation safe_mode

* fix safe_mode logic

* fix: fix user confirmation

* feat: allow disabling of auto-validation
This commit is contained in:
Senghan Bright 2021-08-04 09:16:33 +02:00 committed by GitHub
parent 1929fc49df
commit e7c9dee1e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 18 deletions

View File

@ -268,11 +268,12 @@ return telescope.register_extension {
set_config_state('user_workspaces', ext_config.workspaces, {}) set_config_state('user_workspaces', ext_config.workspaces, {})
-- start the database client -- start the database client
db_client.init(ext_config.ignore_patterns) db_client.init(ext_config.ignore_patterns, ext_config.db_safe_mode or true, ext_config.auto_validate or true)
end, end,
exports = { exports = {
frecency = frecency, frecency = frecency,
get_workspace_tags = get_workspace_tags, get_workspace_tags = get_workspace_tags,
validate_db = db_client.validate,
health = checkhealth, health = checkhealth,
}, },
} }

View File

@ -43,7 +43,7 @@ local function file_is_ignored(filepath)
return is_ignored return is_ignored
end end
local function validate_db() local function validate_db(safe_mode)
if not sql_wrapper then return {} end if not sql_wrapper then return {} end
local queries = sql_wrapper.queries local queries = sql_wrapper.queries
@ -56,33 +56,44 @@ local function validate_db()
end end
end end
-- don't allow removal of >N values from DB without confirmation
local confirmed = false local confirmed = false
if #pending_remove > DB_REMOVE_SAFETY_THRESHOLD then if not safe_mode then
if vim.fn.confirm("Telescope-Frecency: remove " .. #pending_remove .. " entries from SQLite3 database?", "&Yes\n&No", 2) then
confirmed = true confirmed = true
elseif #pending_remove > DB_REMOVE_SAFETY_THRESHOLD then
-- don't allow removal of >N values from DB without confirmation
local user_response = vim.fn.confirm("Telescope-Frecency: remove " .. #pending_remove .. " entries from SQLite3 database?", "&Yes\n&No", 2)
if user_response == 1 then
confirmed = true
else
vim.defer_fn(function() print("TelescopeFrecency: validation aborted.") end, 50)
end end
else else
confirmed = true confirmed = true
end end
if confirmed then if #pending_remove > 0 then
for _, entry in pairs(pending_remove) do if confirmed == true then
-- remove entries from file and timestamp tables for _, entry in pairs(pending_remove) do
sql_wrapper:do_transaction(queries.file_delete_entry , {where = {id = entry.id }}) -- remove entries from file and timestamp tables
sql_wrapper:do_transaction(queries.timestamp_delete_entry, {where = {file_id = entry.id}}) sql_wrapper:do_transaction(queries.file_delete_entry , {where = {id = entry.id }})
sql_wrapper:do_transaction(queries.timestamp_delete_entry, {where = {file_id = entry.id}})
end
print(('Telescope-Frecency: removed %d missing entries.'):format(#pending_remove))
else
print("Telescope-Frecency: validation aborted.")
end end
else
print("TelescopeFrecency: validation aborted.")
end end
end end
local function init(config_ignore_patterns) local function init(config_ignore_patterns, safe_mode, auto_validate)
if sql_wrapper then return end if sql_wrapper then return end
sql_wrapper = sqlwrap:new() sql_wrapper = sqlwrap:new()
local first_run = sql_wrapper:bootstrap() local first_run = sql_wrapper:bootstrap()
ignore_patterns = config_ignore_patterns or default_ignore_patterns ignore_patterns = config_ignore_patterns or default_ignore_patterns
validate_db()
if auto_validate then
validate_db(safe_mode)
end
if first_run then if first_run then
-- TODO: this needs to be scheduled for after shada load -- TODO: this needs to be scheduled for after shada load
@ -210,4 +221,5 @@ return {
init = init, init = init,
get_file_scores = get_file_scores, get_file_scores = get_file_scores,
autocmd_handler = autocmd_handler, autocmd_handler = autocmd_handler,
validate = validate_db,
} }

View File

@ -30,7 +30,4 @@ function! frecency#FrecencyComplete(findstart, base)
end end
endfunction endfunction
command FrecencyValidate lua require'telescope'.extensions.frecency.validate_db()
" lua require'telescope'.extensions.frecency.completefunc(action)
" lua require'telescope'.extensions.frecency.completefunc(res)
" require'telescope._extensions.frecency.db_client'.autocmd_handler(vim.fn.expand('<amatch>'))