Skip to content

Commit ae2889c

Browse files
committed
Improve edit api
1 parent 4b83559 commit ae2889c

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

rusqlite_migration/src/builder.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,11 @@ impl<'u> MigrationsBuilder<'u> {
4242
///
4343
/// Panics if no migration with the `id` provided exists.
4444
#[must_use]
45-
pub fn edit(mut self, id: usize, f: impl Fn(&mut M)) -> Self {
45+
pub fn edit(mut self, id: usize, f: impl Fn(M) -> M) -> Self {
4646
if id < 1 {
4747
panic!("id cannot be equal to 0");
4848
}
49-
f(self
50-
.migrations
51-
.get_mut(id - 1)
52-
.expect("No migration with the given index"));
49+
self.migrations[id - 1] = f(self.migrations[id - 1].clone());
5350
self
5451
}
5552

@@ -76,8 +73,9 @@ impl<'u> M<'u> {
7673
/// Use [`M::up_with_hook`] instead if you're creating a new migration.
7774
/// This method is meant for editing existing transactions
7875
/// when using the [`MigrationsBuilder`].
79-
pub fn set_up_hook(&mut self, hook: impl MigrationHook + 'static) {
76+
pub fn set_up_hook(mut self, hook: impl MigrationHook + 'static) -> Self {
8077
self.up_hook = Some(hook.clone_box());
78+
self
8179
}
8280

8381
/// Replace the `down_hook` in the given migration with the provided one.
@@ -87,7 +85,8 @@ impl<'u> M<'u> {
8785
/// Use [`M::down_with_hook`] instead if you're creating a new migration.
8886
/// This method is meant for editing existing transactions
8987
/// when using the [`MigrationsBuilder`].
90-
pub fn set_down_hook(&mut self, hook: impl MigrationHook + 'static) {
88+
pub fn set_down_hook(mut self, hook: impl MigrationHook + 'static) -> Self {
9189
self.down_hook = Some(hook.clone_box());
90+
self
9291
}
9392
}

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)