Repository navigation
feat(core): read and write .sql files from anywhere on disk - #808
Merged
Merged
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4ab1f08398
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
This was referenced Dec 4, 2026
DokaDev
force-pushed
the
files-048/3-file-core
branch
from
December 6, 2026 05:08
e0fc73d to
cccbc79
Compare
DokaDev
force-pushed
the
files-048/3-file-core
branch
2 times, most recently
from
December 6, 2026 05:39
29d82a9 to
88ca890
Compare
A new module, textfile, reads a text file whole and never lossily: UTF-8 with or without a byte order mark, or UTF-16 (LE or BE) behind its byte order mark, decoded strictly; a UTF-32 byte order mark, invalid UTF-16 and text that is not UTF-8 are refused. The byte order mark is taken off for editing and written back; all-CRLF text is edited as LF and written back as CRLF; mixed line endings and lone CRs are kept byte for byte. A file from disk needs a UTF-8 path and a regular file (a link followed) of at most 20 MiB. A version of a file is its stamp (mtime, length) and an FNV-1a hash of its bytes, so a change of the same length within the same second is seen. A write checks the version last seen first (conflict, missing, exists for a save as unless it replaces), refuses a file without write permission and a folder that is not there or cannot be written, never changes permissions, and goes through atomic_write (through a link to its target, keeping the Unix mode). Errors carry the path and the problem, never the text. Path helpers expand ~, ./ and ../ against the home and launch folders, tell a path argument from a saved query's name, show ~ for home and tell files apart by device and inode (the final path on Windows). The unsaved text of file tabs goes to <state>/files/<id>.sql, a folder private to the user (the private-folder rule moves from the result spill to fsutil) of 0600 files. workspace.toml gets the tab kind file within version 3: its absolute path, the version of its file and whether unsaved text is kept; an older datarig keeps it verbatim. The app opens no file tabs yet: a restored one is left out. Related issue: #311
Saved queries now read and write through textfile, with no size cap: a byte order mark is taken off for editing, so it is never sent to the server again (the PostgreSQL and MySQL lexers did not skip it), and is written back; a CRLF file is edited as lines and written back with CRLF; a UTF-16 file with a byte order mark opens instead of being refused. Autosave, save as and overwrite write each file back in the form it was read in, and a save checks the hash of the file too, so a change of the same length within the same second is a conflict. Related issue: #311
Clippy (manual_is_multiple_of) asks for the method over the remainder. Related issue: #311
A workspace.toml with a file tab lost it at the first save: this build opens no file tabs and left the tab out, where an older build keeps it as a tab of a kind it does not know. The tab is now kept as its text (workspace::tab_text, the form unknown tabs are kept in) and written back as it was, with the keys this build does not read. Its unsaved text stays in the state directory. Related issue: #311
A save as that must not replace a file checked the path, then renamed the new file over it, so a file made there in between was replaced. It now goes through fsutil::create_new, which puts the file in place only where nothing is, and answers Exists otherwise. Related issue: #311
Reading and writing text files from disk (strict UTF-8 and UTF-16 decoding, the line endings, the file hash) brought the CI's Linux x86_64 binary from 32.991 to 33.013 MiB before the 0.47 fixes, within the 34 MiB budget that 0.47.3 set. Related issue: #311
Windows keeps a time in steps of 100 ns, so a stamp of 1 ns precision came back rounded there. A file's own time is always in such steps; the test now uses them too. Related issue: #311
Autosave, Ctrl+S and an overwrite now leave a saved query's file alone when it has no write permission, as a file tab from disk does. The tab shows that the text is not saved and why, Space s a saves it as another query, and the file's permissions are never changed. Once the file may be written again, the next autosave writes it. Related issue: #812
DokaDev
force-pushed
the
files-048/3-file-core
branch
from
December 6, 2026 08:37
88ca890 to
fedc98b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related issue: #311
Related issue: #812
Third part of the 0.48.0 "files" stack, on top of #797 (
files-048/2-new-tab). This is the core of part B of #311 (.sql files from disk): reading and writing text files, the unsaved-text store and the workspace'sfiletab. No UI here: the next part (files-048/4-open-save) opens and saves file tabs with it. The only change users see is in saved queries, which now read and write through the same code.What changes
datarig-core::textfile(new):read(path, rules): follows a link, takes a regular file only (looked at before it is opened, so a pipe never blocks), decodes strictly and never lossily: UTF-8 with or without a byte order mark, UTF-16 LE or BE behind its byte order mark (an unpaired surrogate or an odd length is refused), a UTF-32 byte order mark refused, anything else that is not UTF-8 refused. The byte order mark is taken off and remembered. Line endings: all LF (or none) as is; all CRLF edited as LF and remembered; mixed or a lone CR kept byte for byte.Rules::DISKwants a UTF-8 path and at mostMAX_LEN(20 MiB; exactly at the cap opens, one byte over is refused with both sizes);Rules::SAVED_QUERYhas neither rule.TextFile { text, format, seen }:Formatis the encoding and line ending;Seenis theStamp(mtime and length, moved here fromscripts) and an FNV-1a 64 hash of the bytes (thefnvcrate, already in the tree; pinned to the published test values, as the hash is kept inworkspace.toml).write(path, text, format, expect): encodes back exactly (line endings, byte order mark, UTF-16), checksExpect::Seenfirst (Conflictwhen the stamp or the hash differs, so a change of the same size within the same second is seen;Missingwhen the file or a link's target is gone),Expect::New { replace }answersExistsfor another file there unless the caller replaces (without replace the file is put in place byfsutil::create_new, so a file made there meanwhile is never replaced either),Expect::Anywrites regardless. A file without write permission (ReadOnly), a folder that cannot be written (FolderReadOnly) and a missing folder (NoFolder) are refused before anything is written, and permissions are never changed. The write goes throughfsutil::atomic_write: through a link to its target (the link stays) and keeping the Unix mode.check(path, seen):Same,ChangedorGone, for the check on tab enter and at restore.FileError) carry the path and the problem (an io kind for the rest), never the text.textfile::path:expand(~,./,../, absolute, against the home folder and the launch folder;.and..by name),is_path(/,~,./,../; on Windows alsoC:\,C:/,\\,.\,..\),display(~for the home folder),FileId(device and inode on Unix, the final path on Windows) andhome(HOME, else USERPROFILE, through the app's env lookup;App::homeuses it).Doc.format), and a save checks the hash too (Doc.seen). Saved queries keep their write rules otherwise.workspace::{write,read,remove}_unsaved,<state>/files/<id>.sql, a 0700 folder (made private byfsutil::create_private_dir, moved here from the result spill, which uses it as before) of 0600 files (atomic_write_private).workspace.toml: the tab kindfilewithin version 3, withpath(absolute),mtime,len,hashandunsaved;tab_keyfinds a file tab by its path. A file tab without an absolute path is left out; of two with one id, the later one gets an id of its own and no unsaved text (they would share it). An older datarig keeps the kind verbatim. The TUI does not open file tabs yet: a restored one is kept as its text (workspace::tab_text, the form unknown tabs are kept in) and written back as it was, with the keys this build does not read.Ctrl+Sand an overwrite after a conflict getSaveError::ReadOnly(checked bytextfile::read_only, after the version check, so a change on disk is still a conflict first); the tab shows*and "Not saved: the file is read-only · Space s a saves a copy" (the save-as key from the key map), an asked save says so every time, and the permissions are never changed. Once the file may be written again, the next autosave writes it. Docs: the editor guide;changes/fixed/read-only-saved-query.md.crates/bench/budgets.toml: a note of what the text file core adds to the binary: 32.991 -> 33.013 MiB on the CI's Linux x86_64, measured ate0fc73d2(with Do not save over a saved query whose file was made read-only #812) before the rebase onto 0.47.3. 0.47.3 had already raised the budget to 34 MiB, so this PR no longer changes it. On the new base, CI's performance job measures 33.057 MiB at88ca8906, against the 34 MiB budget.docs/architecture.md(textfile, the saved queries' form and version, the unsaved store and thefilekind) and the editor guide (encodings and line endings of saved queries).changes/fixed/saved-query-encoding.md.Failing first
On the stack base
c356e925(files-048/2-new-tab), the saved-query flow tests (cargo test -p datarig-tui --test it -- a_saved_query_with_a_byte_order_mark a_crlf_saved_query): 2 failed:From the review bot's first finding (fixed in
68c37744), on1c71d47c,a_file_tab_this_build_does_not_open_stays_in_the_workspace: the tab is gone fromworkspace.tomlafter a launch and a quit:The bot's second finding (a file made at a save-as path between the check and the rename was replaced) is fixed in
e9bc3ce4by publishing throughfsutil::create_new; that race is not reproducible in a test, andcreate_newis covered by its own tests. CI's clippy asked foris_multiple_of(1c71d47c), its performance job for the larger binary budget (5074f05a), and its Windows tests for times in 100 ns steps in a workspace test (c4c30718; Windows keeps file times so).For #812 (
e0fc73d2), onc4c30718,cargo test -p datarig-tui --test it -- a_saved_query_made_read_only_outside_is_never_saved_over: 1 failed, the autosave wrote over the 0444 file:The core tests are new with the module they test (
textfile, and thefilekind and unsaved store ofworkspace): on the base they do not build (textfiledoes not exist;TabKind::File,FileTab,write_unsavedare not there), and every one passes here.Tests
textfile(core):round_trips_are_byte_exact(LF, CRLF, mixed, a lone CR, UTF-8 BOM with LF and CRLF, UTF-16 LE with a surrogate pair and CRLF, UTF-16 BE without a final newline, a UTF-16 BOM alone, an empty file, no final newline; pure and on disk),an_edit_is_written_in_the_files_own_form,the_hash_is_fnv_1a_64_bits(the published test values),a_stamp_is_kept_as_nanoseconds_and_a_length(also before the epoch),text_that_cannot_be_read_exactly_is_refused(Windows-1252, a BOM then Latin-1, an unpaired high and low surrogate, an odd length, UTF-32 LE and BE, a folder, a missing file),files_from_disk_are_capped_and_saved_queries_are_not(exactly 20 MiB opens, one byte over is refused with both sizes, uncapped for saved queries),a_path_that_is_not_utf8_is_refused(Unix),a_change_on_disk_is_a_conflict_and_a_file_gone_is_missing(the same length with the mtime set back byFile::set_modified, another length, gone, written again only when asked),save_as_onto_another_file_answers_exists_unless_told_to_replace(also a folder, and a missing folder is never made),a_write_goes_through_a_link_which_stays_a_link(Unix; a dangling link is missing, and something there for a save as),a_read_only_file_and_a_folder_that_cannot_be_written_are_refused(Unix: 0444 file under every expectation, mode untouched; 0555 folder gets no new or replaced file; a 0640 file keeps its mode),errors_never_hold_the_text(a marker in the file never in an error's text, debug or io form).textfile::path(core):the_home_folder_is_home_else_userprofile,an_argument_is_a_path_by_how_it_starts(both rule sets on every system),a_typed_path_is_expanded_from_home_and_the_launch_folder(Unix),a_typed_path_is_expanded_by_the_rules_of_windows(Windows),a_path_under_the_home_folder_is_shown_with_a_tilde(Unix),a_file_is_told_by_the_file_not_by_its_path(another spelling, a link, another case where the file system ignores it).workspace(core):a_file_tab_comes_back_with_its_file(with a key a later version adds),a_file_tab_that_cannot_be_read_back_is_left_out_or_loses_only_what_cannot_be,an_older_datarig_keeps_a_file_tab_as_it_was(the tab as an older reader keeps it, written back and read again),unsaved_text_is_kept_private_and_removed_when_done(0700 folder, 0600 file on Unix, an opened-up folder made private again, ids that are not file names refused).scripts(core):a_saved_query_is_edited_without_its_byte_order_mark_and_written_back_as_it_was(BOM with CRLF, CRLF, a lone CR, UTF-16; save as keeps the form), andsaves_check_the_stamp_and_never_overwrite_a_changed_filenow also checks a change of the same length with the mtime set back.a_saved_query_made_read_only_outside_is_never_saved_over(flow: autosave andCtrl+Stwice leave the bytes and the 0444 mode;*and the words; writable again, the next autosave writes) anda_saved_query_without_write_permission_is_never_saved_over(core: autosave and overwrite refused, a change on disk is a conflict first, the mode kept).a_file_tab_this_build_does_not_open_stays_in_the_workspace(review).workspacealso:a_tab_kept_as_its_text_is_written_back_as_it_was(review).TabStateliterals of the bench and the PostgreSQL and MySQL integration tests (file: None),crash_safety(the save's format), the spill test's import.Not in this part
files-048/4-open-save.