Skip to content

Commit fe427fc

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

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

rusqlite_migration/src/builder.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::iter::FromIterator;
1+
use std::{iter::FromIterator, mem::take};
22

33
use include_dir::Dir;
44

@@ -42,14 +42,12 @@ 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+
let m = take(&mut self.migrations[id - 1]);
50+
self.migrations[id - 1] = f(m);
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/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl Clone for Box<dyn MigrationHook> {
171171
/// A migration can contain up- and down-hooks, which are incomparable closures.
172172
/// To signify `M` equality we compare if two migrations either don't have hooks defined (they are set to `None`)
173173
/// or if the closure memory addresses are the same.
174-
#[derive(Debug, Clone)]
174+
#[derive(Debug, Clone, Default)]
175175
#[must_use]
176176
pub struct M<'u> {
177177
up: &'u str,

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)