Skip to content

Make history --clear more robust - #11049

Closed
Hofer-Julian wants to merge 1 commit into
nushell:mainfrom
Hofer-Julian:history-clear
Closed

Hofer-Julian wants to merge 1 commit into
nushell:mainfrom
Hofer-Julian:history-clear

Conversation

@Hofer-Julian

Copy link
Copy Markdown
Contributor
  • Remove auxiliary files of sqlite
  • Give proper error messages if there are problems

Might help with #7991

- Remove auxiliary files of sqlite
- Give proper error messages if there are problems
@Hofer-Julian

Copy link
Copy Markdown
Contributor Author

Ping :)

@amtoine

amtoine commented Nov 20, 2023

Copy link
Copy Markdown
Member

might supersede #9575

@amtoine amtoine mentioned this pull request Nov 20, 2023
@Hofer-Julian

Copy link
Copy Markdown
Contributor Author

@amtoine can this be merged?
This is a clear improvement as far as I can see, and even removes a "TODO" comment.

@amtoine

amtoine commented Nov 26, 2023

Copy link
Copy Markdown
Member

ooh yeah, i can totally do that 😌

@amtoine amtoine left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment thread crates/nu-cli/src/commands/history.rs Resolved
@amtoine

amtoine commented Nov 26, 2023

Copy link
Copy Markdown
Member

@Hofer-Julian
have you tried this on Windows?

@Hofer-Julian

Copy link
Copy Markdown
Contributor Author

@Hofer-Julian have you tried this on Windows?

I did not.
Is there anything in the code that makes you expect OS specific behaviour?

@amtoine

amtoine commented Nov 27, 2023

Copy link
Copy Markdown
Member

I did not. Is there anything in the code that makes you expect OS specific behaviour?

yeah... Windows paths 😱 💀 😆

@amtoine
amtoine requested a review from fdncred November 27, 2023 17:31
@fdncred

fdncred commented Nov 27, 2023

Copy link
Copy Markdown
Contributor

Is there anything in the code that makes you expect OS specific behaviour?

Nothing in the code but you have to consider that Windows is different than *nix. Windows does not allow you to mess with things that are in use. You get what are called sharing violations. This is one of the reasons why I originally said that we need to use sql to delete from history and sqlite vacuum instead of trying to remove the files.
image
and these files still exist
image

@fdncred
fdncred marked this pull request as draft November 27, 2023 18:05

@fdncred fdncred left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't work in Windows so I'm making it a draft.

@fdncred

fdncred commented Nov 27, 2023

Copy link
Copy Markdown
Contributor

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.

@Hofer-Julian

Copy link
Copy Markdown
Contributor Author

Doesn't work in Windows so I'm making it a draft.

Could you please add the error you are getting. Otherwise this is a bit hard to debug.

@fdncred

fdncred commented Nov 27, 2023

Copy link
Copy Markdown
Contributor

Doesn't work in Windows so I'm making it a draft.

Could you please add the error you are getting. Otherwise this is a bit hard to debug.

It's in the screenshot above.

@Hofer-Julian

Copy link
Copy Markdown
Contributor Author

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?

@fdncred

fdncred commented Nov 27, 2023

Copy link
Copy Markdown
Contributor

@fdncred I see you started working on this, much appreciated #11170. Feel free to reach out if you can need help/testing.

#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 stor commands, I'd appreciate the feedback and/or changes.

@AucaCoyan

Copy link
Copy Markdown
Contributor

Hello!
I tried the branch from the repo of Hofer and it worked on my windows machine. It removed all the history but 1 command: history --clear.
I cannot reproduce the bug 🤔

@Hofer-Julian

Copy link
Copy Markdown
Contributor Author

@fdncred I see you started working on this, much appreciated #11170. Feel free to reach out if you can need help/testing.

#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 stor commands, I'd appreciate the feedback and/or changes.

I am confused. "stor reset" sounds exactly like the behavior you are suggesting for "history --clear".

@Hofer-Julian

Copy link
Copy Markdown
Contributor Author

Hello! I tried the branch from the repo of Hofer and it worked on my windows machine. It removed all the history but 1 command: history --clear. I cannot reproduce the bug 🤔

Did you use the sqlite or file backend?

@fdncred

fdncred commented Nov 28, 2023

Copy link
Copy Markdown
Contributor

I am confused. "stor reset" sounds exactly like the behavior you are suggesting for "history --clear".

The stor family of commands are specifically for a sqlite in-memory database that I'm trying to get support for and landed. Those commands do not know anything about history or any sqlite files on the file system.

@Hofer-Julian

Copy link
Copy Markdown
Contributor Author

I am confused. "stor reset" sounds exactly like the behavior you are suggesting for "history --clear".

The stor family of commands are specifically for a sqlite in-memory database that I'm trying to get support for and landed. Those commands do not know anything about history or any sqlite files on the file system.

Understood.

I will still close this PR. There is not much to salvage from the current approach.

@Hofer-Julian
Hofer-Julian deleted the history-clear branch November 28, 2023 13:17
@amtoine

amtoine commented Nov 28, 2023

Copy link
Copy Markdown
Member

i'm not sure i understand why this was closed 😕

@amtoine amtoine added the A:history Related to the history label Nov 28, 2023
@Hofer-Julian

Copy link
Copy Markdown
Contributor Author

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. :)

@amtoine

amtoine commented Nov 28, 2023

Copy link
Copy Markdown
Member

ok, that's fair!

but history --clear already removes one file: history.sqlite3.
the issue appeared to me to be that history.sqlite3-shm and history.sqlite3-wal were left over, but you're saying they should all three stay there and be emptied?

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"))

@fdncred

fdncred commented Nov 28, 2023

Copy link
Copy Markdown
Contributor

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
A write-ahead log or WAL file is used in place of a rollback journal when SQLite is operating in WAL mode. As with the rollback journal, the purpose of the WAL file is to implement atomic commit and rollback. The WAL file is always located in the same directory as the database file and has the same name as the database file except with the 4 characters "-wal" appended. The WAL file is created when the first connection to the database is opened and is normally removed when the last connection to the database closes. However, if the last connection does not shutdown cleanly, the WAL file will remain in the filesystem and will be automatically cleaned up the next time the database is opened.

According to Stackoverflow: https://stackoverflow.com/questions/12928294/what-content-do-shm-and-wal-files-contain-after-running-checkpoint
The -shm file does not contain any permanent data.

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.

fdncred pushed a commit that referenced this pull request Oct 9, 2026
## 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:history Related to the history

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants