Repository navigation
Check restore overwrite permission before changing the database - #1449
Conversation
Signed-off-by: Floating-Y <118035379+Floating-Y@users.noreply.github.com>
Signed-off-by: Floating-Y <118035379+Floating-Y@users.noreply.github.com>
Signed-off-by: Floating-Y <118035379+Floating-Y@users.noreply.github.com>
WaylandYang
left a comment
There was a problem hiding this comment.
Thanks, this fixes the ordering properly and covers #1444's case too. Small asks before merge: (1) re-run the preflight right before pg_restore (after the prompt), so a target that changed meanwhile is refused before the database is touched; (2) add --single-transaction alongside --exit-on-error, so a SQL failure rolls the database back; (3) keep the staging dir in the current directory as before (or explain the move to the system temp dir); (4) switch the new code comments to Chinese.
Signed-off-by: Floating-Y <118035379+Floating-Y@users.noreply.github.com>
WaylandYang
left a comment
There was a problem hiding this comment.
Thanks, all four are in: the preflight runs again right before pg_restore and refuses a target that moved, --single-transaction rolls a failed SQL restore back, staging is back in the current directory, and the comments are Chinese. Approving and merging.
Why
utopia restore --forcewas parsed but did not authorize directory replacement, and the non-empty-directory check ran afterpg_restore --clean. The documented database overwrite guard was also missing, so an invalid restore target could be discovered after SQL changes.Closes #1448, which records the dependency rationale. Related: #1444 covers the directory replacement case; this PR also protects database overwrite permission, validates targets before restoration and retains original files on failure.
What changes
--force;--yesonly skips confirmation.--dry-runperforms the same initial checks without restoring either target.pg_restore; reject a changed physical target instead of silently redirecting prepared data.--single-transaction --exit-on-error. SQL restore errors roll back the database transaction;--clean --if-existsis added only with--force.sqlx,tokioandtempfile, plus test-onlyutopia-storefor the required database guard. No package versions change. The ordinary CI test job installs matching PostgreSQL 16 clients.Maintainer review follow-up
Implements the four requests in the review:
99, rather than restoring the archived7. Retain the original data directory.Why the code and tests exceed 500 added lines
Code and tests add 1,162 lines, including 926 lines of integration fixtures and assertions. Database overwrite protection, file replacement and shared preflight must land together to keep the destructive operation consistently guarded. A recording command verifies call order and file retention; actual PostgreSQL restoration verifies transaction rollback. The suite covers the eight requested safety behaviors, confirmation-time changes and concrete key/path regressions. Duplicate missing-key and absent-target scenarios were removed; the short command-failure and file-installation rollback cases cover different failure stages. Confirmation synchronization uses the existing fixture and a bounded wait, without a second test framework.
How it was checked
Revision:
39c5d8ffa3ae4fe773101f7505d016186aafd378, based ondevatc8aedf9.cargo test --locked --workspace --no-fail-fastpassed: 1,760 passed, 0 failed, 6 existing ignored, across 46 test targets. Both required-test guards were enabled after migrating a dedicated disposable cluster. SHA256 checks matched all four revised files to the tested source snapshot.343a6e4: a changed directory still reached the restore command, and a changed database was accepted. The expanded 17-case suite at that point had 7 passes and 10 failures, including wrong staging and the observed7 != 99SQL rollback assertion.cargo fmt --all --check,cargo clippy --locked --workspace --all-targets -- -D warnings, and the committed diff check passed.39c5d8f(backend, test, lint, build, migrations and web).Database and filesystem restoration are not atomic. SQL execution errors roll back the database transaction, but a successful database commit can be followed by a failed directory operation. Preflight does not lock out concurrent writers. Stop writers, retain a pre-restore backup, and follow the documented retry/manual-recovery steps for interruption or later filesystem failures. In-place
--cleanreplaces archived objects rather than removing unrelated database objects.Before review
git commit -s).39c5d8f.utopia_store::test_db::url(), run in the ordinary workspace suite, and are not ignored.dev.