-
Notifications
You must be signed in to change notification settings - Fork 6
feat: require explicit registration of absolute database paths #50
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
The dynamic-path setup reads as more boilerplate than the old
add_migrations("main.db", …)one-liner, and some of that looks avoidable without giving up the closure's flexibility (arbitrary, app-derived absolute paths).Two things stand out in the example:
We register the same database twice —
allow_path(&db)andadd_migrations(...)— and have to keep the keys in sync by hand. That's the same coupling behind the byte-identical-key concern: if the two spellings ever drift, migrations silently attach to a key load() never matches.The two calls take different types for the same value: allow_path takes
&db(a path) while add_migrations takesdb.to_string_lossy().into_owned()(a String).Two ways to collapse this to a singlecall, both keeping &Path and canonicalizing to the same key internally:
Option A —
add_migrationsauto-allowlists. Have it take implAsRef<Path>and register the path on the allowlist itself. You can't migrate a database you're not allowed to open, so allow-listing a migration target is implied:allow_path(&db)stays for databases without migrations. Downside: add_migrations now has an allowlist side effect, which is slightly less obvious from its name.Option B — chained handle. Keep
allow_pathas the single allowlist entry point and have it return a per-path handle you attach migrations to:This keeps
add_migrations/allow_pathresponsibilities cleanly separated (allow-listing stays the one explicit gate) while still guaranteeing the migration key is the allowed path.allow_path(&db)alone still works for the no-migrations case.Either removes the stringified key and the hand-synced double registration. What are your thoughts @onehumandev @jjhafer?
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.
Option A makes more sense to me; when we register the path, we register the migrations with it as well.
I'll make that change.
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 we go with Option A, we'll still need to retain the
allow_path(..)API for databases that don't require migrations. I'm still somewhat hesitant about this approach, though, since it introduces implicit behavior into theadd_migrations(..)method.That said, the builder pattern in Option B is appealing because it keeps the API surface unchanged while making it more composable. Perhaps @jjhafer may have some thoughts before we commit to either 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.
Sorry, I realized I didn't actually read option B :) That is a better option than A; I'll await @jjhafer's thoughts.
And one other question for my own edification, as it relates to this; is there a particular reason we chose to pass down and cache the raw strings as the path key instead of using
PathBuf?It's not an issue either way, but given that we are keying against a string that we are requiring to be a path, I was curious why we didn't use
PathBuf, even if just for semantic reasons.Uh oh!
There was an error while loading. Please reload this page.
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.
Good question — it was deliberate for a couple of specific reasons:
PathBuf; theStringonly reappears after validation, as an opaque cache key.PathBufis the wrong type for those.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.
After looking at this more, I think there's a simpler way to do this, which is a single
register_databasemethod that takes the db path and an optional Migrator as a parameter. That solves the possible duplication issue, and allows creating a DB without a migrator.I've updated this MR with this change; let me know if this is acceptable, or if another solution would be preferred. Thanks!
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.
Thanks @onehumandev. I like it; nice! Two small follow-ups:
impl AsRef<Path>(dropping theto_string_lossy().into_owned()boilerplate)?Builder(&str) andSetupRegistrar(impl Into<String>) share the same signature?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 like it with the above follow ups that @velocitysystems mentioned.
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.
After discussion with @velocitysystems , there is a larger simplification that can be made here. To avoid having a polluted MR, I'll be closing down this one and opening a new one with the changed approach, as the changes will be significantly different from what this MR currently has.
Thanks!