-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
Improve edit api #91
Improve edit api #91
Conversation
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 for tackling this!
I would like to explore other options before we consider introducing take_mut
(or similar crates) as a dependency though (reasoning in inline comments).
I think we can do something like this: diff --git a/rusqlite_migration/src/builder.rs b/rusqlite_migration/src/builder.rs
index 8ecfcf6..28ccef0 100644
--- a/rusqlite_migration/src/builder.rs
+++ b/rusqlite_migration/src/builder.rs
@@ -1,4 +1,4 @@
-use std::iter::FromIterator;
+use std::{iter::FromIterator, mem};
use include_dir::Dir;
@@ -10,6 +10,9 @@ pub struct MigrationsBuilder<'u> {
migrations: Vec<M<'u>>,
}
+// Placeholder migration used to replace and edit
+const PLACEHOLDER_M: M<'static> = M::up("");
+
impl<'u> MigrationsBuilder<'u> {
/// Creates a set of migrations from a given directory by scanning subdirectories with a specified name pattern.
/// The migrations are loaded and stored in the binary.
@@ -42,14 +45,19 @@ pub fn from_directory(dir: &'static Dir<'static>) -> Result<Self> {
///
/// Panics if no migration with the `id` provided exists.
#[must_use]
- pub fn edit(mut self, id: usize, f: impl Fn(&mut M)) -> Self {
+ pub fn edit(mut self, id: usize, f: impl Fn(M)) -> Self {
if id < 1 {
panic!("id cannot be equal to 0");
}
- f(self
- .migrations
- .get_mut(id - 1)
- .expect("No migration with the given index"));
+
+ let migration_to_edit = mem::replace(
+ self.migrations
+ .get_mut(id - 1)
+ .expect("No migration with the given index"),
+ PLACEHOLDER_M.clone(),
+ );
+
+ f(migration_to_edit);
self
} What do you think @czocher ? |
65a5b26
to
3cfd84b
Compare
We still need to |
3cfd84b
to
fe427fc
Compare
I see two issued with implementing
If we want to avoid the cloning, we could keep a Vec<Option> inside |
That's the best case in my opinion. I'll implement it and check how it'll look like. |
I suspect it’ll be easier if we merge #92 first. I’m happy to approve that other PR once we discuss the points raised there. |
fe427fc
to
ae2889c
Compare
Let's do that first, yes. |
ae2889c
to
ed168ee
Compare
ed168ee
to
b293d85
Compare
This PR implements my take on #86. The edit api now takes ownership of the migration it edits.
Closes #86