refactor: move legacy-logs migration into local storage adapter#53
Merged
Conversation
The one-time legacy-logs migration ran as a top-level side effect in config.js at require time. Because 9 modules import config.js, it fired far beyond startup; and it ran regardless of STORAGE_BACKEND, so an S3/R2 setup would create a local logs dir and move legacy logs into a directory the server never reads. Fold the migration into the local storage adapter's init() (server/storage/ local.js), where the logs dir is already created. Only the local adapter has this logic, so non-local backends are excluded by construction — no backend flag to forget. The migration is gated to a freshly-created logs dir via an existed-before-mkdir snapshot (mirrors the original guard) and is best-effort: a failed rename is caught and logged, never crashing startup. config.js loses its fs/path imports and LEGACY_LOGS_DIR and is now side-effect-free at import. The previously-untested migration gains 6 focused tests (require purity, fresh migrate, no-sentinel skip, existing-dir no-clobber, no-legacyDir/S3 path, best-effort error handling). Closes #51 (the require-time migration item; the ratelimit-log dir-ensure nit is intentionally left as-is, documented in the issue discussion).
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.
Addresses the require-time migration hygiene item raised during the #50/#51/#52 CCXRAY_HOME work (Codex review of the merged state flagged it as should-fix).
Problem
server/config.jsran the one-time legacy-logs migration as a top-level side effect at require time:require('./config'), so the mkdir + file-move ran far beyond startup.STORAGE_BACKEND=s3, it created a local logs dir and moved legacy logs into a directory the server never reads.Approach (chosen from a 5-way spike + Codex comparison: B > C > A > D > E)
Fold the migration into the local storage adapter's
init()(server/storage/local.js), where the logs dir is already created:storage/index.jspasseslegacyDirsolely on the local branch, so S3/R2 can never run it (no flag to forget).init()snapshots whether the logs dir existed before mkdir, so migration only ever runs into a freshly-created dir (mirrors the originalif (!existsSync(LOGS_DIR)), never clobbers populated logs).startServer()/init()(matches the original catch-and-log).fs,path, andLEGACY_LOGS_DIR; zero filesystem side effects at import.index.jsalreadyawait config.storage.init().Why not the alternatives
existsSync(logsDir)guard that is only valid because they run beforestorage.init()— fragile ordering. B runs insideinit()via the existed-snapshot, so the guard is robust.Tests
Previously untested. Adds 6 focused tests (
test/legacy-migration.test.js): require-time purity, fresh migrate, no-sentinel skip, existing-dir no-clobber, no-legacyDir (S3 path), and best-effort error handling.Verification
node -con all changed files.Out of scope
ratelimit-log.js:78dir-ensure nit — intentionally left as-is (it's an intentional fire-and-forget design; documented in the #51 discussion).