Skip to content

Commit 65a5b26

Browse files
committed
Improve edit api
1 parent 4b83559 commit 65a5b26

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

rusqlite_migration/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ include_dir = { version = "0.7.3", optional = true }
2525
tokio = { version = "1.25", features = ["macros"], optional = true }
2626
tokio-rusqlite = { version = "0.4.0", optional = true }
2727
log = "0.4"
28+
take_mut = "0.2.2"
2829

2930
[dependencies.rusqlite]
3031
version = ">=0.29.0"

rusqlite_migration/src/builder.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::iter::FromIterator;
22

33
use include_dir::Dir;
4+
use take_mut::take;
45

56
use crate::{loader::from_directory, MigrationHook, Result, M};
67

@@ -42,14 +43,11 @@ impl<'u> MigrationsBuilder<'u> {
4243
///
4344
/// Panics if no migration with the `id` provided exists.
4445
#[must_use]
45-
pub fn edit(mut self, id: usize, f: impl Fn(&mut M)) -> Self {
46+
pub fn edit(mut self, id: usize, f: impl Fn(M) -> M) -> Self {
4647
if id < 1 {
4748
panic!("id cannot be equal to 0");
4849
}
49-
f(self
50-
.migrations
51-
.get_mut(id - 1)
52-
.expect("No migration with the given index"));
50+
take(&mut self.migrations[id - 1], f);
5351
self
5452
}
5553

@@ -76,8 +74,9 @@ impl<'u> M<'u> {
7674
/// Use [`M::up_with_hook`] instead if you're creating a new migration.
7775
/// This method is meant for editing existing transactions
7876
/// when using the [`MigrationsBuilder`].
79-
pub fn set_up_hook(&mut self, hook: impl MigrationHook + 'static) {
77+
pub fn set_up_hook(mut self, hook: impl MigrationHook + 'static) -> Self {
8078
self.up_hook = Some(hook.clone_box());
79+
self
8180
}
8281

8382
/// Replace the `down_hook` in the given migration with the provided one.
@@ -87,7 +86,8 @@ impl<'u> M<'u> {
8786
/// Use [`M::down_with_hook`] instead if you're creating a new migration.
8887
/// This method is meant for editing existing transactions
8988
/// when using the [`MigrationsBuilder`].
90-
pub fn set_down_hook(&mut self, hook: impl MigrationHook + 'static) {
89+
pub fn set_down_hook(mut self, hook: impl MigrationHook + 'static) -> Self {
9190
self.down_hook = Some(hook.clone_box());
91+
self
9292
}
9393
}

rusqlite_migration/src/tests/builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use crate::{MigrationsBuilder, M};
77
fn test_non_existing_index() {
88
let ms = vec![M::up("CREATE TABLE t(a);")];
99

10-
let _ = MigrationsBuilder::from_iter(ms.clone()).edit(100, move |_t| {});
10+
let _ = MigrationsBuilder::from_iter(ms.clone()).edit(100, move |_t| _t);
1111
}
1212

1313
#[test]
1414
#[should_panic]
1515
fn test_0_index() {
1616
let ms = vec![M::up("CREATE TABLE t(a);")];
1717

18-
let _ = MigrationsBuilder::from_iter(ms).edit(0, move |_t| {});
18+
let _ = MigrationsBuilder::from_iter(ms).edit(0, move |_t| _t);
1919
}

0 commit comments

Comments
 (0)