Repository navigation
Make history --clear more robust - #11049
Hofer-Julian wants to merge 1 commit into
Conversation
- Remove auxiliary files of sqlite - Give proper error messages if there are problems
|
Ping :) |
|
might supersede #9575 |
|
@amtoine can this be merged? |
|
ooh yeah, i can totally do that 😌 |
amtoine
left a comment
There was a problem hiding this comment.
that's pretty nice 😏
> rm ~/.config/nushell/history.sqlite3-*
> history --clear
Error: nu::shell::io_error
× I/O error
help: Failed to remove file "/home/amtoine/.config/nushell/history.sqlite3-wal": No such file or directory (os error 2)
Failed to remove file "/home/amtoine/.config/nushell/history.sqlite3-shm": No such file or directory (os error 2)|
@Hofer-Julian |
I did not. |
yeah... Windows paths 😱 💀 😆 |
fdncred
left a comment
There was a problem hiding this comment.
Doesn't work in Windows so I'm making it a draft.
|
To add a little more detail, @rgwood tried this before. The PR is buried here some where I'm sure. As I recall, we have to leave 1 record in the database because if you delete everything, nushell tries to get and entry from the db and pukes. So, I'd start with leaving 1 entry. I also think a vacuum is needed. Read more about it here https://www.sqlite.org/lang_vacuum.html. |
Could you please add the error you are getting. Otherwise this is a bit hard to debug. |
It's in the screenshot above. |
|
Hah, I've actually worked on dropping sqlite tables instead of deleting the file on Windows just a few months ago. I completely forgot 😅 @fdncred I see you started working on this, much appreciated #11170. Feel free to reach out if you can need help/testing. I think we can close this PR then, agreed? |
#11170 is unrelated to this PR, so I think we still need something to do this. However, if you want to play around with the |
|
Hello! |
I am confused. "stor reset" sounds exactly like the behavior you are suggesting for "history --clear". |
Did you use the sqlite or file backend? |
The |
Understood. I will still close this PR. There is not much to salvage from the current approach. |
|
i'm not sure i understand why this was closed 😕 |
|
This approach deleted the sqlite files instead of modifying the existing database. Opening a new PR is just as much work as adapting this one. :) |
|
ok, that's fair! but there is still the issue that, if you remove the history file yourself, Nushell will crash next time, which imo should still be fixed and was fixed by this very PR 😮 > rm ~/.config/nushell/history.sqlite3
> nu
Error: × error within history database: SqliteFailure(Error { code: SystemIoFailure, extended_code: 522 }, Some("disk I/O error")) |
|
the shm and wal files are related to allowing sqlite to work concurrently with multiple sessions which is called WAL mode or Write-Ahead-Logging. You can read more about it here https://www.sqlite.org/walformat.html. Once I quit out of using nushell the shm and wal files are flushed into the sqlite history db. It does this on Windows. I haven't checked any other OS. There could be data loss if these files are removed incorrectly. From sqlite's website: https://www.sqlite.org/tempfiles.html According to Stackoverflow: https://stackoverflow.com/questions/12928294/what-content-do-shm-and-wal-files-contain-after-running-checkpoint When the last connection is closed, the database is automatically checkpointed, and the -wal file is then deleted. This implies that after a checkpoint, and if no other connections exist, the -wal file does not contain any important data. If possible, you should close the connection before taking the backup. |
## Description `history --clear` deleted the sqlite history file with `std::fs::remove_file`, leaving a TODO about the `-wal`/`-shm` files. That goes wrong in several ways: - the running REPL still holds a connection to the deleted file, so every later command in that session is written to an unlinked file and lost; - the leftover `-wal`/`-shm` files don't match the new database, so the next session can fail with `disk I/O error` (as reported in the issue) or `no such table: history`; - on Windows the file is in use and can't be deleted at all (the reason #11049 was shelved). As suggested in #11049, this clears the sqlite history in place through reedline's `SqliteBackedHistory::clear`, which runs `delete from history` followed by `VACUUM`. The table stays, so the REPL's open connection keeps working and new sessions open the database normally. Plaintext history is unchanged. Tests added in `crates/nu-cli/tests/commands/history_output.rs`: - `sqlite_history_clear_removes_all_entries`: import records, `history --clear`, `history | length` is 0; - `sqlite_history_clear_keeps_database_usable`: a connection opened before the clear (standing in for the REPL) and a fresh `SqliteBackedHistory::with_file` can both add entries afterwards, and `history` shows both. Without this fix, the entry written through the pre-existing connection is lost. ## User-facing changes (Release notes) Fixed `history --clear` with sqlite history: it now empties the history database in place instead of deleting the file, so the current session keeps recording history and new sessions no longer fail to start with a "disk I/O error". It also works on Windows while Nushell is running. ## Additional notes Fixes #14244 Supersedes #11049


Might help with #7991