-
Notifications
You must be signed in to change notification settings - Fork 210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
utils of history with sqlite #779
Conversation
fj0r
commented
Mar 8, 2024
- backup
- restore
- timing
- top
- backup - restore - timing - top
export def 'history restore' [name: string@"nu-complete history_backup_file"] { | ||
rm -f $nu.history-path | ||
cat ([$env.history_backup_dir, $"($name).sql"] | path join) | ||
| sqlite3 $nu.history-path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be good to check for sqlite3 before using it. maybe with the which command.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's nothing special about this command. Updates can be done with query db
, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that when the database file does not exist, an error will occur when using query db
, and using sqlite3
will create a new database. I am not sure. I need to test it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
open test.sqlite | query db 'create table test(a text);'
Error: nu::shell::file_not_found
× File not found
╭─[entry #1:1:6]
1 │ open test.sqlite | query db 'create table test(a text);'
· ─────┬─────
· ╰── file not found
╰────
help: test.sqlite does not exist
'create table test(a text);' | sqlite3 test.sqlite
# After using sqlite3 to create the database, you will find that the table already exists using `query db`
open test.sqlite | query db 'create table test(a text);'
Error: × Failed to query SQLite database
╭─[entry #1:1:29]
1 │ open test.sqlite | query db 'create table test(a text);'
· ──────────────┬─────────────
· ╰── table test already exists in create table test(a text); at offset 13
╰────
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how to create an empty database file using query db
, ever used this function before
export def empty-sqlite [] {
# sqlite3 empty.db "VACUUM;"; cat empty.db | gzip | encode base64
'H4sIAAAAAAAAAwsO9MksSVVIyy/KTSxRMGYQYGBkZHBQUGBgYGCEYhhAZhMLGBn0ihbwglgCZOgeBaNgFIyCUTAKRsEoGAWjYBSMglEwCkYBVQAANHgbMAAQAAA='
| decode base64 --binary | gzip -d
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm very sorry, I saw that the reply had a Pending
tag, and I found that I need to submit it. I don't know this before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been wondering if the stor
family of commands can be used to replace the sqlite3 stuff here. For backup and restore, it seems like all you'd have to do is stor import --file-name $nu.history-path
and then stor export --file-name /some/other/path/history.sqlite3
. Have you tried that yet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems feasible, but the database file is larger than the sql file, it is acceptable.
The main question is, if other applications are also using stor and create tables, will stor import
delete the existing tables, and if there are others tables when saving, will they be saved together?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it is not limited to text format, can it just copy the database file?
Text formats can be parsed and processed, and there may be some scenarios where this is required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be a small problem with stor
stor create -t foo -c { bar: str }
stor insert -t foo -d {bar: "foobar"}
stor insert -t foo -d {bar: "foo'bar"}
Error: × Failed to open SQLite connection in memory from insert
╭─[source:1:1]
1 │ nu
· ▲
· ╰── near "bar": syntax error in INSERT INTO foo ( bar) VALUES ( 'foo'bar') at offset 37
╰────
stor insert -t foo -d {bar: "foo\'bar"}
Error: × Failed to open SQLite connection in memory from insert
╭─[source:1:1]
1 │ nu
· ▲
· ╰── near "bar": syntax error in INSERT INTO foo ( bar) VALUES ( 'foo'bar') at offset 37
╰────
stor insert -t foo -d {bar: "foo\\'bar"}
Error: × Failed to open SQLite connection in memory from insert
╭─[source:1:1]
1 │ nu
· ▲
· ╰── near "bar": syntax error in INSERT INTO foo ( bar) VALUES ( 'foo\'bar') at offset 38
╰────
I think this is an interesting set of custom commands. However, it would be nice if they were cross platform. In order to do that, we'd need to use nushell built-in command in place of externals. It would also be good to eliminate the use of the sqlite3 command if possible. |
d4f121e
to
7d328f6
Compare
I'm sorry once again. |
@maxim-uvarov https://github.com/fj0r/nushell/blob/main/scripts/history-utils/backup.nu
|
https://github.com/fj0r/nushell/blob/rustic/scripts/rustic/mod.nu |
@fj0r thank you! Just as a remark - I once tried updating history with If I understand it correctly, now I should use Still, altering history is on my list, and I will need to learn how to deal with backups. |
@maxim-uvarov sqlite may damage the database during concurrent writing. Unfortunately, in the nushell environment, the timing of writing to history's sqlite is uncontrollable. For example, if you execute an update statement through UPDATE: after testing, it was found that nushell writes history records first and then executes commands. If there is only one nushell, there is no problem. However, consider this situation. You update all history records in one nushell process. Assuming it takes 10 seconds. Before it is finished, you execute any command in another nushell process. This also requires modifying the database file, which may cause problems. From the user's perspective, the history of nushell itself is only a conventional history, and some modern environment experiences are not taken into account, such as the search and synchronization of mixed history of multiple machines. If you have higher requirements in this regard, consider https://github.com/atuinsh/atuin |
We run the sqlite history in WAL mode so there shouldn't be any synchronization issues. |